assert.snapshot(value, name)


稳定性: 1 - 实验

从文件中读取 name 快照并将 value 与快照进行比较。 valueutil.inspect() 序列化。 如果该值不严格等于快照,则 assert.snapshot() 返回一个使用 AssertionError 拒绝的 Promise

快照文件名使用与应用程序的主入口点相同的基本名称,扩展名为 .snapshot。 如果快照文件不存在,则创建它。 --update-assert-snapshot 命令行标志可用于强制更新现有快照。

import assert from 'node:assert/strict';

// Assuming that the application's main entrypoint is app.mjs, this reads the
// 'snapshotName' snapshot from app.snapshot and strictly compares its value
// to `util.inspect('value')`.
await assert.snapshot('value', 'snapshotName');const assert = require('node:assert/strict');

(async () => {
  // Assuming that the application's main entrypoint is app.js, this reads the
  // 'snapshotName' snapshot from app.snapshot and strictly compares its value
  // to `util.inspect('value')`.
  await assert.snapshot('value', 'snapshotName');
})();

Stability: 1 - Experimental

Reads the name snapshot from a file and compares value to the snapshot. value is serialized with util.inspect(). If the value is not strictly equal to the snapshot, assert.snapshot() returns a rejected Promise with an AssertionError.

The snapshot filename uses the same basename as the application's main entrypoint with a .snapshot extension. If the snapshot file does not exist, it is created. The --update-assert-snapshot command line flag can be used to force the update of an existing snapshot.

import assert from 'node:assert/strict';

// Assuming that the application's main entrypoint is app.mjs, this reads the
// 'snapshotName' snapshot from app.snapshot and strictly compares its value
// to `util.inspect('value')`.
await assert.snapshot('value', 'snapshotName');const assert = require('node:assert/strict');

(async () => {
  // Assuming that the application's main entrypoint is app.js, this reads the
  // 'snapshotName' snapshot from app.snapshot and strictly compares its value
  // to `util.inspect('value')`.
  await assert.snapshot('value', 'snapshotName');
})();