An alias of assert.ok.
namespace
assert.default
The input that is checked for being truthy.
class AssertionError
Indicates the failure of an assertion. All errors thrown by the
node:assertmodule will be instances of theAssertionErrorclass.- static stackTraceLimit: number
The
Error.stackTraceLimitproperty specifies the number of stack frames collected by a stack trace (whether generated bynew Error().stackorError.captureStackTrace(obj)).The default value is
10but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
- targetObject: object,constructorOpt?: Function): void;
Create .stack property on a target object
interface Assert
- doesNotReject: {(block: Promise<unknown> | () => Promise<unknown>, message?: string | Error) => Promise<void>; (block: Promise<unknown> | () => Promise<unknown>, error: AssertPredicate, message?: string | Error) => Promise<void>}
- doesNotThrow: {(block: () => unknown, message?: string | Error) => void; (block: () => unknown, error: AssertPredicate, message?: string | Error) => void}
- rejects: {(block: Promise<unknown> | () => Promise<unknown>, message?: string | Error) => Promise<void>; (block: Promise<unknown> | () => Promise<unknown>, error: AssertPredicate, message?: string | Error) => Promise<void>}
- throws: {(block: () => unknown, message?: string | Error) => void; (block: () => unknown, error: AssertPredicate, message?: string | Error) => void}
interface AssertionErrorOptions
interface AssertOptions
- skipPrototype?: boolean
If set to
true, skips prototype and constructor comparison in deep equality checks.
interface AssertStrict
- doesNotReject: {(block: Promise<unknown> | () => Promise<unknown>, message?: string | Error) => Promise<void>; (block: Promise<unknown> | () => Promise<unknown>, error: AssertPredicate, message?: string | Error) => Promise<void>}
- doesNotThrow: {(block: () => unknown, message?: string | Error) => void; (block: () => unknown, error: AssertPredicate, message?: string | Error) => void}
- rejects: {(block: Promise<unknown> | () => Promise<unknown>, message?: string | Error) => Promise<void>; (block: Promise<unknown> | () => Promise<unknown>, error: AssertPredicate, message?: string | Error) => Promise<void>}
- throws: {(block: () => unknown, message?: string | Error) => void; (block: () => unknown, error: AssertPredicate, message?: string | Error) => void}
interface CallTrackerCall
- type AssertMethodNames = 'deepEqual' | 'deepStrictEqual' | 'doesNotMatch' | 'doesNotReject' | 'doesNotThrow' | 'equal' | 'fail' | 'ifError' | 'match' | 'notDeepEqual' | 'notDeepStrictEqual' | 'notEqual' | 'notStrictEqual' | 'ok' | 'partialDeepStrictEqual' | 'rejects' | 'strictEqual' | 'throws'
- type AssertPredicate = RegExp | new () => object | (thrown: unknown) => boolean | object | Error
- const Assert: {new (options?: AssertOptions & { strict: true }) => AssertStrict; new (options: AssertOptions) => Assert}
The
Assertclass allows creating independent assertion instances with custom options. - actual: unknown,expected: unknown,): void;
Strict assertion mode
An alias of deepStrictEqual.
Legacy assertion mode
Stability: 3 - Legacy: Use deepStrictEqual instead.
Tests for deep equality between the
actualandexpectedparameters. Consider using deepStrictEqual instead. deepEqual can have surprising results.Deep equality means that the enumerable "own" properties of child objects are also recursively evaluated by the following rules.
- actual: unknown,expected: T,): asserts actual is T;
Tests for deep equality between the
actualandexpectedparameters. "Deep" equality means that the enumerable "own" properties of child objects are recursively evaluated also by the following rules. - value: string,regExp: RegExp,): void;
Expects the
stringinput not to match the regular expression.import assert from 'node:assert/strict'; assert.doesNotMatch('I will fail', /fail/); // AssertionError [ERR_ASSERTION]: The input was expected to not match the ... assert.doesNotMatch(123, /pass/); // AssertionError [ERR_ASSERTION]: The "string" argument must be of type string. assert.doesNotMatch('I will pass', /different/); // OKIf the values do match, or if the
stringargument is of another type thanstring, an{@link AssertionError}is thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of an Error then it will be thrown instead of the{@link AssertionError}. - block: Promise<unknown> | () => Promise<unknown>,): Promise<void>;
Awaits the
asyncFnpromise or, ifasyncFnis a function, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is not rejected.If
asyncFnis a function and it throws an error synchronously,assert.doesNotReject()will return a rejectedPromisewith that error. If the function does not return a promise,assert.doesNotReject()will return a rejectedPromisewith an ERR_INVALID_RETURN_VALUE error. In both cases the error handler is skipped.Using
assert.doesNotReject()is actually not useful because there is little benefit in catching a rejection and then rejecting it again. Instead, consider adding a comment next to the specific code path that should not reject and keep error messages as expressive as possible.If specified,
errorcan be aClass,RegExp, or a validation function. See throws for more details.Besides the async nature to await the completion behaves identically to doesNotThrow.
import assert from 'node:assert/strict'; await assert.doesNotReject( async () => { throw new TypeError('Wrong value'); }, SyntaxError, );import assert from 'node:assert/strict'; assert.doesNotReject(Promise.reject(new TypeError('Wrong value'))) .then(() => { // ... });block: Promise<unknown> | () => Promise<unknown>,): Promise<void>;Awaits the
asyncFnpromise or, ifasyncFnis a function, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is not rejected.If
asyncFnis a function and it throws an error synchronously,assert.doesNotReject()will return a rejectedPromisewith that error. If the function does not return a promise,assert.doesNotReject()will return a rejectedPromisewith an ERR_INVALID_RETURN_VALUE error. In both cases the error handler is skipped.Using
assert.doesNotReject()is actually not useful because there is little benefit in catching a rejection and then rejecting it again. Instead, consider adding a comment next to the specific code path that should not reject and keep error messages as expressive as possible.If specified,
errorcan be aClass,RegExp, or a validation function. See throws for more details.Besides the async nature to await the completion behaves identically to doesNotThrow.
import assert from 'node:assert/strict'; await assert.doesNotReject( async () => { throw new TypeError('Wrong value'); }, SyntaxError, );import assert from 'node:assert/strict'; assert.doesNotReject(Promise.reject(new TypeError('Wrong value'))) .then(() => { // ... }); - block: () => unknown,): void;
Asserts that the function
fndoes not throw an error.Using
assert.doesNotThrow()is actually not useful because there is no benefit in catching an error and then rethrowing it. Instead, consider adding a comment next to the specific code path that should not throw and keep error messages as expressive as possible.When
assert.doesNotThrow()is called, it will immediately call thefnfunction.If an error is thrown and it is the same type as that specified by the
errorparameter, then anAssertionErroris thrown. If the error is of a different type, or if theerrorparameter is undefined, the error is propagated back to the caller.If specified,
errorcan be aClass,RegExp, or a validation function. See throws for more details.The following, for instance, will throw the
TypeErrorbecause there is no matching error type in the assertion:import assert from 'node:assert/strict'; assert.doesNotThrow( () => { throw new TypeError('Wrong value'); }, SyntaxError, );However, the following will result in an
AssertionErrorwith the message 'Got unwanted exception...':import assert from 'node:assert/strict'; assert.doesNotThrow( () => { throw new TypeError('Wrong value'); }, TypeError, );If an
AssertionErroris thrown and a value is provided for themessageparameter, the value ofmessagewill be appended to theAssertionErrormessage:import assert from 'node:assert/strict'; assert.doesNotThrow( () => { throw new TypeError('Wrong value'); }, /Wrong value/, 'Whoops', ); // Throws: AssertionError: Got unwanted exception: Whoopsblock: () => unknown,): void;Asserts that the function
fndoes not throw an error.Using
assert.doesNotThrow()is actually not useful because there is no benefit in catching an error and then rethrowing it. Instead, consider adding a comment next to the specific code path that should not throw and keep error messages as expressive as possible.When
assert.doesNotThrow()is called, it will immediately call thefnfunction.If an error is thrown and it is the same type as that specified by the
errorparameter, then anAssertionErroris thrown. If the error is of a different type, or if theerrorparameter is undefined, the error is propagated back to the caller.If specified,
errorcan be aClass,RegExp, or a validation function. See throws for more details.The following, for instance, will throw the
TypeErrorbecause there is no matching error type in the assertion:import assert from 'node:assert/strict'; assert.doesNotThrow( () => { throw new TypeError('Wrong value'); }, SyntaxError, );However, the following will result in an
AssertionErrorwith the message 'Got unwanted exception...':import assert from 'node:assert/strict'; assert.doesNotThrow( () => { throw new TypeError('Wrong value'); }, TypeError, );If an
AssertionErroris thrown and a value is provided for themessageparameter, the value ofmessagewill be appended to theAssertionErrormessage:import assert from 'node:assert/strict'; assert.doesNotThrow( () => { throw new TypeError('Wrong value'); }, /Wrong value/, 'Whoops', ); // Throws: AssertionError: Got unwanted exception: Whoops - actual: unknown,expected: unknown,): void;
Strict assertion mode
An alias of strictEqual.
Legacy assertion mode
Stability: 3 - Legacy: Use strictEqual instead.
Tests shallow, coercive equality between the
actualandexpectedparameters using the==operator.NaNis specially handled and treated as being identical if both sides areNaN.import assert from 'node:assert'; assert.equal(1, 1); // OK, 1 == 1 assert.equal(1, '1'); // OK, 1 == '1' assert.equal(NaN, NaN); // OK assert.equal(1, 2); // AssertionError: 1 == 2 assert.equal({ a: { b: 1 } }, { a: { b: 1 } }); // AssertionError: { a: { b: 1 } } == { a: { b: 1 } }If the values are not equal, an
AssertionErroris thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of anErrorthen it will be thrown instead of theAssertionError. - value: unknown): asserts value is undefined | null;
Throws
valueifvalueis notundefinedornull. This is useful when testing theerrorargument in callbacks. The stack trace contains all frames from the error passed toifError()including the potential new frames forifError()itself.import assert from 'node:assert/strict'; assert.ifError(null); // OK assert.ifError(0); // AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0 assert.ifError('error'); // AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error' assert.ifError(new Error()); // AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error // Create some random error frames. let err; (function errorFrame() { err = new Error('test error'); })(); (function ifErrorFrame() { assert.ifError(err); })(); // AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error // at ifErrorFrame // at errorFrame - value: string,regExp: RegExp,): void;
Expects the
stringinput to match the regular expression.import assert from 'node:assert/strict'; assert.match('I will fail', /pass/); // AssertionError [ERR_ASSERTION]: The input did not match the regular ... assert.match(123, /pass/); // AssertionError [ERR_ASSERTION]: The "string" argument must be of type string. assert.match('I will pass', /pass/); // OKIf the values do not match, or if the
stringargument is of another type thanstring, an{@link AssertionError}is thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of an Error then it will be thrown instead of the{@link AssertionError}. - actual: unknown,expected: unknown,): void;
Strict assertion mode
An alias of notDeepStrictEqual.
Legacy assertion mode
Stability: 3 - Legacy: Use notDeepStrictEqual instead.
Tests for any deep inequality. Opposite of deepEqual.
import assert from 'node:assert'; const obj1 = { a: { b: 1, }, }; const obj2 = { a: { b: 2, }, }; const obj3 = { a: { b: 1, }, }; const obj4 = { __proto__: obj1 }; assert.notDeepEqual(obj1, obj1); // AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } } assert.notDeepEqual(obj1, obj2); // OK assert.notDeepEqual(obj1, obj3); // AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } } assert.notDeepEqual(obj1, obj4); // OKIf the values are deeply equal, an
AssertionErroris thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of anErrorthen it will be thrown instead of theAssertionError. - actual: unknown,expected: unknown,): void;
Tests for deep strict inequality. Opposite of deepStrictEqual.
import assert from 'node:assert/strict'; assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); // OKIf the values are deeply and strictly equal, an
AssertionErroris thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of anErrorthen it will be thrown instead of theAssertionError. - actual: unknown,expected: unknown,): void;
Strict assertion mode
An alias of notStrictEqual.
Legacy assertion mode
Stability: 3 - Legacy: Use notStrictEqual instead.
Tests shallow, coercive inequality with the
!=operator.NaNis specially handled and treated as being identical if both sides areNaN.import assert from 'node:assert'; assert.notEqual(1, 2); // OK assert.notEqual(1, 1); // AssertionError: 1 != 1 assert.notEqual(1, '1'); // AssertionError: 1 != '1'If the values are equal, an
AssertionErroris thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of anErrorthen it will be thrown instead of theAssertionError. - actual: unknown,expected: unknown,): void;
Tests strict inequality between the
actualandexpectedparameters as determined byObject.is().import assert from 'node:assert/strict'; assert.notStrictEqual(1, 2); // OK assert.notStrictEqual(1, 1); // AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to: // // 1 assert.notStrictEqual(1, '1'); // OKIf the values are strictly equal, an
AssertionErroris thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of anErrorthen it will be thrown instead of theAssertionError. - value: unknown,): asserts value;
Tests if
valueis truthy. It is equivalent toassert.equal(!!value, true, message).If
valueis not truthy, anAssertionErroris thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter isundefined, a default error message is assigned. If themessageparameter is an instance of anErrorthen it will be thrown instead of theAssertionError. If no arguments are passed in at allmessagewill be set to the string:'No value argument passed to `assert.ok()`'.Be aware that in the
replthe error message will be different to the one thrown in a file! See below for further details.import assert from 'node:assert/strict'; assert.ok(true); // OK assert.ok(1); // OK assert.ok(); // AssertionError: No value argument passed to `assert.ok()` assert.ok(false, 'it\'s false'); // AssertionError: it's false // In the repl: assert.ok(typeof 123 === 'string'); // AssertionError: false == true // In a file (e.g. test.js): assert.ok(typeof 123 === 'string'); // AssertionError: The expression evaluated to a falsy value: // // assert.ok(typeof 123 === 'string') assert.ok(false); // AssertionError: The expression evaluated to a falsy value: // // assert.ok(false) assert.ok(0); // AssertionError: The expression evaluated to a falsy value: // // assert.ok(0)import assert from 'node:assert/strict'; // Using `assert()` works the same: assert(0); // AssertionError: The expression evaluated to a falsy value: // // assert(0) - actual: unknown,expected: unknown,): void;
Tests for partial deep equality between the
actualandexpectedparameters. "Deep" equality means that the enumerable "own" properties of child objects are recursively evaluated also by the following rules. "Partial" equality means that only properties that exist on theexpectedparameter are going to be compared.This method always passes the same test cases as
assert.deepStrictEqual(), behaving as a super set of it. - block: Promise<unknown> | () => Promise<unknown>,): Promise<void>;
Awaits the
asyncFnpromise or, ifasyncFnis a function, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is rejected.If
asyncFnis a function and it throws an error synchronously,assert.rejects()will return a rejectedPromisewith that error. If the function does not return a promise,assert.rejects()will return a rejectedPromisewith an ERR_INVALID_RETURN_VALUE error. In both cases the error handler is skipped.Besides the async nature to await the completion behaves identically to throws.
If specified,
errorcan be aClass,RegExp, a validation function, an object where each property will be tested for, or an instance of error where each property will be tested for including the non-enumerablemessageandnameproperties.If specified,
messagewill be the message provided by the{@link AssertionError}if theasyncFnfails to reject.import assert from 'node:assert/strict'; await assert.rejects( async () => { throw new TypeError('Wrong value'); }, { name: 'TypeError', message: 'Wrong value', }, );import assert from 'node:assert/strict'; await assert.rejects( async () => { throw new TypeError('Wrong value'); }, (err) => { assert.strictEqual(err.name, 'TypeError'); assert.strictEqual(err.message, 'Wrong value'); return true; }, );import assert from 'node:assert/strict'; assert.rejects( Promise.reject(new Error('Wrong value')), Error, ).then(() => { // ... });errorcannot be a string. If a string is provided as the second argument, thenerroris assumed to be omitted and the string will be used formessageinstead. This can lead to easy-to-miss mistakes. Please read the example in throws carefully if using a string as the second argument gets considered.block: Promise<unknown> | () => Promise<unknown>,): Promise<void>;Awaits the
asyncFnpromise or, ifasyncFnis a function, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is rejected.If
asyncFnis a function and it throws an error synchronously,assert.rejects()will return a rejectedPromisewith that error. If the function does not return a promise,assert.rejects()will return a rejectedPromisewith an ERR_INVALID_RETURN_VALUE error. In both cases the error handler is skipped.Besides the async nature to await the completion behaves identically to throws.
If specified,
errorcan be aClass,RegExp, a validation function, an object where each property will be tested for, or an instance of error where each property will be tested for including the non-enumerablemessageandnameproperties.If specified,
messagewill be the message provided by the{@link AssertionError}if theasyncFnfails to reject.import assert from 'node:assert/strict'; await assert.rejects( async () => { throw new TypeError('Wrong value'); }, { name: 'TypeError', message: 'Wrong value', }, );import assert from 'node:assert/strict'; await assert.rejects( async () => { throw new TypeError('Wrong value'); }, (err) => { assert.strictEqual(err.name, 'TypeError'); assert.strictEqual(err.message, 'Wrong value'); return true; }, );import assert from 'node:assert/strict'; assert.rejects( Promise.reject(new Error('Wrong value')), Error, ).then(() => { // ... });errorcannot be a string. If a string is provided as the second argument, thenerroris assumed to be omitted and the string will be used formessageinstead. This can lead to easy-to-miss mistakes. Please read the example in throws carefully if using a string as the second argument gets considered. - actual: unknown,expected: T,): asserts actual is T;
Tests strict equality between the
actualandexpectedparameters as determined byObject.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(1, '1', new TypeError('Inputs are not identical')); // TypeError: Inputs are not identicalIf the values are not strictly equal, an
AssertionErroris thrown with amessageproperty set equal to the value of themessageparameter. If themessageparameter is undefined, a default error message is assigned. If themessageparameter is an instance of anErrorthen it will be thrown instead of theAssertionError. - block: () => unknown,): void;
Expects the function
fnto throw an error.If specified,
errorcan be aClass,RegExp, a validation function, a validation object where each property will be tested for strict deep equality, or an instance of error where each property will be tested for strict deep equality including the non-enumerablemessageandnameproperties. When using an object, it is also possible to use a regular expression, when validating against a string property. See below for examples.If specified,
messagewill be appended to the message provided by theAssertionErrorif thefncall fails to throw or in case the error validation fails.Custom validation object/error instance:
import assert from 'node:assert/strict'; const err = new TypeError('Wrong value'); err.code = 404; err.foo = 'bar'; err.info = { nested: true, baz: 'text', }; err.reg = /abc/i; assert.throws( () => { throw err; }, { name: 'TypeError', message: 'Wrong value', info: { nested: true, baz: 'text', }, // Only properties on the validation object will be tested for. // Using nested objects requires all properties to be present. Otherwise // the validation is going to fail. }, ); // Using regular expressions to validate error properties: assert.throws( () => { throw err; }, { // The `name` and `message` properties are strings and using regular // expressions on those will match against the string. If they fail, an // error is thrown. name: /^TypeError$/, message: /Wrong/, foo: 'bar', info: { nested: true, // It is not possible to use regular expressions for nested properties! baz: 'text', }, // The `reg` property contains a regular expression and only if the // validation object contains an identical regular expression, it is going // to pass. reg: /abc/i, }, ); // Fails due to the different `message` and `name` properties: assert.throws( () => { const otherErr = new Error('Not found'); // Copy all enumerable properties from `err` to `otherErr`. for (const [key, value] of Object.entries(err)) { otherErr[key] = value; } throw otherErr; }, // The error's `message` and `name` properties will also be checked when using // an error as validation object. err, );Validate instanceof using constructor:
import assert from 'node:assert/strict'; assert.throws( () => { throw new Error('Wrong value'); }, Error, );Validate error message using
RegExp:Using a regular expression runs
.toStringon the error object, and will therefore also include the error name.import assert from 'node:assert/strict'; assert.throws( () => { throw new Error('Wrong value'); }, /^Error: Wrong value$/, );Custom error validation:
The function must return
trueto indicate all internal validations passed. It will otherwise fail with anAssertionError.import assert from 'node:assert/strict'; assert.throws( () => { throw new Error('Wrong value'); }, (err) => { assert(err instanceof Error); assert(/value/.test(err)); // Avoid returning anything from validation functions besides `true`. // Otherwise, it's not clear what part of the validation failed. Instead, // throw an error about the specific validation that failed (as done in this // example) and add as much helpful debugging information to that error as // possible. return true; }, 'unexpected error', );errorcannot be a string. If a string is provided as the second argument, thenerroris assumed to be omitted and the string will be used formessageinstead. This can lead to easy-to-miss mistakes. Using the same message as the thrown error message is going to result in anERR_AMBIGUOUS_ARGUMENTerror. Please read the example below carefully if using a string as the second argument gets considered:import assert from 'node:assert/strict'; function throwingFirst() { throw new Error('First'); } function throwingSecond() { throw new Error('Second'); } function notThrowing() {} // The second argument is a string and the input function threw an Error. // The first case will not throw as it does not match for the error message // thrown by the input function! assert.throws(throwingFirst, 'Second'); // In the next example the message has no benefit over the message from the // error and since it is not clear if the user intended to actually match // against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error. assert.throws(throwingSecond, 'Second'); // TypeError [ERR_AMBIGUOUS_ARGUMENT] // The string is only used (as message) in case the function does not throw: assert.throws(notThrowing, 'Second'); // AssertionError [ERR_ASSERTION]: Missing expected exception: Second // If it was intended to match for the error message do this instead: // It does not throw because the error messages match. assert.throws(throwingSecond, /Second$/); // If the error message does not match, an AssertionError is thrown. assert.throws(throwingFirst, /Second$/); // AssertionError [ERR_ASSERTION]Due to the confusing error-prone notation, avoid a string as the second argument.
block: () => unknown,): void;Expects the function
fnto throw an error.If specified,
errorcan be aClass,RegExp, a validation function, a validation object where each property will be tested for strict deep equality, or an instance of error where each property will be tested for strict deep equality including the non-enumerablemessageandnameproperties. When using an object, it is also possible to use a regular expression, when validating against a string property. See below for examples.If specified,
messagewill be appended to the message provided by theAssertionErrorif thefncall fails to throw or in case the error validation fails.Custom validation object/error instance:
import assert from 'node:assert/strict'; const err = new TypeError('Wrong value'); err.code = 404; err.foo = 'bar'; err.info = { nested: true, baz: 'text', }; err.reg = /abc/i; assert.throws( () => { throw err; }, { name: 'TypeError', message: 'Wrong value', info: { nested: true, baz: 'text', }, // Only properties on the validation object will be tested for. // Using nested objects requires all properties to be present. Otherwise // the validation is going to fail. }, ); // Using regular expressions to validate error properties: assert.throws( () => { throw err; }, { // The `name` and `message` properties are strings and using regular // expressions on those will match against the string. If they fail, an // error is thrown. name: /^TypeError$/, message: /Wrong/, foo: 'bar', info: { nested: true, // It is not possible to use regular expressions for nested properties! baz: 'text', }, // The `reg` property contains a regular expression and only if the // validation object contains an identical regular expression, it is going // to pass. reg: /abc/i, }, ); // Fails due to the different `message` and `name` properties: assert.throws( () => { const otherErr = new Error('Not found'); // Copy all enumerable properties from `err` to `otherErr`. for (const [key, value] of Object.entries(err)) { otherErr[key] = value; } throw otherErr; }, // The error's `message` and `name` properties will also be checked when using // an error as validation object. err, );Validate instanceof using constructor:
import assert from 'node:assert/strict'; assert.throws( () => { throw new Error('Wrong value'); }, Error, );Validate error message using
RegExp:Using a regular expression runs
.toStringon the error object, and will therefore also include the error name.import assert from 'node:assert/strict'; assert.throws( () => { throw new Error('Wrong value'); }, /^Error: Wrong value$/, );Custom error validation:
The function must return
trueto indicate all internal validations passed. It will otherwise fail with anAssertionError.import assert from 'node:assert/strict'; assert.throws( () => { throw new Error('Wrong value'); }, (err) => { assert(err instanceof Error); assert(/value/.test(err)); // Avoid returning anything from validation functions besides `true`. // Otherwise, it's not clear what part of the validation failed. Instead, // throw an error about the specific validation that failed (as done in this // example) and add as much helpful debugging information to that error as // possible. return true; }, 'unexpected error', );errorcannot be a string. If a string is provided as the second argument, thenerroris assumed to be omitted and the string will be used formessageinstead. This can lead to easy-to-miss mistakes. Using the same message as the thrown error message is going to result in anERR_AMBIGUOUS_ARGUMENTerror. Please read the example below carefully if using a string as the second argument gets considered:import assert from 'node:assert/strict'; function throwingFirst() { throw new Error('First'); } function throwingSecond() { throw new Error('Second'); } function notThrowing() {} // The second argument is a string and the input function threw an Error. // The first case will not throw as it does not match for the error message // thrown by the input function! assert.throws(throwingFirst, 'Second'); // In the next example the message has no benefit over the message from the // error and since it is not clear if the user intended to actually match // against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error. assert.throws(throwingSecond, 'Second'); // TypeError [ERR_AMBIGUOUS_ARGUMENT] // The string is only used (as message) in case the function does not throw: assert.throws(notThrowing, 'Second'); // AssertionError [ERR_ASSERTION]: Missing expected exception: Second // If it was intended to match for the error message do this instead: // It does not throw because the error messages match. assert.throws(throwingSecond, /Second$/); // If the error message does not match, an AssertionError is thrown. assert.throws(throwingFirst, /Second$/); // AssertionError [ERR_ASSERTION]Due to the confusing error-prone notation, avoid a string as the second argument.