Skip to content

Commit b2d4fb0

Browse files
authored
Merge pull request #952 from vitest-dev/sync-7bcfba99-1
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 7bcfba9
2 parents 5a7b5b0 + 70440c7 commit b2d4fb0

4 files changed

Lines changed: 105 additions & 1 deletion

File tree

guide/extending-matchers.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ function customMatcher(this: MatcherState, received: unknown, arg1: unknown, arg
107107

108108
expect.extend({ customMatcher })
109109
```
110+
<!-- TODO: translation -->
111+
::: tip
112+
To build custom **snapshot matchers** (wrappers around `toMatchSnapshot` / `toMatchInlineSnapshot` / `toMatchFileSnapshot`), use the composable functions from `vitest/runtime`. See [Custom Snapshot Matchers](/guide/snapshot#custom-snapshot-matchers).
113+
:::
110114

111115
断言方法可以访问上下文 `this` 对象中的这些属性:
112116

guide/migration.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,36 @@ export default defineConfig({
640640
```
641641

642642
否则快照中会出现大量转义的 `"` 字符。
643+
<!-- TODO: translation -->
644+
### Custom Snapshot Matchers <Badge type="warning">experimental</Badge> <Version>4.1.3</Version>
645+
646+
Jest imports snapshot composables from `jest-snapshot`. In Vitest, import from `vitest/runtime` instead:
647+
648+
```ts
649+
const { toMatchSnapshot } = require('jest-snapshot') // [!code --]
650+
import { toMatchSnapshot } from 'vitest/runtime' // [!code ++]
651+
652+
expect.extend({
653+
toMatchTrimmedSnapshot(received: string, length: number) {
654+
return toMatchSnapshot.call(this, received.slice(0, length))
655+
},
656+
})
657+
```
658+
659+
For inline snapshots, the same applies:
660+
661+
```ts
662+
const { toMatchInlineSnapshot } = require('jest-snapshot') // [!code --]
663+
import { toMatchInlineSnapshot } from 'vitest/runtime' // [!code ++]
664+
665+
expect.extend({
666+
toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) {
667+
return toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot)
668+
},
669+
})
670+
```
671+
672+
See [Custom Snapshot Matchers](/guide/snapshot#custom-snapshot-matchers) for the full guide.
643673

644674
## 从 Mocha + Chai + Sinon 迁移 {#mocha-chai-sinon}
645675

guide/snapshot.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,76 @@ Pretty foo: Object {
192192
```
193193

194194
我们使用的是 Jest 的 `pretty-format` 来序列化快照。你可以在这里阅读更多相关内容:[pretty-format](https://github.com/facebook/jest/blob/main/packages/pretty-format/README.md#serialize).
195+
<!-- TODO: translation -->
196+
## Custom Snapshot Matchers <Badge type="warning">experimental</Badge> <Version>4.1.3</Version> {#custom-snapshot-matchers}
197+
198+
You can build custom snapshot matchers using the composable functions exported from `vitest/runtime`. These let you transform values before snapshotting while preserving full snapshot lifecycle support (creation, update, inline rewriting).
199+
200+
```ts
201+
import { expect, test } from 'vitest'
202+
import { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } from 'vitest/runtime'
203+
204+
expect.extend({
205+
toMatchTrimmedSnapshot(received: string, length: number) {
206+
return toMatchSnapshot.call(this, received.slice(0, length))
207+
},
208+
toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) {
209+
return toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot)
210+
},
211+
async toMatchTrimmedFileSnapshot(received: string, file: string) {
212+
return toMatchFileSnapshot.call(this, received.slice(0, 10), file)
213+
},
214+
})
215+
216+
test('file snapshot', () => {
217+
expect('extra long string oh my gerd').toMatchTrimmedSnapshot(10)
218+
})
219+
220+
test('inline snapshot', () => {
221+
expect('extra long string oh my gerd').toMatchTrimmedInlineSnapshot()
222+
})
223+
224+
test('raw file snapshot', async () => {
225+
await expect('extra long string oh my gerd').toMatchTrimmedFileSnapshot('./raw-file.txt')
226+
})
227+
```
228+
229+
The composables return `{ pass, message }` so you can further customize the error:
230+
231+
```ts
232+
expect.extend({
233+
toMatchTrimmedSnapshot(received: string, length: number) {
234+
const result = toMatchSnapshot.call(this, received.slice(0, length))
235+
return { ...result, message: () => `Trimmed snapshot failed: ${result.message()}` }
236+
},
237+
})
238+
```
239+
240+
::: warning
241+
For inline snapshot matchers, the snapshot argument must be the last parameter (or second-to-last when using property matchers). Vitest rewrites the last string argument in the source code, so custom arguments before the snapshot work, but custom arguments after it are not supported.
242+
:::
243+
244+
::: tip
245+
File snapshot matchers must be `async``toMatchFileSnapshot` returns a `Promise`. Remember to `await` the result in the matcher and in your test.
246+
:::
247+
248+
For TypeScript, extend the `Assertion` interface:
249+
250+
```ts
251+
import 'vitest'
252+
253+
declare module 'vitest' {
254+
interface Assertion<T = any> {
255+
toMatchTrimmedSnapshot: (length: number) => T
256+
toMatchTrimmedInlineSnapshot: (inlineSnapshot?: string) => T
257+
toMatchTrimmedFileSnapshot: (file: string) => Promise<T>
258+
}
259+
}
260+
```
261+
262+
::: tip
263+
See [Extending Matchers](/guide/extending-matchers) for more on `expect.extend` and custom matcher conventions.
264+
:::
195265

196266
## 与 Jest 的区别 {#difference-from-jest}
197267

guide/test-context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ This applies to all suite-level hooks: `beforeAll`, `afterAll`, and `aroundAll`.
894894
:::
895895

896896
::: tip
897-
Suite-level hooks can only access [**file-scoped** and **worker-scoped** fixtures](#fixture-scopes). Test-scoped fixtures are not available in these hooks because they run outside the context of individual tests. If you try to access a test-scoped fixture in a suite-level hook, Vitest will throw an error.
897+
Suite-level hooks can only access [**file-scoped** and **worker-scoped** fixtures](#fixture-scopes), including `auto` fixtures. Test-scoped fixtures are not available in these hooks because they run outside the context of individual tests. If you try to access a test-scoped fixture in a suite-level hook, Vitest will throw an error.
898898

899899
```ts
900900
const test = baseTest

0 commit comments

Comments
 (0)