Skip to content

Commit 746d898

Browse files
authored
feat(snapshot): provide config to resolveSnapshotPath (#6800)
1 parent 697c35c commit 746d898

File tree

13 files changed

+87
-8
lines changed

13 files changed

+87
-8
lines changed

Diff for: docs/config/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ A list of paths to snapshot serializer modules for snapshot testing, useful if y
18991899

19001900
### resolveSnapshotPath<NonProjectOption />
19011901

1902-
- **Type**: `(testPath: string, snapExtension: string) => string`
1902+
- **Type**: `(testPath: string, snapExtension: string, context: { config: SerializedConfig }) => string`
19031903
- **Default**: stores snapshot files in `__snapshots__` directory
19041904

19051905
Overrides default snapshot path. For example, to store snapshots next to test files:

Diff for: packages/browser/src/node/rpc.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ErrorWithDiff } from 'vitest'
2-
import type { BrowserCommandContext } from 'vitest/node'
2+
import type { BrowserCommandContext, ResolveSnapshotPathHandlerContext } from 'vitest/node'
33
import type { WebSocket } from 'ws'
44
import type { BrowserServer } from './server'
55
import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from './types'
@@ -90,7 +90,9 @@ export function setupBrowserRpc(server: BrowserServer) {
9090
return ctx.report('onUserConsoleLog', log)
9191
},
9292
resolveSnapshotPath(testPath) {
93-
return ctx.snapshot.resolvePath(testPath)
93+
return ctx.snapshot.resolvePath<ResolveSnapshotPathHandlerContext>(testPath, {
94+
config: project.getSerializableConfig(),
95+
})
9496
},
9597
resolveSnapshotRawPath(testPath, rawPath) {
9698
return ctx.snapshot.resolveRawPath(testPath, rawPath)

Diff for: packages/snapshot/src/manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class SnapshotManager {
2323
addSnapshotResult(this.summary, result)
2424
}
2525

26-
resolvePath(testPath: string): string {
26+
resolvePath<T = any>(testPath: string, context?: T): string {
2727
const resolver
2828
= this.options.resolveSnapshotPath || (() => {
2929
return join(
@@ -32,7 +32,7 @@ export class SnapshotManager {
3232
)
3333
})
3434

35-
const path = resolver(testPath, this.extension)
35+
const path = resolver(testPath, this.extension, context)
3636
return path
3737
}
3838

Diff for: packages/snapshot/src/types/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface SnapshotStateOptions {
2020
snapshotEnvironment: SnapshotEnvironment
2121
expand?: boolean
2222
snapshotFormat?: PrettyFormatOptions
23-
resolveSnapshotPath?: (path: string, extension: string) => string
23+
resolveSnapshotPath?: (path: string, extension: string, context?: any) => string
2424
}
2525

2626
export interface SnapshotMatchOptions {

Diff for: packages/vitest/src/node/pools/rpc.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RawSourceMap } from 'vite-node'
22
import type { RuntimeRPC } from '../../types/rpc'
3+
import type { ResolveSnapshotPathHandlerContext } from '../types/config'
34
import type { WorkspaceProject } from '../workspace'
45
import { mkdir, writeFile } from 'node:fs/promises'
56
import { join } from 'pathe'
@@ -20,7 +21,9 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
2021
ctx.snapshot.add(snapshot)
2122
},
2223
resolveSnapshotPath(testPath: string) {
23-
return ctx.snapshot.resolvePath(testPath)
24+
return ctx.snapshot.resolvePath<ResolveSnapshotPathHandlerContext>(testPath, {
25+
config: project.getSerializableConfig(),
26+
})
2427
},
2528
async getSourceMap(id, force) {
2629
if (force) {

Diff for: packages/vitest/src/node/types/config.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { SerializedDiffOptions } from '@vitest/utils/diff'
66
import type { AliasOptions, ConfigEnv, DepOptimizationConfig, ServerOptions, UserConfig as ViteUserConfig } from 'vite'
77
import type { ViteNodeServerOptions } from 'vite-node'
88
import type { ChaiConfig } from '../../integrations/chai/config'
9+
import type { SerializedConfig } from '../../runtime/config'
910
import type { EnvironmentOptions } from '../../types/environment'
1011
import type { Arrayable, ErrorWithDiff, ParsedStack, ProvidedContext } from '../../types/general'
1112
import type { HappyDOMOptions } from '../../types/happy-dom-options'
@@ -225,6 +226,14 @@ type ReporterWithOptions<Name extends ReporterName = ReporterName> =
225226
: [Name, Partial<BuiltinReporterOptions[Name]>]
226227
: [Name, Record<string, unknown>]
227228

229+
export interface ResolveSnapshotPathHandlerContext { config: SerializedConfig }
230+
231+
export type ResolveSnapshotPathHandler = (
232+
testPath: string,
233+
snapExtension: string,
234+
context: ResolveSnapshotPathHandlerContext
235+
) => string
236+
228237
export interface InlineConfig {
229238
/**
230239
* Name of the project. Will be used to display in the reporter.
@@ -574,7 +583,7 @@ export interface InlineConfig {
574583
/**
575584
* Resolve custom snapshot path
576585
*/
577-
resolveSnapshotPath?: (path: string, extension: string) => string
586+
resolveSnapshotPath?: ResolveSnapshotPathHandler
578587

579588
/**
580589
* Path to a custom snapshot environment module that has a default export of `SnapshotEnvironment` object.

Diff for: packages/vitest/src/public/node.ts

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export type {
7979
ProjectConfig,
8080
ResolvedConfig,
8181
ResolvedProjectConfig,
82+
ResolveSnapshotPathHandler,
83+
ResolveSnapshotPathHandlerContext,
8284
RuntimeConfig,
8385
SequenceHooks,
8486
SequenceSetupFiles,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`basic 1`] = `"hello"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`basic 1`] = `"hello"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test, expect } from 'vitest'
2+
3+
test('basic', () => {
4+
expect('hello').toMatchSnapshot()
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { join, dirname, basename } from 'node:path';
2+
import { defineConfig } from 'vitest/config';
3+
4+
export default defineConfig({
5+
test: {
6+
resolveSnapshotPath(path, extension, context) {
7+
return join(
8+
dirname(path),
9+
'__snapshots__',
10+
context.config.name ?? 'na',
11+
basename(path) + extension
12+
);
13+
},
14+
},
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineWorkspace } from 'vitest/config'
2+
3+
export default defineWorkspace([
4+
{
5+
extends: './vitest.config.ts',
6+
test: {
7+
name: 'project1',
8+
root: import.meta.dirname,
9+
}
10+
},
11+
{
12+
extends: './vitest.config.ts',
13+
test: {
14+
name: 'project2',
15+
root: import.meta.dirname,
16+
}
17+
}
18+
])

Diff for: test/config/test/snapshot.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect, test } from 'vitest'
2+
import { runVitest } from '../../test-utils'
3+
4+
test('resolveSnapshotPath context', async () => {
5+
const { stderr, ctx } = await runVitest({
6+
root: './fixtures/snapshot-path-context',
7+
})
8+
expect(stderr).toBe('')
9+
expect(
10+
Object.fromEntries(
11+
ctx!.state.getFiles().map(f => [`${f.projectName}|${f.name}`, f.result?.state]),
12+
),
13+
).toMatchInlineSnapshot(`
14+
{
15+
"project1|basic.test.ts": "pass",
16+
"project2|basic.test.ts": "pass",
17+
}
18+
`)
19+
})

0 commit comments

Comments
 (0)