M
assert.strictEqual
History
Added in: v0.1.21
v0.1.21
Introduced in: v0.1.21
v0.1.21
Message may now be a
printf-like format string or function.v26.0.0
Used comparison changed from Strict Equality to
Object.is().v10.0.0
assert.strictEqual(actual, expected, message?): void
Attributes
actual:
anyexpected:
anyPostfix
printf-like arguments in case
it's used as format string.
If message is a function, it is called in case of a comparison failure. The
function receives the actual and expected arguments and has to return a
string that is going to be used as error message.
printf-like format strings and functions are beneficial for performance
reasons in case arguments are passed through. In addition, it allows nice
formatting with ease.Tests strict equality between the actual and expected parameters as
determined by Object.is().
import assert from 'node:assert/strict'; assert.strictEqual(1, 2); // AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal: // // 1 !== 2 assert.strictEqual(1, 1); // OK assert.strictEqual('Hello foobar', 'Hello World!'); // AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal: // + actual - expected // // + 'Hello foobar' // - 'Hello World!' // ^ const apples = 1; const oranges = 2; assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`); // AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 assert.strictEqual(apples, oranges, 'apples %s !== oranges %s', apples, oranges); // AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 assert.strictEqual(1, '1', new TypeError('Inputs are not identical')); // TypeError: Inputs are not identical assert.strictEqual(apples, oranges, (actual, expected) => { // Do 'heavy' computations return `I expected ${expected} but I got ${actual}`; }); // AssertionError [ERR_ASSERTION]: I expected oranges but I got apples
const assert = require('node:assert/strict'); assert.strictEqual(1, 2); // AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal: // // 1 !== 2 assert.strictEqual(1, 1); // OK assert.strictEqual('Hello foobar', 'Hello World!'); // AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal: // + actual - expected // // + 'Hello foobar' // - 'Hello World!' // ^ const apples = 1; const oranges = 2; assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`); // AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 assert.strictEqual(apples, oranges, 'apples %s !== oranges %s', apples, oranges); // AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 assert.strictEqual(1, '1', new TypeError('Inputs are not identical')); // TypeError: Inputs are not identical assert.strictEqual(apples, oranges, (actual, expected) => { // Do 'heavy' computations return `I expected ${expected} but I got ${actual}`; }); // AssertionError [ERR_ASSERTION]: I expected oranges but I got apples
If the values are not strictly equal, an AssertionError is thrown with a
message property set equal to the value of the message parameter. If the
message parameter is undefined, a default error message is assigned. If the
message parameter is an instance of Error then it will be thrown
instead of the AssertionError.