new assert.AssertionError(options)


  • options <Object>

    • message <string> 如果提供,则将错误消息设置为此值。
    • actual <any> 错误实例上的 actual 属性将包含此值。在内部用于 actual 错误输入,例如使用 assert.strictEqual()
    • expected <any> 错误实例上的 expected 属性将包含此值。在内部用于 expected 错误输入,例如使用 assert.strictEqual()
    • operator <string> 错误实例上的 operator 属性将包含此值。在内部用于表明用于比较的操作(或触发错误的断言函数)。
    • stackStartFn <Function> 如果提供,则生成的堆栈跟踪将移除所有帧直到提供的函数。

Error 的子类,表明断言的失败。

所有实例都包含内置的 Error 属性(messagename)以及:

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 [ERR_ASSERTION]');
  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 going to be set to this value.
    • actual <any> The actual property on the error instance is going to contain this value. Internally used for the actual error input in case e.g., assert.strictEqual() is used.
    • expected <any> The expected property on the error instance is going to contain this value. Internally used for the expected error input in case e.g., assert.strictEqual() is used.
    • operator <string> The operator property on the error instance is going to contain this value. Internally used to indicate what operation was used for comparison (or what assertion function triggered the error).
    • stackStartFn <Function> If provided, the generated stack trace is going to remove all frames up to the provided 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 the actual value in case e.g., assert.strictEqual() is used.
  • expected <any> Set to the expected value in case e.g., assert.strictEqual() is used.
  • generatedMessage <boolean> Indicates if the message was auto-generated (true) or not.
  • code <string> This is always set to the string ERR_ASSERTION to indicate that the error is actually 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 [ERR_ASSERTION]');
  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);
}