new assert.AssertionError(options)
options
<Object>message
<string> 如果提供,则将错误消息设置为此值。actual
<any> 错误实例上的actual
属性将包含此值。expected
<any> 错误实例上的expected
属性将包含此值。operator
<string> 错误实例上的operator
属性将包含此值。stackStartFn
<Function> 如果提供,则生成的堆栈跟踪将移除所有帧直到提供的函数。
Error
的子类,表明断言的失败。
所有实例都包含内置的 Error
属性(message
和 name
)以及:
actual
<any> 设置方法的actual
参数,例如assert.strictEqual()
。expected
<any> 设置方法的expected
参数,例如assert.strictEqual()
。generatedMessage
<boolean> 表明消息是否是自动生成的。code
<string> 始终设置为字符串ERR_ASSERTION
以表明错误实际上是断言错误。operator
<string> 设置为传入的运算符值。
const assert = require('assert');
// 生成 AssertionError 以便稍后比较错误的消息:
const { message } = new assert.AssertionError({
actual: 1,
expected: 2,
operator: 'strictEqual'
});
// 验证错误的输出:
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);
}
options
<Object>message
<string> If provided, the error message is set to this value.actual
<any> Theactual
property on the error instance.expected
<any> Theexpected
property on the error instance.operator
<string> Theoperator
property on the error instance.stackStartFn
<Function> If provided, the generated stack trace omits frames before this function.
A subclass of Error
that indicates the failure of an assertion.
All instances contain the built-in Error
properties (message
and name
)
and:
actual
<any> Set to theactual
argument for methods such asassert.strictEqual()
.expected
<any> Set to theexpected
value for methods such asassert.strictEqual()
.generatedMessage
<boolean> Indicates if the message was auto-generated (true
) or not.code
<string> Value is alwaysERR_ASSERTION
to show that the error is an assertion error.operator
<string> Set to the passed in operator value.
const assert = require('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);
}