Node.js v14.11.0 文档


assert(断言)#

中英对照提交修改

稳定性: 2 - 稳定

源代码: lib/assert.js

assert 模块提供了一组断言函数,用于验证不变量。

严格的断言模式#

中英对照提交修改

在严格的断言模式中,非严格方法的行为类似于其对应的严格方法。 例如,assert.deepEqual() 的行为类似于 assert.deepStrictEqual()

在严格的断言模式中,对象的错误消息会显示差异。 在传统的断言模式中,对象的错误消息会显示对象(通常是截断的)。

使用严格的断言模式:

const assert = require('assert').strict;

错误差异的示例:

const assert = require('assert').strict;

assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected ... Lines skipped
//
//   [
//     [
// ...
//       2,
// +     3
// -     '3'
//     ],
// ...
//     5
//   ]

若要禁用颜色,则使用 NO_COLORNODE_DISABLE_COLORS 环境变量。 这也会禁用 REPL 中的颜色。 有关终端环境中的颜色支持,详见 tty 的 getColorDepth() 文档。

传统的断言模式#

中英对照提交修改

传统的断言模式在以下方法中使用抽象的相等性比较

使用传统的断言模式:

const assert = require('assert');

只要有可能,请使用严格的断言模式。 否则,抽象的相等性比较可能会导致意外的结果。 特别是对于 assert.deepEqual(),其中的比较规则是松散的:

// 注意:这不会抛出 AssertionError!
assert.deepEqual(/a/gi, new Date());

assert.AssertionError 类#

中英对照提交修改

表明断言的失败。 assert 模块抛出的所有错误都是 AssertionError 类的实例。

new assert.AssertionError(options)#

中英对照提交修改

  • options <Object>

    • message <string> 如果提供,则将错误消息设置为此值。
    • actual <any> 错误实例上的 actual 属性将包含此值。
    • expected <any> 错误实例上的 expected 属性将包含此值。
    • 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');
  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);
}

assert.CallTracker 类#

暂无中英对照

稳定性: 1 - 实验

This feature is currently experimental and behavior might still change.

new assert.CallTracker()#

暂无中英对照提交修改

Creates a new CallTracker object which can be used to track if functions were called a specific number of times. The tracker.verify() must be called for the verification to take place. The usual pattern would be to call it in a process.on('exit') handler.

const assert = require('assert');

const tracker = new assert.CallTracker();

function func() {}

// callsfunc() must be called exactly 1 time before tracker.verify().
const callsfunc = tracker.calls(func, 1);

callsfunc();

// Calls tracker.verify() and verifies if all tracker.calls() functions have
// been called exact times.
process.on('exit', () => {
  tracker.verify();
});

tracker.calls([fn][, exact])#

暂无中英对照提交修改

The wrapper function is expected to be called exactly exact times. If the function has not been called exactly exact times when tracker.verify() is called, then tracker.verify() will throw an error.

const assert = require('assert');

// Creates call tracker.
const tracker = new assert.CallTracker();

function func() {}

// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func);

tracker.report()#

暂无中英对照提交修改

  • Returns: <Array> of objects containing information about the wrapper functions returned by tracker.calls().
  • Object <Object>

    • message <string>
    • actual <number> The actual number of times the function was called.
    • expected <number> The number of times the function was expected to be called.
    • operator <string> The name of the function that is wrapped.
    • stack <Object> A stack trace of the function.

The arrays contains information about the expected and actual number of calls of the functions that have not been called the expected number of times.

const assert = require('assert');

// Creates call tracker.
const tracker = new assert.CallTracker();

function func() {}

function foo() {}

// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);

// Returns an array containing information on callsfunc()
tracker.report();
// [
//  {
//    message: 'Expected the func function to be executed 2 time(s) but was
//    executed 0 time(s).',
//    actual: 0,
//    expected: 2,
//    operator: 'func',
//    stack: stack trace
//  }
// ]

