Node Test Runner got released with Node v20 as a stable feature. This means we can test our NodeJS applications without installing other 3rd party applications. I have already made an intro on this feature you can read in this article. But to do testing, we need to use some assertion methods. And in this article, I will cover some of them.
Package
To use assertion methods, we need to import them from the appropriate package. Test functions are all located in the node:test package, however, assertion methods are located in the node:assert.
import assert from ‘node:assert’;
Equality
With this feature, there are multiple functions for testing equality. Some test equality of value, but others test the equality of objects. On top of that, some test strict equality, while others don’t. And the difference between those is the same as the difference between double and triple equal.
describe("equality", () => {
it("equal", () => {
assert.equal(1, "1");
});
it("not equal", () => {
assert.notEqual(1, 2);
});
it("strict equal", () => {
assert.strictEqual(1, 1);
});
it("strict not equal", () => {
assert.notStrictEqual(1, "1");
});
it("deep equal", () => {
const result = {a: 1, b: {c: 2}};
const expected =…