严格的断言模式
在严格的断言模式中,非严格方法的行为类似于其对应的严格方法。
例如,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_COLOR
或 NODE_DISABLE_COLORS
环境变量。
这也会禁用 REPL 中的颜色。
有关终端环境中的颜色支持,详见 tty 的 getColorDepth() 文档。
In strict assertion mode, non-strict methods behave like their corresponding
strict methods. For example, assert.deepEqual()
will behave like
assert.deepStrictEqual()
.
In strict assertion mode, error messages for objects display a diff. In legacy assertion mode, error messages for objects display the objects, often truncated.
To use strict assertion mode:
const assert = require('assert').strict;
Example error diff:
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
// ]
To deactivate the colors, use the NO_COLOR
or NODE_DISABLE_COLORS
environment variables. This will also deactivate the colors in the REPL. For
more on color support in terminal environments, read the tty
getColorDepth() documentation.