严格断言模式
\Strict assertion mode
版本历史
| 版本 | 变更 |
|---|---|
| v15.0.0 | 暴露为 |
| v13.9.0, v12.16.2 | 将 "严格模式" 更改为 "严格断言模式",将 "旧版模式" 更改为 "旧版断言模式",以避免与 "严格模式" 的更常见含义混淆。 |
| v9.9.0 | 添加错误差异到严格断言模式。 |
| v9.9.0 | 添加严格断言模式到断言模块。 |
| v9.9.0 | 新增于: v9.9.0 |
在严格断言模式下,非严格方法的行为与其对应的严格方法相同。例如,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 环境变量。这也将停用交互式解释器中的颜色。有关终端环境中颜色支持的更多信息,请读阅终端 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.