严格断言模式
【Strict assertion mode】
在严格断言模式下,非严格方法的行为就像它们对应的严格方法。例如,assert.deepEqual() 的行为将像 assert.deepStrictEqual() 一样。
【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:】
import { strict as assert } from 'node:assert';const assert = require('node:assert').strict;import assert from 'node:assert/strict';const assert = require('node:assert/strict');错误差异的示例:
【Example error diff:】
import { strict as assert } from 'node:assert';
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
// ]const assert = require('node: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() 文档。
【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.】