Node.js Reference ยท util, assert and the Test Runner
Node.js assert.strictEqual() method
Reference for assert.strictEqual() in Node.js: compare with Object.is semantics, read the failure diff, and know how it differs from loose assert.equal().
assert.strictEqual(actual, expected) passes when the two values are identical according to Object.is(), and throws an AssertionError otherwise. It is the everyday assertion for primitives such as numbers, strings and booleans, and for checking that two variables point to the very same object.
It does not look inside objects. Two separate objects with the same contents are not strictly equal; use assert.deepStrictEqual() for that. The failure message shows both values, so you see what you got and what you wanted.
import assert from "node:assert/strict"; assert.strictEqual(1 + 1, 2); assert.strictEqual("a" + "b", "ab");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
assert.strictEqual(actual: unknown, expected: unknown, message?: string | Error): void assert.notStrictEqual(actual: unknown, expected: unknown, message?: string | Error): void
| Name | Description |
|---|---|
actual |
The value your code produced. |
expected |
The value you expect. |
message |
Optional text, or an Error that is thrown instead of the default one. |
Returns: undefined.
Errors: an AssertionError with operator "strictEqual" (or "notStrictEqual"). Its actual and expected properties hold the two values. With a custom message, generatedMessage is false.
Usage notes
- The comparison is
Object.is():NaNequalsNaN, but0does not equal-0. This differs from===in exactly those two cases. - No type conversion happens.
1and"1"are different. - Put the value from your code first and the literal you expect second, so the diff reads "actual versus expected" correctly.
- For long multi-line strings, the failure message prints a line diff, which makes small differences easy to spot.
- In
node:assert/strict,assert.equalis an alias ofassert.strictEqual. In the legacynode:assertmoduleequal()uses==and hides bugs, so always import the strict module. - Objects and arrays compare by reference. A custom message of
Values have same structure but are not reference-equaltells you to switch todeepStrictEqual.