tracker.verify()#

暂无中英对照提交修改

Iterates through the list of functions passed to tracker.calls() and will throw an error for functions that have not been called the expected number of times.

const assert = require('assert');

// Creates call tracker.
const tracker = new assert.CallTracker();

function func() {}

// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);

callsfunc();

// Will throw an error since callsfunc() was only called once.
tracker.verify();

assert(value[, message])#

中英对照提交修改

assert.ok() 的别名。

assert.deepEqual(actual, expected[, message])#

暂无中英对照

Strict assertion mode

An alias of assert.deepStrictEqual().

Legacy assertion mode

稳定性: 0 - 弃用: 改为使用 assert.deepStrictEqual()

Tests for deep equality between the actual and expected parameters. Consider using assert.deepStrictEqual() instead. assert.deepEqual() can have surprising results.

Deep equality means that the enumerable "own" properties of child objects are also recursively evaluated by the following rules.

比较运算的详细说明#

中英对照提交修改

  • NaN 之外,使用抽象的相等性比较==)来比较原始值。如果双方均为 NaN,则视为相同。
  • 对象的类型标签应该相同。
  • 只考虑可枚举的自身属性
  • 始终比较 Error 的名称和消息,即使这些不是可枚举的属性。
  • 对象封装器作为对象和解封装后的值都进行比较。
  • Object 属性的比较是无序的。
  • Map 键名与 Set 子项的比较是无序的。
  • 当两边的值不相同或遇到循环引用时,递归停止。
  • 不测试对象的 [[Prototype]]
  • 可枚举的自身 Symbol 属性也会比较。
  • WeakMapWeakSet 的比较不依赖于它们的值。

以下示例不会抛出 AssertionError,因为抽象的相等性比较==)会将原始类型视为相等。

// 不会抛出 AssertionError。
assert.deepEqual('+00000000', false);

“深度”相等意味着还会比较子对象的可枚举“自有”属性:

const assert = require('assert');

const obj1 = {
  a: {
    b: 1
  }
};
const obj2 = {
  a: {
    b: 2
  }
};
const obj3 = {
  a: {
    b: 1
  }
};
const obj4 = Object.create(obj1);

assert.deepEqual(obj1, obj1);
// OK

// b 的值不同:
assert.deepEqual(obj1, obj2);
// 抛出 AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }

assert.deepEqual(obj1, obj3);
// OK

// 原型会被忽略:
assert.deepEqual(obj1, obj4);
// 抛出 AssertionError: { a: { b: 1 } } deepEqual {}

如果值不相等,则抛出 AssertionError,并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 Error 的实例,那么它将被抛出而不是 AssertionError

assert.deepStrictEqual(actual, expected[, message])#

中英对照提交修改

测试 actual 参数和 expected 参数之间的深度相等。 深度相等意味着子对象的可枚举的自身属性也通过以下规则进行递归计算。

比较运算的详细说明#

中英对照提交修改

const assert = require('assert').strict;

// 失败,因为 1 !== '1'。
assert.deepStrictEqual({ a: 1 }, { a: '1' });
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
//   {
// +   a: 1
// -   a: '1'
//   }

// 以下对象没有自身属性。
const date = new Date();
const object = {};
const fakeDate = {};
Object.setPrototypeOf(fakeDate, Date.prototype);

// 原型不同:
assert.deepStrictEqual(object, fakeDate);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + {}
// - Date {}

// 类型标签不同:
assert.deepStrictEqual(date, fakeDate);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + 2018-04-26T00:49:08.604Z
// - Date {}

assert.deepStrictEqual(NaN, NaN);
// 通过,因为使用 SameValue 比较。

// 解封装后的数字不同:
assert.deepStrictEqual(new Number(1), new Number(2));
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + [Number: 1]
// - [Number: 2]

assert.deepStrictEqual(new String('foo'), Object('foo'));
// 通过,因为对象与解封装后的字符串都是相同的。

