Skip to content

Commit 82339b6

Browse files
hi-ogawacodex
andauthored
fix(snapshot): export custom snapshot matcher helper from vitest (#10042)
Co-authored-by: Codex <noreply@openai.com>
1 parent 5360ec1 commit 82339b6

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

guide/extending-matchers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ expect.extend({ customMatcher })
108108
```
109109

110110
::: tip
111-
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).
111+
To build custom **snapshot matchers** (wrappers around `toMatchSnapshot()` / `toMatchInlineSnapshot()` / `toMatchFileSnapshot()`), use `Snapshots` exported from `vitest`. See [Custom Snapshot Matchers](/guide/snapshot#custom-snapshot-matchers).
112112
:::
113113

114114
Matcher function has access to `this` context with the following properties:

guide/migration.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,12 @@ Otherwise your snapshots will have a lot of escaped `"` characters.
652652

653653
### Custom Snapshot Matchers <Badge type="warning">experimental</Badge> <Version>4.1.3</Version>
654654

655-
Jest imports snapshot composables from `jest-snapshot`. In Vitest, import from `vitest/runtime` instead:
655+
Jest imports snapshot composables from `jest-snapshot`. In Vitest, use `Snapshots` from `vitest` instead:
656656

657657
```ts
658658
const { toMatchSnapshot } = require('jest-snapshot') // [!code --]
659-
import { toMatchSnapshot } from 'vitest/runtime' // [!code ++]
659+
import { Snapshots } from 'vitest' // [!code ++]
660+
const { toMatchSnapshot } = Snapshots // [!code ++]
660661

661662
expect.extend({
662663
toMatchTrimmedSnapshot(received: string, length: number) {
@@ -669,7 +670,8 @@ For inline snapshots, the same applies:
669670

670671
```ts
671672
const { toMatchInlineSnapshot } = require('jest-snapshot') // [!code --]
672-
import { toMatchInlineSnapshot } from 'vitest/runtime' // [!code ++]
673+
import { Snapshots } from 'vitest' // [!code ++]
674+
const { toMatchInlineSnapshot } = Snapshots // [!code ++]
673675

674676
expect.extend({
675677
toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) {

guide/snapshot.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ Pretty foo: Object {
202202

203203
## Custom Snapshot Matchers <Badge type="warning">experimental</Badge> <Version>4.1.3</Version> {#custom-snapshot-matchers}
204204

205-
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).
205+
You can build custom snapshot matchers using the composable functions exposed on `Snapshots` from `vitest`. These let you transform values before snapshotting while preserving full snapshot lifecycle support (creation, update, inline rewriting).
206206

207207
```ts
208-
import { expect, test } from 'vitest'
209-
import { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } from 'vitest/runtime'
208+
import { expect, test, Snapshots } from 'vitest'
209+
210+
const { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } = Snapshots
210211

211212
expect.extend({
212213
toMatchTrimmedSnapshot(received: string, length: number) {
@@ -236,6 +237,10 @@ test('raw file snapshot', async () => {
236237
The composables return `{ pass, message }` so you can further customize the error:
237238

238239
```ts
240+
import { Snapshots } from 'vitest'
241+
242+
const { toMatchSnapshot } = Snapshots
243+
239244
expect.extend({
240245
toMatchTrimmedSnapshot(received: string, length: number) {
241246
const result = toMatchSnapshot.call(this, received.slice(0, length))

0 commit comments

Comments
 (0)