class assert.AssertionError extends errors.Error
Indicates the failure of an assertion. All errors thrown by the node:assert
module will be instances of the AssertionError class.
new assert.AssertionError(options): assert.AssertionError
Attributes
options:
Objectmessage:
stringIf provided, the error message is set to this value.
actual:
anyThe
actual property on the error instance.expected:
anyThe
expected property on the error instance.operator:
stringThe
operator property on the error instance.stackStartFn:
FunctionIf provided, the generated stack trace omits
frames before this function.
diff:
stringIf set to
'full', shows the full diff in assertion errors. Defaults to 'simple'.
Accepted values: 'simple', 'full'.A subclass of Error that indicates the failure of an assertion.
All instances contain the built-in Error properties (message and name)
and:
Attributes
actual:
anySet to the
actual argument for methods such as
assert.strictEqual().expected:
anySet to the
expected value for methods such as
assert.strictEqual().generatedMessage:
booleanIndicates if the message was auto-generated
(
true) or not.code:
stringValue is always
ERR_ASSERTION to show that the error is an
assertion error.operator:
stringSet to the passed in operator value.
import assert from 'node:assert'; // Generate an AssertionError to compare the error message later: const { message } = new assert.AssertionError({ actual: 1, expected: 2, operator: 'strictEqual', }); // Verify error output: try { assert.strictEqual(1, 2); } catch (err) { assert(err instanceof assert.AssertionError); assert.strictEqual(err.message, message); assert.strictEqual(err.name, 'AssertionError'); assert.strictEqual(err.actual, 1); assert.strictEqual(err.expected, 2); assert.strictEqual(err.code, 'ERR_ASSERTION'); assert.strictEqual(err.operator, 'strictEqual'); assert.strictEqual(err.generatedMessage, true); }
const assert = require('node:assert'); // Generate an AssertionError to compare the error message later: const { message } = new assert.AssertionError({ actual: 1, expected: 2, operator: 'strictEqual', }); // Verify error output: try { assert.strictEqual(1, 2); } catch (err) { assert(err instanceof assert.AssertionError); assert.strictEqual(err.message, message); assert.strictEqual(err.name, 'AssertionError'); assert.strictEqual(err.actual, 1); assert.strictEqual(err.expected, 2); assert.strictEqual(err.code, 'ERR_ASSERTION'); assert.strictEqual(err.operator, 'strictEqual'); assert.strictEqual(err.generatedMessage, true); }