assert.deepStrictEqual(-0, -0);
// 通过。

// 使用 SameValue 比较的零不同:
assert.deepStrictEqual(0, -0);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
// + 0
// - -0

const symbol1 = Symbol();
const symbol2 = Symbol();
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
// 通过,因为在两个对象上的 symbol 相同。
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
//
// {
//   [Symbol()]: 1
// }

const weakMap1 = new WeakMap();
const weakMap2 = new WeakMap([[{}, {}]]);
const weakMap3 = new WeakMap();
weakMap3.unequal = true;

assert.deepStrictEqual(weakMap1, weakMap2);
// 通过,因为无法比较条目。

// 失败,因为 weakMap3 有一个 weakMap1 不包含的属性:
assert.deepStrictEqual(weakMap1, weakMap3);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected
//
//   WeakMap {
// +   [items unknown]
// -   [items unknown],
// -   unequal: true
//   }

如果值不相等,则抛出 AssertionError,并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 Error 的实例,那么它将被抛出而不是 AssertionError

assert.doesNotMatch(string, regexp[, message])#

暂无中英对照

稳定性: 1 - 实验

Expects the string input not to match the regular expression.

This feature is currently experimental and the name might change or it might be completely removed again.

const assert = require('assert').strict;

assert.doesNotMatch('I will fail', /fail/);
// AssertionError [ERR_ASSERTION]: The input was expected to not match the ...

assert.doesNotMatch(123, /pass/);
// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.

assert.doesNotMatch('I will pass', /different/);
// OK

If the values do match, or if the string argument is of another type than string, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

assert.doesNotReject(asyncFn[, error][, message])#

中英对照提交修改

等待 asyncFn Promise,或者,如果 asyncFn 是一个函数,则立即调用该函数并等待返回的 Promise 完成。 然后它将检查 Promise 是否被拒绝。

如果 asyncFn 是一个函数并且它同步抛出一个错误,则 assert.doesNotReject() 将返回一个带有该错误的被拒绝的 Promise。 如果函数未返回 Promise,则 assert.doesNotReject() 将返回一个被拒绝的 Promise,其中包含 ERR_INVALID_RETURN_VALUE 错误。 在这两种情况下都会跳过错误处理函数。

使用 assert.doesNotReject() 实际上没有用处,因为捕获拒绝然后再次拒绝它几乎没有什么好处。 应该考虑在不应拒绝的特定代码路径旁边添加注释,并尽可能保留错误消息。

如果指定,则 error 可以是 ClassRegExp 或验证函数。 有关更多详细信息,请参见 assert.throws()

除了等待的异步性质之外,完成行为与 assert.doesNotThrow() 完全相同。

(async () => {
  await assert.doesNotReject(
    async () => {
      throw new TypeError('错误值');
    },
    SyntaxError
  );
})();
assert.doesNotReject(Promise.reject(new TypeError('错误值')))
  .then(() => {
    // ...
  });

assert.doesNotThrow(fn[, error][, message])#

中英对照提交修改

断言 fn 函数不会抛出错误。

使用 assert.doesNotThrow() 实际上没有用处,因为捕获错误然后重新抛出它没有任何好处。 应该考虑在不应抛出错误的特定代码路径旁边添加注释,并尽可能保留错误消息。

当调用 assert.doesNotThrow() 时,它将立即调用 fn 函数。

如果抛出错误并且它与 error 参数指定的类型相同,则抛出 AssertionError。 如果错误的类型不同,或者 error 参数未定义,则错误将传播回调用方。

如果指定,则 error 可以是 ClassRegExp 或验证函数。 有关更多详细信息,请参见 assert.throws()

例如,以下示例将抛出 TypeError,因为断言中没有匹配的错误类型:

assert.doesNotThrow(
  () => {
    throw new TypeError('错误值');
  },
  SyntaxError
);

