Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to resolveSnapshotPath #1620

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ Format options for snapshot testing. These options are passed down to [`pretty-f

### resolveSnapshotPath

- **Type**: `(testPath: string, snapExtension: string) => string`
- **Type**: `(testPath: string, snapExtension: string, options: { context: any }) => string`
- **Default**: stores snapshot files in `__snapshots__` directory

Overrides default snapshot path. For example, to store snapshots next to test files:
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/integrations/snapshot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class SnapshotClient {
filePath,
new SnapshotState(
filePath,
await rpc().resolveSnapshotPath(filePath),
await rpc().resolveSnapshotPath(filePath, undefined, { context: this.test.context }),
getWorkerState().config.snapshotOptions,
),
)
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/integrations/snapshot/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SnapshotManager {
addSnapshotResult(this.summary, result)
}

resolvePath(testPath: string) {
resolvePath(testPath: string, options: { context: any }) {
const resolver = this.options.resolveSnapshotPath || (() => {
return join(
join(
Expand All @@ -26,7 +26,7 @@ export class SnapshotManager {
)
})

return resolver(testPath, this.extension)
return resolver(testPath, this.extension, options)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ function createChannel(ctx: Vitest) {
snapshotSaved(snapshot) {
ctx.snapshot.add(snapshot)
},
resolveSnapshotPath(testPath: string) {
return ctx.snapshot.resolvePath(testPath)
resolveSnapshotPath(testPath: string, options: { context: any }) {
return ctx.snapshot.resolvePath(testPath, options)
},
async getSourceMap(id, force) {
if (force) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export interface InlineConfig {
/**
* Resolve custom snapshot path
*/
resolveSnapshotPath?: (path: string, extension: string) => string
resolveSnapshotPath?: (path: string, extension: string, options: { context: any }) => string

/**
* Pass with no tests
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface SnapshotStateOptions {
updateSnapshot: SnapshotUpdateState
expand?: boolean
snapshotFormat?: PrettyFormatOptions
resolveSnapshotPath?: (path: string, extension: string) => string
resolveSnapshotPath?: (path: string, extension: string, options: { context: any }) => string
}

export interface SnapshotMatchOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface WorkerRPC {
onTaskUpdate: (pack: TaskResultPack[]) => void

snapshotSaved: (snapshot: SnapshotResult) => void
resolveSnapshotPath: (testPath: string) => string
resolveSnapshotPath: (testPath: string, options: { context: any }) => string
}

export interface WorkerGlobalState {
Expand Down