If specified, error can be a Class,
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-enumerable message and name properties. 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, message will be appended to the message provided by theAssertionError if the fn call fails to throw or in case the error validation
fails.
assert.throws( () => { throwerr; }, { 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: throws( () => { throwerr; }, { // 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: throws( () => { constotherErr = newError('Not found'); // Copy all enumerable properties from `err` to `otherErr`. for (const [key, value] ofObject.entries(err)) { otherErr[key] = value; } throwotherErr; }, // The error's `message` and `name` properties will also be checked when using // an error as validation object. err );
The function must return true to indicate all internal validations passed.
It will otherwise fail with an AssertionError.
importassertfrom'assert/strict';
assert.throws( () => { thrownewError('Wrong value'); }, (err) => { assert(errinstanceofError); 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. returntrue; }, 'unexpected error' );
error cannot be a string. If a string is provided as the second
argument, then error is assumed to be omitted and the string will be used formessage instead. This can lead to easy-to-miss mistakes. Using the same
message as the thrown error message is going to result in anERR_AMBIGUOUS_ARGUMENT error. Please read the example below carefully if using
a string as the second argument gets considered:
// 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.
Expects the function
fn
to throw an error.If specified,
error
can 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-enumerablemessage
andname
properties. 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,
message
will be appended to the message provided by theAssertionError
if thefn
call fails to throw or in case the error validation fails.Custom validation object/error instance:
Validate instanceof using constructor:
Validate error message using
RegExp
:Using a regular expression runs
.toString
on the error object, and will therefore also include the error name.Custom error validation:
The function must return
true
to indicate all internal validations passed. It will otherwise fail with anAssertionError
.error
cannot be a string. If a string is provided as the second argument, thenerror
is assumed to be omitted and the string will be used formessage
instead. This can lead to easy-to-miss mistakes. Using the same message as the thrown error message is going to result in anERR_AMBIGUOUS_ARGUMENT
error. Please read the example below carefully if using a string as the second argument gets considered:Due to the confusing error-prone notation, avoid a string as the second argument.