以下示例将导致 AssertionError,并显示消息 'Got unwanted exception...':

assert.doesNotThrow(
  () => {
    throw new TypeError('错误值');
  },
  TypeError
);

如果抛出 AssertionError 并为 message 参数提供了值,则 message 的值将附加到 AssertionError 消息:

assert.doesNotThrow(
  () => {
    throw new TypeError('错误值');
  },
  /错误值/,
  '出错啦'
);
// AssertionError: Got unwanted exception: 出错啦

assert.equal(actual, expected[, message])#

暂无中英对照

Strict assertion mode

An alias of assert.strictEqual().

Legacy assertion mode

稳定性: 0 - 弃用: 改为使用 assert.strictEqual()

Tests shallow, coercive equality between the actual and expected parameters using the Abstract Equality Comparison ( == ). NaN is special handled and treated as being identical in case both sides are NaN.

const assert = require('assert');

assert.equal(1, 1);
// OK, 1 == 1
assert.equal(1, '1');
// OK, 1 == '1'
assert.equal(NaN, NaN);
// OK

assert.equal(1, 2);
// AssertionError: 1 == 2
assert.equal({ a: { b: 1 } }, { a: { b: 1 } });
// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }

If the values are not equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

assert.fail([message])#

中英对照提交修改

使用提供的错误消息或默认错误消息抛出 AssertionError。 如果 message 参数是 Error 的实例,则它将被抛出而不是 AssertionError

const assert = require('assert').strict;

assert.fail();
// AssertionError [ERR_ASSERTION]: Failed

assert.fail('失败');
// AssertionError [ERR_ASSERTION]: 失败

assert.fail(new TypeError('需要数组'));
// TypeError: 需要数组

使用带有两个以上参数的 assert.fail() 是可能的,但已弃用。 请参见下文了解更多详情。

assert.fail(actual, expected[, message[, operator[, stackStartFn]]])#

暂无中英对照

稳定性: 0 - 弃用: 改为使用 assert.fail([message]) 或其他 assert 函数。

If message is falsy, the error message is set as the values of actual and expected separated by the provided operator. If just the two actual and expected arguments are provided, operator will default to '!='. If message is provided as third argument it will be used as the error message and the other arguments will be stored as properties on the thrown object. If stackStartFn is provided, all stack frames above that function will be removed from stacktrace (see Error.captureStackTrace). If no arguments are given, the default message Failed will be used.

const assert = require('assert').strict;

assert.fail('a', 'b');
// AssertionError [ERR_ASSERTION]: 'a' != 'b'

assert.fail(1, 2, undefined, '>');
// AssertionError [ERR_ASSERTION]: 1 > 2

assert.fail(1, 2, 'fail');
// AssertionError [ERR_ASSERTION]: fail

assert.fail(1, 2, 'whoops', '>');
// AssertionError [ERR_ASSERTION]: whoops

assert.fail(1, 2, new TypeError('need array'));
// TypeError: need array

In the last three cases actual, expected, and operator have no influence on the error message.

Example use of stackStartFn for truncating the exception's stacktrace:

function suppressFrame() {
  assert.fail('a', 'b', undefined, '!==', suppressFrame);
}
suppressFrame();
// AssertionError [ERR_ASSERTION]: 'a' !== 'b'
//     at repl:1:1
//     at ContextifyScript.Script.runInThisContext (vm.js:44:33)
//     ...

assert.ifError(value)#

中英对照提交修改

如果 value 不为 undefinednull,则抛出 value。 在回调中测试 error 参数时,这很有用。 堆栈跟踪包含传递给 ifError() 的错误的所有帧,包括 ifError() 本身的潜在新帧。

const assert = require('assert').strict;

assert.ifError(null);
// 通过。
assert.ifError(0);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError('错误');
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: '错误'
assert.ifError(new Error());
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error

// 创建一些随机错误帧。
let err;
(function errorFrame() {
  err = new Error('测试错误');
})();

