@@ -4,10 +4,10 @@ title: 测试快照 | 指南
44
55# 测试快照 {#snapshot}
66
7- 当你希望确保函数的输出不会意外更改时,快照测试是一个非常有用的工具。
8-
97<CourseLink href =" https://vueschool.io/lessons/snapshots-in-vitest?friend=vueuse " >通过 Vue School 的视频学习快照</CourseLink >
108
9+ 当你希望确保函数的输出不会意外更改时,快照测试是一个非常有用的工具。
10+
1111使用快照时,Vitest 将获取给定值的快照,将其比较时将参考存储在测试旁边的快照文件。如果两个快照不匹配,则测试将失败:要么更改是意外的,要么参考快照需要更新到测试结果的新版本。
1212
1313## 使用快照 {#use-snapshots}
@@ -39,7 +39,7 @@ exports['toUpperCase 1'] = '"FOOBAR"'
3939在异步并发测试中使用快照时,由于 JavaScript 的限制,你需要使用 [ 测试环境] ( /guide/test-context ) 中的 ` expect ` 来确保检测到正确的测试。
4040:::
4141
42- 如同前文 ,你可以使用 [ ` toMatchInlineSnapshot() ` ] ( /api/#tomatchinlinesnapshot ) 将内联快照存储在测试文件中。
42+ 同样 ,你可以使用 [ ` toMatchInlineSnapshot() ` ] ( /api/#tomatchinlinesnapshot ) 将内联快照存储在测试文件中。
4343
4444``` ts
4545import { expect , it } from ' vitest'
@@ -63,12 +63,12 @@ it('toUpperCase', () => {
6363
6464这允许你直接查看期望输出,而无需跨不同的文件跳转。
6565
66- ## 更新快照 {#updating-snapshots}
67-
6866::: warning
69- 在异步并发测试中使用快照时,由于 JavaScript 的限制,你需要使用 [ 测试环境 ] ( /guide/test-context ) 中的 ` expect ` 来确保检测到正确的测试 。
67+ 在异步并发测试中使用快照时,必须使用本地的 [ Test Context ] ( /guide/test-context ) 中的 ` expect ` 方法,以确保正确检测到对应的测试 。
7068:::
7169
70+ ## 更新快照 {#updating-snapshots}
71+
7272当接收到的值与快照不匹配时,测试将失败,并显示它们之间的差异。当需要更改快照时,你可能希望从当前状态更新快照。
7373
7474在监听(watch)模式下, 你可以在终端中键入 ` u ` 键直接更新失败的快照。
@@ -81,7 +81,7 @@ vitest -u
8181
8282## 文件快照 {#file-snapshots}
8383
84- 调用 ` toMatchSnapshot() ` 时,我们将所有快照存储在格式化的快照文件中。这意味着我们需要转义快照字符串中的一些字符(即双引号 ` " ` 和反引号 `\ ` `)。同时,你可能会丢失快照内容的语法突出显示(如果它们是某种语言)。
84+ 调用 ` toMatchSnapshot() ` 时,我们将所有快照存储在格式化的快照文件中。这意味着我们需要转义快照字符串中的一些字符(即双引号 ` " ` 和反引号 `` ` `` )。同时,你可能会丢失快照内容的语法突出显示(如果它们是某种语言)。
8585
8686为了改善这种情况,我们引入 [ ` toMatchFileSnapshot() ` ] ( /api/expect#tomatchfilesnapshot ) 以在文件中显式快照。这允许你为快照文件分配任何文件扩展名,并使它们更具可读性。
8787
@@ -96,41 +96,46 @@ it('render basic', async () => {
9696
9797它将与 ` ./test/basic.output.html ` 的内容进行比较。并且可以用 ` --update ` 标志写回。
9898
99- ## 图像快照 {#visual -snapshots}
99+ ## 图像快照 {#image -snapshots}
100100
101- 对于 UI 组件和页面的视觉回归测试,Vitest 通过 [ 浏览器模式 ] ( /guide/browser/ ) 提供了内置支持,使用 [ ` toMatchScreenshot() ` ] ( /guide/browser/assertion-api#tomatchscreenshot-experimental ) 断言:
101+ 快照图像也可以使用 [ ` jest-image-snapshot ` ] ( https://github.com/americanexpress/jest-image-snapshot ) 。
102102
103- ``` ts
104- import { expect , test } from ' vitest '
105- import { page } from ' vitest/browser '
103+ ``` bash
104+ npm i -D jest-image-snapshot
105+ ```
106106
107- test (' button looks correct' , async () => {
108- const button = page .getByRole (' button' )
109- await expect (button ).toMatchScreenshot (' primary-button' )
107+ ``` ts
108+ test (' image snapshot' , () => {
109+ expect (readFileSync (' ./test/stubs/input-image.png' ))
110+ .toMatchImageSnapshot ()
110111})
111112```
112113
113- 它会捕获屏幕截图并与参考图像进行比较,以检测意外的视觉变化。在[ 视觉回归测试指南] ( /guide/browser/visual-regression-testing ) 中了解更多内容。
114-
115114## 自定义序列化程序 {#custom-serializer}
116115
117- 你可以添加自己的逻辑来修改快照的序列化方式。像 Jest 一样,Vitest 为内置的 JavaScript 类型、HTML 元素、ImmutableJS 和 React 元素提供了默认的序列化程序。
116+ 你可以添加自己的逻辑来修改快照的序列化方式。像 Jest 一样,Vitest 默认有内置的 JavaScript 类型、HTML 元素、ImmutableJS 和 React 元素提供了默认的序列化程序。
118117
119118可以使用 [ ` expect.addSnapshotSerializer ` ] ( /api/expect#expect-addsnapshotserializer ) 添加自定义序列器。
120119
121120``` ts
122121expect .addSnapshotSerializer ({
123122 serialize(val , config , indentation , depth , refs , printer ) {
124- // `printer` is a function that serializes a value using existing plugins.
125- return ` Pretty foo: ${printer (val .foo , config , indentation , depth , refs )} `
123+ // `printer` 是一个通过现有插件对值进行序列化的函数。
124+ return ` Pretty foo: ${printer (
125+ val .foo ,
126+ config ,
127+ indentation ,
128+ depth ,
129+ refs
130+ )} `
126131 },
127132 test(val ) {
128133 return val && Object .prototype .hasOwnProperty .call (val , ' foo' )
129134 },
130135})
131136```
132137
133- 我们还支持 [ snapshotSerializers] ( /config/#snapshotserializers ) 选项来隐式添加自定义序列化器 。
138+ 我们还支持 [ snapshotSerializers] ( /config/#snapshotserializers ) 选项,可以隐式添加自定义序列化器 。
134139
135140``` ts [path/to/custom-serializer.ts]
136141import { SnapshotSerializer } from ' vitest'
@@ -156,7 +161,7 @@ export default defineConfig({
156161})
157162```
158163
159- 如下所示的测试添加后 :
164+ 添加类似的测试后 :
160165
161166``` ts
162167test (' foo snapshot test' , () => {
0 commit comments