new assert.AssertionError(options)
-
options<Object>-
message<string> 如果提供,则错误消息将设置为此值。¥
message<string> If provided, the error message is set to this value. -
actual<any> 错误实例上的actual属性。¥
actual<any> Theactualproperty on the error instance. -
expected<any> 错误实例上的expected属性。¥
expected<any> Theexpectedproperty on the error instance. -
operator<string> 错误实例上的operator属性。¥
operator<string> Theoperatorproperty on the error instance. -
stackStartFn<Function> 如果提供,则生成的堆栈跟踪将省略此函数之前的帧。¥
stackStartFn<Function> If provided, the generated stack trace omits frames before this function. -
diff<string> 如果设置为'full',则显示断言错误的完整差异。默认为'simple'。可接受的值:'simple','full'.¥
diff<string> If set to'full', shows the full diff in assertion errors. Defaults to'simple'. Accepted values:'simple','full'.
-
<Error> 的子类,表示断言的失败。
¥A subclass of <Error> that indicates the failure of an assertion.
所有实例都包含内置的 Error 属性(message 和 name),以及:
¥All instances contain the built-in Error properties (message and name)
and:
-
actual<any> 对于assert.strictEqual()等方法,设置为actual参数。¥
actual<any> Set to theactualargument for methods such asassert.strictEqual(). -
expected<any> 对于assert.strictEqual()等方法,设置为expected值。¥
expected<any> Set to theexpectedvalue for methods such asassert.strictEqual(). -
generatedMessage<boolean> 指示消息是否是自动生成的 (true)。¥
generatedMessage<boolean> Indicates if the message was auto-generated (true) or not. -
code<string> 值始终为ERR_ASSERTION,以表明该错误是断言错误。¥
code<string> Value is alwaysERR_ASSERTIONto show that the error is an assertion error. -
operator<string> 设置为传入的运算符值。¥
operator<string> Set 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);
}