(function ifErrorFrame() {
  assert.ifError(err);
})();
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 测试错误
//     at ifErrorFrame
//     at errorFrame

assert.match(string, regexp[, message])#

暂无中英对照

稳定性: 1 - 实验

Expects the string input to match the regular expression.

This feature is currently experimental and the name might change or it might be completely removed again.

const assert = require('assert').strict;

assert.match('I will fail', /pass/);
// AssertionError [ERR_ASSERTION]: The input did not match the regular ...

assert.match(123, /pass/);
// AssertionError [ERR_ASSERTION]: The "string" argument must be of type string.

assert.match('I will pass', /pass/);
// OK

If the values do not match, or if the string argument is of another type than string, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

assert.notDeepEqual(actual, expected[, message])#

暂无中英对照

Strict assertion mode

An alias of assert.notDeepStrictEqual().

Legacy assertion mode

稳定性: 0 - 弃用: 改为使用 assert.notDeepStrictEqual()

Tests for any deep inequality. Opposite of assert.deepEqual().

const assert = require('assert');

const obj1 = {
  a: {
    b: 1
  }
};
const obj2 = {
  a: {
    b: 2
  }
};
const obj3 = {
  a: {
    b: 1
  }
};
const obj4 = Object.create(obj1);

assert.notDeepEqual(obj1, obj1);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }

assert.notDeepEqual(obj1, obj2);
// OK

assert.notDeepEqual(obj1, obj3);
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }

assert.notDeepEqual(obj1, obj4);
// OK

If the values are deeply equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

assert.notDeepStrictEqual(actual, expected[, message])#

中英对照提交修改

测试深度严格的不平等。 与 assert.deepStrictEqual() 相反。

const assert = require('assert').strict;

assert.notDeepStrictEqual({ a: 1 }, { a: '1' });
// 通过。

如果值深度且严格相等,则抛出 AssertionError,并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 Error 的实例,则它将被抛出而不是 AssertionError

assert.notEqual(actual, expected[, message])#

暂无中英对照

Strict assertion mode

An alias of assert.notStrictEqual().

Legacy assertion mode

稳定性: 0 - 弃用: 改为使用 assert.notStrictEqual()

Tests shallow, coercive inequality with the Abstract Equality Comparison (!= ). NaN is special handled and treated as being identical in case both sides are NaN.

const assert = require('assert');

assert.notEqual(1, 2);
// OK

assert.notEqual(1, 1);
// AssertionError: 1 != 1

assert.notEqual(1, '1');
// AssertionError: 1 != '1'

If the values are equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

assert.notStrictEqual(actual, expected[, message])#

中英对照提交修改

测试 actual 参数和 expected 参数之间的严格不相等,使用 SameValue比较

const assert = require('assert').strict;

assert.notStrictEqual(1, 2);
// 通过。

assert.notStrictEqual(1, 1);
// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:
//
// 1

assert.notStrictEqual(1, '1');
// 通过。

如果值严格相等,则抛出 AssertionError,并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 Error 的实例,则它将被抛出而不是 AssertionError

assert.ok(value[, message])#

中英对照提交修改

测试 value 是否为真值。 等同于 assert.equal(!!value, true, message)

如果 value 不是真值,则抛出 AssertionError,并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 Error 的实例,则它将被抛出而不是 AssertionError。 如果没有传入任何参数,则将 message 设置为字符串:'No value argument passed to `assert.ok()`'

注意,在 repl 中,错误消息将与文件中抛出的错误消息不同!请参见下文了解更多详情。

const assert = require('assert').strict;

assert.ok(true);
// OK
assert.ok(1);
// OK

assert.ok();
// AssertionError: No value argument passed to `assert.ok()`

assert.ok(false, '这是假值');
// AssertionError: 这是假值

// 在 repl 中:
assert.ok(typeof 123 === 'string');
// AssertionError: false == true

