context.assert.snapshot(value[, options])
-
value
<any> 要序列化为字符串的值。如果 Node.js 是使用--test-update-snapshots
标志启动的,则序列化值将写入快照文件。否则,序列化值将与现有快照文件中的相应值进行比较。¥
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 the snapshot file. Otherwise, the serialized value is compared to the corresponding value in the existing snapshot file. -
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 serializevalue
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.
-
此函数实现快照测试的断言。
¥This function implements assertions for snapshot testing.
test('snapshot test with default serialization', (t) => {
t.assert.snapshot({ value1: 1, value2: 2 });
});
test('snapshot test with custom serialization', (t) => {
t.assert.snapshot({ value3: 3, value4: 4 }, {
serializers: [(value) => JSON.stringify(value)],
});
});