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


  • value <any> 要序列化为字符串的值。如果 Node.js 是使用 --test-update-snapshots 标志启动的,序列化后的值将写入 path。否则,序列化后的值将与现有快照文件的内容进行比较。
  • path <string> 写入序列化 value 的文件。
  • options <Object> 可选配置选项。支持以下属性:
    • serializers <Array> 一个同步函数数组,用于将 value 序列化为字符串。value 作为唯一参数传递给第一个序列化函数。每个序列化函数的返回值作为输入传递给下一个序列化函数。一旦所有序列化函数运行完毕,得到的值将被强制转换为字符串。默认值: 如果未提供序列化函数,将使用测试运行器的默认序列化函数。

此函数将 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:】

  • 快照文件路径由用户明确提供。
  • 每个快照文件仅限于一个快照值。
  • 测试运行器不会执行额外的转义。

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

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