// 在文件中(例如 test.js):
assert.ok(typeof 123 === 'string');
// AssertionError: The expression evaluated to a falsy value:
//
//   assert.ok(typeof 123 === 'string')

assert.ok(false);
// AssertionError: The expression evaluated to a falsy value:
//
//   assert.ok(false)

assert.ok(0);
// AssertionError: The expression evaluated to a falsy value:
//
//   assert.ok(0)

// 与使用 `assert()` 相同:
assert(0);
// AssertionError: The expression evaluated to a falsy value:
//
//   assert(0)

assert.rejects(asyncFn[, error][, message])#

中英对照提交修改

等待 asyncFn Promise,或者,如果 asyncFn 是一个函数,则立即调用该函数并等待返回的 Promise 完成。 然后它将检查 Promise 是否被拒绝。

如果 asyncFn 是一个函数并且它同步抛出一个错误,则 assert.rejects() 将返回一个带有该错误的被拒绝的 Promise。 如果函数未返回 Promise,则 assert.rejects() 将返回一个被拒绝的 Promise,其中包含 ERR_INVALID_RETURN_VALUE 错误。 在这两种情况下都会跳过错误处理函数。

除了等待的异步性质之外,完成行为与 assert.throws() 完全相同。

如果指定,则 error 可以是 ClassRegExp、验证函数、将测试每个属性的对象、或者将测试每个属性的错误实例(包括不可枚举的 messagename 属性)。

如果指定 message,则当 asyncFn 无法拒绝时 message 将是 AssertionError 提供的消息。

(async () => {
  await assert.rejects(
    async () => {
      throw new TypeError('错误值');
    },
    {
      name: 'TypeError',
      message: '错误值'
    }
  );
})();
(async () => {
  await assert.rejects(
    async () => {
      throw new TypeError('错误值');
    },
    (err) => {
      assert.strictEqual(err.name, 'TypeError');
      assert.strictEqual(err.message, '错误值');
      return true;
    }
  );
})();
assert.rejects(
  Promise.reject(new Error('错误值')),
  Error
).then(() => {
  // ...
});

注意, error 不能是字符串。 如果提供了一个字符串作为第二个参数,则假定 error 被忽略,而字符串将用于 message。 这可能导致容易错过的错误。 如果考虑使用字符串作为第二个参数,请仔细阅读 assert.throws() 中的示例。

assert.strictEqual(actual, expected[, message])#

中英对照提交修改

测试 actual 参数和 expected 参数之间的严格相等性,使用 SameValue比较

const assert = require('assert').strict;

assert.strictEqual(1, 2);
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
//
// 1 !== 2

assert.strictEqual(1, 1);
// OK

assert.strictEqual('Hello foobar', 'Hello World!');
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
// + actual - expected
//
// + 'Hello foobar'
// - 'Hello World!'
//          ^

const apples = 1;
const oranges = 2;
assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2

assert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
// TypeError: Inputs are not identical

如果值不严格相等,则抛出 AssertionError,并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 Error 的实例,则它将被抛出而不是 AssertionError

assert.throws(fn[, error][, message])#

中英对照提交修改

期望 fn 函数抛出错误。

如果指定,则 error 可以是 ClassRegExp、验证函数,每个属性将被测试严格的深度相等的验证对象、或每个属性(包括不可枚举的 messagename 属性)将被测试严格的深度相等的错误实例。 使用对象时,还可以在对字符串属性进行验证时使用正则表达式。 请参见下面的示例。

如果指定 message,则当 fn 调用无法抛出或错误验证失败时, message 将附加到 AssertionError 提供的消息。

自定义的验证对象/错误实例:

const err = new TypeError('错误值');
err.code = 404;
err.foo = 'bar';
err.info = {
  nested: true,
  baz: 'text'
};
err.reg = /abc/i;

