context.assert.fileSnapshot(value, path[, options])


  • value <any> 要序列化为字符串的值。如果 Node.js 以 --test-update-snapshots 标志启动,则序列化值将写入 path。否则,将序列化值与现有快照文件的内容进行比较。

    ¥value <any> A value to serialize to a string. If Node.js was started with the --test-update-snapshots flag, the serialized value is written to path. Otherwise, the serialized value is compared to the contents of the existing snapshot file.

  • path <string> 序列化 value 写入的文件。

    ¥path <string> The file where the serialized value is written.

  • options <Object> 可选配置选项。支持以下属性:

    ¥options <Object> Optional configuration options. The following properties are supported:

    • serializers <Array> 用于将 value 序列化为字符串的同步函数数组。value 作为第一个序列化器函数的唯一参数传递。每个序列化器的返回值作为输入传递给下一个序列化器。一旦所有序列化器都运行完毕,结果值就会被强制转换为字符串。默认值:如果没有提供序列化器,则使用测试运行器的默认序列化器。

      ¥serializers <Array> An array of synchronous functions used to serialize value into a string. value is passed as the only argument to the first serializer function. The return value of each serializer is passed as input to the next serializer. Once all serializers have run, the resulting value is coerced to a string. Default: If no serializers are provided, the test runner's default serializers are used.

此函数序列化 value 并将其写入 path 指定的文件。

¥This function serializes value and writes it to the file specified by path.

test('snapshot test with default serialization', (t) => {
  t.assert.fileSnapshot({ value1: 1, value2: 2 }, './snapshots/snapshot.json');
}); 

此函数与 context.assert.snapshot() 有以下不同:

¥This function differs from context.assert.snapshot() in the following ways:

  • 快照文件路径由用户明确提供。

    ¥The snapshot file path is explicitly provided by the user.

  • 每个快照文件仅限于一个快照值。

    ¥Each snapshot file is limited to a single snapshot value.

  • 测试运行器不执行任何额外的转义。

    ¥No additional escaping is performed by the test runner.

这些差异使快照文件能够更好地支持语法高亮等功能。

¥These differences allow snapshot files to better support features such as syntax highlighting.