diff --git a/docs/guide/extending-matchers.md b/docs/guide/extending-matchers.md index 2d634c22b..bc6255d3a 100644 --- a/docs/guide/extending-matchers.md +++ b/docs/guide/extending-matchers.md @@ -108,7 +108,7 @@ expect.extend({ customMatcher }) ``` ::: tip -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). +To build custom **snapshot matchers** (wrappers around `toMatchSnapshot()` / `toMatchInlineSnapshot()` / `toMatchFileSnapshot()`), use `Snapshots` exported from `vitest`. See [Custom Snapshot Matchers](/guide/snapshot#custom-snapshot-matchers). ::: Matcher function has access to `this` context with the following properties: diff --git a/docs/guide/migration.md b/docs/guide/migration.md index 952da819b..82963564b 100644 --- a/docs/guide/migration.md +++ b/docs/guide/migration.md @@ -652,11 +652,12 @@ Otherwise your snapshots will have a lot of escaped `"` characters. ### Custom Snapshot Matchers experimental 4.1.3 -Jest imports snapshot composables from `jest-snapshot`. In Vitest, import from `vitest/runtime` instead: +Jest imports snapshot composables from `jest-snapshot`. In Vitest, use `Snapshots` from `vitest` instead: ```ts const { toMatchSnapshot } = require('jest-snapshot') // [!code --] -import { toMatchSnapshot } from 'vitest/runtime' // [!code ++] +import { Snapshots } from 'vitest' // [!code ++] +const { toMatchSnapshot } = Snapshots // [!code ++] expect.extend({ toMatchTrimmedSnapshot(received: string, length: number) { @@ -669,7 +670,8 @@ For inline snapshots, the same applies: ```ts const { toMatchInlineSnapshot } = require('jest-snapshot') // [!code --] -import { toMatchInlineSnapshot } from 'vitest/runtime' // [!code ++] +import { Snapshots } from 'vitest' // [!code ++] +const { toMatchInlineSnapshot } = Snapshots // [!code ++] expect.extend({ toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) { diff --git a/docs/guide/snapshot.md b/docs/guide/snapshot.md index eb115a513..f9156e0e0 100644 --- a/docs/guide/snapshot.md +++ b/docs/guide/snapshot.md @@ -202,11 +202,12 @@ Pretty foo: Object { ## Custom Snapshot Matchers experimental 4.1.3 {#custom-snapshot-matchers} -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). +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). ```ts -import { expect, test } from 'vitest' -import { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } from 'vitest/runtime' +import { expect, test, Snapshots } from 'vitest' + +const { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } = Snapshots expect.extend({ toMatchTrimmedSnapshot(received: string, length: number) { @@ -236,6 +237,10 @@ test('raw file snapshot', async () => { The composables return `{ pass, message }` so you can further customize the error: ```ts +import { Snapshots } from 'vitest' + +const { toMatchSnapshot } = Snapshots + expect.extend({ toMatchTrimmedSnapshot(received: string, length: number) { const result = toMatchSnapshot.call(this, received.slice(0, length)) diff --git a/packages/vitest/src/integrations/snapshot/chai.ts b/packages/vitest/src/integrations/snapshot/chai.ts index 67946107f..5cffb7fdd 100644 --- a/packages/vitest/src/integrations/snapshot/chai.ts +++ b/packages/vitest/src/integrations/snapshot/chai.ts @@ -1,4 +1,4 @@ -import type { ChaiPlugin, MatcherState, SyncExpectationResult } from '@vitest/expect' +import type { AsyncExpectationResult, ChaiPlugin, MatcherState, SyncExpectationResult } from '@vitest/expect' import type { Test } from '@vitest/runner' import { chai, createAssertionMessage, equals, iterableEquality, recordAsyncExpect, subsetEquality, wrapAssertion } from '@vitest/expect' import { getNames } from '@vitest/runner/utils' @@ -268,100 +268,109 @@ function assertMatchResult(result: SyncExpectationResult): void { } /** - * Composable for building custom snapshot matchers via `expect.extend`. - * Call with `this` bound to the matcher state. Returns `{ pass, message }` - * compatible with the custom matcher return contract. - * - * @example - * ```ts - * import { toMatchSnapshot } from 'vitest/runtime' - * - * expect.extend({ - * toMatchTrimmedSnapshot(received: string) { - * return toMatchSnapshot.call(this, received.slice(0, 10)) - * }, - * }) - * ``` + * Composable snapshot matcher helpers for building custom snapshot matchers + * with `expect.extend`. * * @experimental * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers */ -export function toMatchSnapshot( - this: MatcherState, - received: unknown, - propertiesOrHint?: object | string, - hint?: string, -): SyncExpectationResult { - return toMatchSnapshotImpl({ - assertion: this.__vitest_assertion__, - received, - ...normalizeArguments(propertiesOrHint, hint), - }) -} +export const Snapshots = { + /** + * Composable for building custom snapshot matchers via `expect.extend`. + * Call with `this` bound to the matcher state. Returns `{ pass, message }` + * compatible with the custom matcher return contract. + * + * @example + * ```ts + * import { Snapshots } from 'vitest/runtime' + * + * expect.extend({ + * toMatchTrimmedSnapshot(received: string) { + * return Snapshots.toMatchSnapshot.call(this, received.slice(0, 10)) + * }, + * }) + * ``` + * + * @experimental + * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers + */ + toMatchSnapshot( + this: MatcherState, + received: unknown, + propertiesOrHint?: object | string, + hint?: string, + ): SyncExpectationResult { + return toMatchSnapshotImpl({ + assertion: this.__vitest_assertion__, + received, + ...normalizeArguments(propertiesOrHint, hint), + }) + }, -/** - * Composable for building custom inline snapshot matchers via `expect.extend`. - * Call with `this` bound to the matcher state. Returns `{ pass, message }` - * compatible with the custom matcher return contract. - * - * @example - * ```ts - * import { toMatchInlineSnapshot } from 'vitest/runtime' - * - * expect.extend({ - * toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) { - * return toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot) - * }, - * }) - * ``` - * - * @experimental - * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers - */ -export function toMatchInlineSnapshot( - this: MatcherState, - received: unknown, - propertiesOrInlineSnapshot?: object | string, - inlineSnapshotOrHint?: string, - hint?: string, -): SyncExpectationResult { - return toMatchSnapshotImpl({ - assertion: this.__vitest_assertion__, - received, - isInline: true, - ...normalizeInlineArguments(propertiesOrInlineSnapshot, inlineSnapshotOrHint, hint), - }) -} + /** + * Composable for building custom inline snapshot matchers via `expect.extend`. + * Call with `this` bound to the matcher state. Returns `{ pass, message }` + * compatible with the custom matcher return contract. + * + * @example + * ```ts + * import { Snapshots } from 'vitest/runtime' + * + * expect.extend({ + * toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) { + * return Snapshots.toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot) + * }, + * }) + * ``` + * + * @experimental + * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers + */ + toMatchInlineSnapshot( + this: MatcherState, + received: unknown, + propertiesOrInlineSnapshot?: object | string, + inlineSnapshotOrHint?: string, + hint?: string, + ): SyncExpectationResult { + return toMatchSnapshotImpl({ + assertion: this.__vitest_assertion__, + received, + isInline: true, + ...normalizeInlineArguments(propertiesOrInlineSnapshot, inlineSnapshotOrHint, hint), + }) + }, -/** - * Composable for building custom file snapshot matchers via `expect.extend`. - * Call with `this` bound to the matcher state. Returns a `Promise<{ pass, message }>` - * compatible with the custom matcher return contract. - * - * @example - * ```ts - * import { toMatchFileSnapshot } from 'vitest/runtime' - * - * expect.extend({ - * async toMatchTrimmedFileSnapshot(received: string, file: string) { - * return toMatchFileSnapshot.call(this, received.slice(0, 10), file) - * }, - * }) - * ``` - * - * @experimental - * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers - */ -export function toMatchFileSnapshot( - this: MatcherState, - received: unknown, - filepath: string, - hint?: string, -): Promise { - return toMatchFileSnapshotImpl({ - assertion: this.__vitest_assertion__, - received, - filepath, - hint, - }) + /** + * Composable for building custom file snapshot matchers via `expect.extend`. + * Call with `this` bound to the matcher state. Returns a `Promise<{ pass, message }>` + * compatible with the custom matcher return contract. + * + * @example + * ```ts + * import { Snapshots } from 'vitest/runtime' + * + * expect.extend({ + * async toMatchTrimmedFileSnapshot(received: string, file: string) { + * return Snapshots.toMatchFileSnapshot.call(this, received.slice(0, 10), file) + * }, + * }) + * ``` + * + * @experimental + * @see https://vitest.dev/guide/snapshot.html#custom-snapshot-matchers + */ + toMatchFileSnapshot( + this: MatcherState, + received: unknown, + filepath: string, + hint?: string, + ): AsyncExpectationResult { + return toMatchFileSnapshotImpl({ + assertion: this.__vitest_assertion__, + received, + filepath, + hint, + }) + }, } diff --git a/packages/vitest/src/public/index.ts b/packages/vitest/src/public/index.ts index 09aab798d..6bf7a83b9 100644 --- a/packages/vitest/src/public/index.ts +++ b/packages/vitest/src/public/index.ts @@ -30,6 +30,7 @@ export type { } from '../api/types' export { assert, chai, createExpect, expect, should } from '../integrations/chai' export { inject } from '../integrations/inject' +export { Snapshots } from '../integrations/snapshot/chai' export { vi, vitest } from '../integrations/vi' export type { VitestUtils } from '../integrations/vi' diff --git a/packages/vitest/src/public/runtime.ts b/packages/vitest/src/public/runtime.ts index 4fbf36da6..f8d048111 100644 --- a/packages/vitest/src/public/runtime.ts +++ b/packages/vitest/src/public/runtime.ts @@ -11,7 +11,6 @@ import { getWorkerState } from '../runtime/utils' export { environments as builtinEnvironments } from '../integrations/env/index' export { populateGlobal } from '../integrations/env/utils' -export { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } from '../integrations/snapshot/chai' export { VitestNodeSnapshotEnvironment as VitestSnapshotEnvironment } from '../integrations/snapshot/environments/node' export type { Environment, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ff3e04afc..4d2c401c4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1632,6 +1632,9 @@ importers: specifier: workspace:* version: link:../../packages/vitest devDependencies: + '@vitest/browser-playwright': + specifier: workspace:* + version: link:../../packages/browser-playwright jest-image-snapshot: specifier: ^6.5.1 version: 6.5.1 diff --git a/test/core/test/exports.test.ts b/test/core/test/exports.test.ts index 93aaf0555..43eb30534 100644 --- a/test/core/test/exports.test.ts +++ b/test/core/test/exports.test.ts @@ -19,6 +19,7 @@ it('exports snapshot', async ({ skip, task }) => { ".": { "BenchmarkRunner": "function", "EvaluatedModules": "function", + "Snapshots": "object", "TestRunner": "function", "afterAll": "function", "afterEach": "function", @@ -165,9 +166,6 @@ it('exports snapshot', async ({ skip, task }) => { "__INTERNAL": "object", "builtinEnvironments": "object", "populateGlobal": "function", - "toMatchFileSnapshot": "function", - "toMatchInlineSnapshot": "function", - "toMatchSnapshot": "function", }, "./snapshot": { "VitestSnapshotEnvironment": "function", diff --git a/test/snapshots/package.json b/test/snapshots/package.json index 2351a5f17..b8759ec7f 100644 --- a/test/snapshots/package.json +++ b/test/snapshots/package.json @@ -9,6 +9,7 @@ "vitest": "workspace:*" }, "devDependencies": { + "@vitest/browser-playwright": "workspace:*", "jest-image-snapshot": "^6.5.1" } } diff --git a/test/snapshots/test/custom-matcher.test.ts b/test/snapshots/test/custom-matcher.test.ts index a0226ac2f..e5aece864 100644 --- a/test/snapshots/test/custom-matcher.test.ts +++ b/test/snapshots/test/custom-matcher.test.ts @@ -1,15 +1,9 @@ import fs, { readFileSync } from 'node:fs' import { join } from 'node:path' +import { playwright } from '@vitest/browser-playwright' import { expect, test } from 'vitest' -import { editFile, runVitest } from '../../test-utils' - -const INLINE_BLOCK_RE = /\/\/ -- TEST INLINE START --\n([\s\S]*?)\/\/ -- TEST INLINE END --/g - -function extractInlineBlocks(content: string): string { - return [...content.matchAll(INLINE_BLOCK_RE)] - .map(m => m[1].trim()) - .join('\n\n') -} +import { editFile, runInlineTests, runVitest } from '../../test-utils' +import { extractInlineSnaphsots } from './utils' test('custom snapshot matcher', async () => { const root = join(import.meta.dirname, 'fixtures/custom-matcher') @@ -55,15 +49,15 @@ test('custom snapshot matcher', async () => { "reversed": "ihihih", }" `) - expect(extractInlineBlocks(readFileSync(testFile, 'utf-8'))).toMatchInlineSnapshot(` - "test('inline', () => { - expect(\`hehehe\`).toMatchCustomInlineSnapshot(\` + expect(extractInlineSnaphsots(readFileSync(testFile, 'utf-8'))).toMatchInlineSnapshot(` + " + expect(\`hehehe\`).toMatchCustomInlineSnapshot(\` Object { "length": 6, "reversed": "eheheh", } \`) - })" + " `) expect(result.errorTree()).toMatchInlineSnapshot(` Object { @@ -103,13 +97,13 @@ test('custom snapshot matcher', async () => { + "reversed": "tide-ahahah", } - ❯ basic.test.ts:46:25 - 44| - 45| test('file', () => { - 46| expect(\`hahaha-edit\`).toMatchCustomSnapshot() - | ^ - 47| }) + ❯ basic.test.ts:50:25 48| + 49| test('file', () => { + 50| expect(\`hahaha-edit\`).toMatchCustomSnapshot() + | ^ + 51| }) + 52| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/5]⎯ @@ -125,13 +119,13 @@ test('custom snapshot matcher', async () => { + "reversed": "tide-opopop", } - ❯ basic.test.ts:50:25 - 48| - 49| test('properties 1', () => { - 50| expect(\`popopo-edit\`).toMatchCustomSnapshot({ length: 6 }) - | ^ - 51| }) + ❯ basic.test.ts:54:25 52| + 53| test('properties 1', () => { + 54| expect(\`popopo-edit\`).toMatchCustomSnapshot({ length: 6 }) + | ^ + 55| }) + 56| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/5]⎯ @@ -147,13 +141,13 @@ test('custom snapshot matcher', async () => { + "reversed": "tide-epepep", } - ❯ basic.test.ts:54:25 - 52| - 53| test('properties 2', () => { - 54| expect(\`pepepe-edit\`).toMatchCustomSnapshot({ length: expect.toSatis… - | ^ - 55| }) + ❯ basic.test.ts:58:25 56| + 57| test('properties 2', () => { + 58| expect(\`pepepe-edit\`).toMatchCustomSnapshot({ length: expect.toSatis… + | ^ + 59| }) + 60| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/5]⎯ @@ -170,13 +164,13 @@ test('custom snapshot matcher', async () => { + "reversed": "tide-ihihih", } - ❯ basic.test.ts:58:3 - 56| - 57| test('raw', async () => { - 58| await expect(\`hihihi-edit\`).toMatchCustomFileSnapshot('./__snapshots… - | ^ - 59| }) + ❯ basic.test.ts:62:3 60| + 61| test('raw', async () => { + 62| await expect(\`hihihi-edit\`).toMatchCustomFileSnapshot('./__snapshots… + | ^ + 63| }) + 64| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/5]⎯ @@ -193,13 +187,13 @@ test('custom snapshot matcher', async () => { + "reversed": "tide-eheheh", } - ❯ basic.test.ts:63:25 - 61| // -- TEST INLINE START -- - 62| test('inline', () => { - 63| expect(\`hehehe-edit\`).toMatchCustomInlineSnapshot(\` + ❯ basic.test.ts:66:25 + 64| + 65| test('inline', () => { + 66| expect(\`hehehe-edit\`).toMatchCustomInlineSnapshot(\` | ^ - 64| Object { - 65| "length": 6, + 67| Object { + 68| "length": 6, ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/5]⎯ @@ -245,13 +239,13 @@ test('custom snapshot matcher', async () => { + "reversed": "tide-opopop", } - ❯ basic.test.ts:50:25 - 48| - 49| test('properties 1', () => { - 50| expect(\`popopo-edit\`).toMatchCustomSnapshot({ length: 6 }) - | ^ - 51| }) + ❯ basic.test.ts:54:25 52| + 53| test('properties 1', () => { + 54| expect(\`popopo-edit\`).toMatchCustomSnapshot({ length: 6 }) + | ^ + 55| }) + 56| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/2]⎯ @@ -267,13 +261,13 @@ test('custom snapshot matcher', async () => { + "reversed": "tide-epepep", } - ❯ basic.test.ts:54:25 - 52| - 53| test('properties 2', () => { - 54| expect(\`pepepe-edit\`).toMatchCustomSnapshot({ length: expect.toSatis… - | ^ - 55| }) + ❯ basic.test.ts:58:25 56| + 57| test('properties 2', () => { + 58| expect(\`pepepe-edit\`).toMatchCustomSnapshot({ length: expect.toSatis… + | ^ + 59| }) + 60| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/2]⎯ @@ -310,15 +304,15 @@ test('custom snapshot matcher', async () => { "reversed": "tide-ihihih", }" `) - expect(extractInlineBlocks(readFileSync(testFile, 'utf-8'))).toMatchInlineSnapshot(` - "test('inline', () => { - expect(\`hehehe-edit\`).toMatchCustomInlineSnapshot(\` + expect(extractInlineSnaphsots(readFileSync(testFile, 'utf-8'))).toMatchInlineSnapshot(` + " + expect(\`hehehe-edit\`).toMatchCustomInlineSnapshot(\` Object { "length": 11, "reversed": "tide-eheheh", } \`) - })" + " `) expect(result.errorTree()).toMatchInlineSnapshot(` Object { @@ -336,3 +330,76 @@ test('custom snapshot matcher', async () => { } `) }) + +test('browser', async () => { + const result = await runInlineTests({ + 'basic.test.ts': ` +import { test, expect, Snapshots } from 'vitest' + +const { + toMatchFileSnapshot, + toMatchInlineSnapshot, + toMatchSnapshot, +} = Snapshots + +expect.extend({ + toMatchTrimmedSnapshot(received: string) { + return toMatchSnapshot.call(this, received.slice(0, 10)) + }, + toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) { + return toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot) + }, + async toMatchTrimmedFileSnapshot(received: string, filepath: string) { + return toMatchFileSnapshot.call(this, received.slice(0, 10), filepath) + }, +}) + +test('file snapshot', () => { + expect('extra long string oh my gerd').toMatchTrimmedSnapshot() +}) + +test('inline snapshot', () => { + expect('super long string oh my gerd').toMatchTrimmedInlineSnapshot() +}) + +test('raw file snapshot', async () => { + await expect('crazy long string oh my gerd').toMatchTrimmedFileSnapshot('./raw.txt') +}) +`, + }, { + update: 'all', + browser: { + enabled: true, + headless: true, + screenshotFailures: false, + provider: playwright(), + instances: [ + { + browser: 'chromium', + }, + ], + }, + }) + expect(result.stderr).toMatchInlineSnapshot(`""`) + expect(result.errorTree()).toMatchInlineSnapshot(` + Object { + "basic.test.ts": Object { + "file snapshot": "passed", + "inline snapshot": "passed", + "raw file snapshot": "passed", + }, + } + `) + expect(result.fs.readFile('__snapshots__/basic.test.ts.snap')).toMatchInlineSnapshot(` + "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + + exports[\`file snapshot 1\`] = \`"extra long"\`; + " + `) + expect(extractInlineSnaphsots(result.fs.readFile('basic.test.ts'))).toMatchInlineSnapshot(` + " + expect('super long string oh my gerd').toMatchTrimmedInlineSnapshot(\`"super long"\`) + " + `) + expect(result.fs.readFile('raw.txt')).toMatchInlineSnapshot(`"crazy long"`) +}) diff --git a/test/snapshots/test/fixtures/custom-matcher/basic.test.ts b/test/snapshots/test/fixtures/custom-matcher/basic.test.ts index 1da84b6d0..0f99d1cd2 100644 --- a/test/snapshots/test/fixtures/custom-matcher/basic.test.ts +++ b/test/snapshots/test/fixtures/custom-matcher/basic.test.ts @@ -1,5 +1,10 @@ -import { expect, test } from 'vitest' -import { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } from "vitest/runtime" +import { expect, test, Snapshots } from 'vitest' + +const { + toMatchFileSnapshot, + toMatchInlineSnapshot, + toMatchSnapshot, +} = Snapshots // custom snapshot matcher to wraper input code string interface CustomMatchers { @@ -10,7 +15,6 @@ interface CustomMatchers { declare module 'vitest' { interface Assertion extends CustomMatchers {} - interface AsymmetricMatchersContaining extends CustomMatchers {} } function formatCustom(input: string) { @@ -58,7 +62,6 @@ test('raw', async () => { await expect(`hihihi`).toMatchCustomFileSnapshot('./__snapshots__/raw.txt') }) -// -- TEST INLINE START -- test('inline', () => { expect(`hehehe`).toMatchCustomInlineSnapshot(` Object { @@ -67,4 +70,3 @@ test('inline', () => { } `) }) -// -- TEST INLINE END -- diff --git a/test/snapshots/test/utils.ts b/test/snapshots/test/utils.ts new file mode 100644 index 000000000..32dfc2254 --- /dev/null +++ b/test/snapshots/test/utils.ts @@ -0,0 +1,14 @@ +export function extractInlineSnaphsots(code: string) { + const matches = Array.from( + code.matchAll(/\.toMatch(\w*)InlineSnapshot\(\s*`[\s\S]*?`\s*\)/g), + ) + const snapshots = matches.map((match) => { + const end = match.index! + match[0].length + const start = code.lastIndexOf('expect', match.index) + if (start === -1) { + throw new Error(`Failed to extract inline snapshot: no expect found for match ${match[0]}`) + } + return code.slice(start, end) + }) + return `\n${snapshots.join('\n\n')}\n` +} diff --git a/test/snapshots/vitest.config.ts b/test/snapshots/vitest.config.ts index 285e9484a..e57162974 100644 --- a/test/snapshots/vitest.config.ts +++ b/test/snapshots/vitest.config.ts @@ -3,7 +3,7 @@ import { defaultExclude } from 'vitest/config' export default defineConfig({ test: { - exclude: [...defaultExclude, '**/fixtures'], + exclude: [...defaultExclude, '**/fixtures', '**/vitest-test-*/**'], snapshotFormat: { printBasicPrototype: true, },