assert.throws(
  () => {
    throw err;
  },
  {
    name: 'TypeError',
    message: '错误值',
    info: {
      nested: true,
      baz: 'text'
    }
    // 仅测试验证对象上的属性。
    // 使用嵌套对象需要存在所有属性。
    // 否则验证将失败。
  }
);

// 使用正则表达式验证错误属性:
assert.throws(
  () => {
    throw err;
  },
  {
    // `name` 和 `message` 属性是字符串,使用正则表达式将匹配字符串。 
    // 如果失败,则会抛出错误。
    name: /^TypeError$/,
    message: /错误/,
    foo: 'bar',
    info: {
      nested: true,
      // 无法对嵌套属性使用正则表达式!
      baz: 'text'
    },
    // `reg` 属性包含一个正则表达式,
    // 并且只有当验证对象包含相同的正则表达式时,
    // 它才会通过。
    reg: /abc/i
  }
);

// 由于 `message` 和 `name` 属性不同而失败:
assert.throws(
  () => {
    const otherErr = new Error('未找到');
    // 将所有可枚举的属性从 `err` 拷贝到 `otherErr`。
    for (const [key, value] of Object.entries(err)) {
      otherErr[key] = value;
    }
    throw otherErr;
  },
  // 当使用错误作为验证对象时,也会检查错误的 `message`  和 `name` 属性。
  err
);

使用构造函数验证 instanceof:

assert.throws(
  () => {
    throw new Error('错误值');
  },
  Error
);

使用 RegExp 验证错误消息:

使用正则表达式在错误对象上运行 .toString,因此也将包含错误名称。

assert.throws(
  () => {
    throw new Error('错误值');
  },
  /^Error: 错误值$/
);

自定义的错误验证函数:

该函数必须返回 true,以表明已通过所有的内部验证。 否则它将会失败并带上 AssertionError

assert.throws(
  () => {
    throw new Error('错误值');
  },
  (err) => {
    assert(err instanceof Error);
    assert(/value/.test(err));
    // 避免从验证函数返回 `true` 以外的任何东西。 
    // 否则,会不清楚验证的哪一部分失败。 
    // 应该抛出有关失败的特定验证的错误(如本例所示),并向该错误添加尽可能多的有用的调试信息。
    return true;
  },
  '不是期望的错误'
);

error 不能是字符串。 如果提供了一个字符串作为第二个参数,则假定 error 被忽略,而字符串将用于 message。 这可能导致容易错过的错误。 使用与抛出的错误消息相同的消息将导致 ERR_AMBIGUOUS_ARGUMENT 错误。 如果使用字符串作为第二个参数,请仔细阅读下面的示例:

function throwingFirst() {
  throw new Error('错误一');
}

function throwingSecond() {
  throw new Error('错误二');
}

function notThrowing() {}

// 第二个参数是一个字符串,输入函数抛出一个错误。
// 第一种情况不会抛出,因为它与输入函数抛出的错误消息不匹配!
assert.throws(throwingFirst, '错误二');
// 在下一个示例中,传入的消息类似来自错误的消息,
// 并且由于不清楚用户是否打算实际匹配错误消息,
// 因此 Node.js 抛出了 `ERR_AMBIGUOUS_ARGUMENT` 错误。
assert.throws(throwingSecond, '错误二');
// TypeError [ERR_AMBIGUOUS_ARGUMENT]

// 该字符串仅在函数未抛出时使用(作为消息):
assert.throws(notThrowing, '错误二');
// AssertionError [ERR_ASSERTION]: Missing expected exception: 错误二

// 如果要匹配错误消息,请执行以下操作:
// 它不会抛出错误,因为错误消息匹配。
assert.throws(throwingSecond, /错误二$/);

// 如果错误消息不匹配,则抛出 AssertionError。
assert.throws(throwingFirst, /错误二$/);
// AssertionError [ERR_ASSERTION]

由于令人困惑的表示法,建议不要使用字符串作为第二个参数。 这可能会导致难以发现的错误。