Assert
This module is used for writing unit tests for your applications.
First things first, do const assert = require('assert');
// `assert.notEqual(actual, expected[, message])`
// shallow comparison with `!=` operator
assert.notEqual(1, 1); // Assertion error
assert.notEqual(1, '1'); // Assertion error
assert.notEqual({name: 'K'}, {name: 'K'}) // OK
// `assert.notStrictEqual(actual, expected[, message])`
// comparison with `!==` operator
assert.notStrictEqual(1, 1); // Assertion error
assert.notStrictEqual(1, '1'); // OK
assert.notStrictEqual({name: 'K'}, {name: 'K'}) // OK
// `assert.ok(value[, message])`
// check for truthy value
assert.ok(1); // OK
assert.ok({}); // OK
assert.ok(new Error()); // OK
assert.ok(null); // Assertion error
// `assert.strictEqual(actual, expected[, message])`
// comparison with '===' operator
// `assert.equal(actual, expected[, message])`
// shallow comparison with '==' operator
// `assert.throws(block[, error][, message])`
// expects `block` function to throw an error of constructor
// defined in second argument
assert.throws(() => {throw new Error();}); // OK
assert.throws(() => {console.log('No errors in this block');}); // Assertion error
assert.throws(() => {throw new Error();}, Error); // OK
assert.throws(() => {throw new Error();}, Warning); // Assertion error
// `assert.doesNotThrow(block[, message])`
// doesn't expect `block` function to throw an error
// `assert.deepEqual(actual, expected[, message])`
// deep comparison
assert.deepEqual({
name: 'g',
age: 11,
kids: [4, 8, 9, 10],
adderss: {
street: 11,
city: 'TY',
state: [{id: 0, name: 'KKR'}]
}
}, {
name: 'g',
age: 11,
kids: [4, 8, 9, 10],
adderss: {
street: 11,
city: 'TY',
state: [{id: 0, name: 'KKR'}]
}
}); // OK
// `assert.ifError(value)`
// if truthy then throws it back
// if falsy do nothing
// if error then throws it back (useful while testing the first argument, error in callbacks.)
assert.ifError(56); // OK i.e. throws 56
assert.ifError(null); // OK
assert.ifError(new Error()); // throws error