[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat(snapshot): provide `config` to `resolveSnapshotPath` (#6800)

authored by

Hiroshi Ogawa and committed by
GitHub
(Nov 13, 2024, 5:04 PM +0100) 746d8986 697c35c5

+87 -8
+1 -1
docs/config/index.md
··· 1899 1899 1900 1900 ### resolveSnapshotPath<NonProjectOption /> 1901 1901 1902 - - **Type**: `(testPath: string, snapExtension: string) => string` 1902 + - **Type**: `(testPath: string, snapExtension: string, context: { config: SerializedConfig }) => string` 1903 1903 - **Default**: stores snapshot files in `__snapshots__` directory 1904 1904 1905 1905 Overrides default snapshot path. For example, to store snapshots next to test files:
+2 -2
packages/snapshot/src/manager.ts
··· 23 23 addSnapshotResult(this.summary, result) 24 24 } 25 25 26 - resolvePath(testPath: string): string { 26 + resolvePath<T = any>(testPath: string, context?: T): string { 27 27 const resolver 28 28 = this.options.resolveSnapshotPath || (() => { 29 29 return join( ··· 32 32 ) 33 33 }) 34 34 35 - const path = resolver(testPath, this.extension) 35 + const path = resolver(testPath, this.extension, context) 36 36 return path 37 37 } 38 38
+19
test/config/test/snapshot.test.ts
··· 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 + })
+4 -2
packages/browser/src/node/rpc.ts
··· 1 1 import type { ErrorWithDiff } from 'vitest' 2 - import type { BrowserCommandContext } from 'vitest/node' 2 + import type { BrowserCommandContext, ResolveSnapshotPathHandlerContext } from 'vitest/node' 3 3 import type { WebSocket } from 'ws' 4 4 import type { BrowserServer } from './server' 5 5 import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from './types' ··· 90 90 return ctx.report('onUserConsoleLog', log) 91 91 }, 92 92 resolveSnapshotPath(testPath) { 93 - return ctx.snapshot.resolvePath(testPath) 93 + return ctx.snapshot.resolvePath<ResolveSnapshotPathHandlerContext>(testPath, { 94 + config: project.getSerializableConfig(), 95 + }) 94 96 }, 95 97 resolveSnapshotRawPath(testPath, rawPath) { 96 98 return ctx.snapshot.resolveRawPath(testPath, rawPath)
+1 -1
packages/snapshot/src/types/index.ts
··· 20 20 snapshotEnvironment: SnapshotEnvironment 21 21 expand?: boolean 22 22 snapshotFormat?: PrettyFormatOptions 23 - resolveSnapshotPath?: (path: string, extension: string) => string 23 + resolveSnapshotPath?: (path: string, extension: string, context?: any) => string 24 24 } 25 25 26 26 export interface SnapshotMatchOptions {
+2
packages/vitest/src/public/node.ts
··· 79 79 ProjectConfig, 80 80 ResolvedConfig, 81 81 ResolvedProjectConfig, 82 + ResolveSnapshotPathHandler, 83 + ResolveSnapshotPathHandlerContext, 82 84 RuntimeConfig, 83 85 SequenceHooks, 84 86 SequenceSetupFiles,
+5
test/config/fixtures/snapshot-path-context/basic.test.ts
··· 1 + import { test, expect } from 'vitest' 2 + 3 + test('basic', () => { 4 + expect('hello').toMatchSnapshot() 5 + })
+15
test/config/fixtures/snapshot-path-context/vitest.config.ts
··· 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 + });
+18
test/config/fixtures/snapshot-path-context/vitest.workspace.ts
··· 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 + ])
+4 -1
packages/vitest/src/node/pools/rpc.ts
··· 1 1 import type { RawSourceMap } from 'vite-node' 2 2 import type { RuntimeRPC } from '../../types/rpc' 3 + import type { ResolveSnapshotPathHandlerContext } from '../types/config' 3 4 import type { WorkspaceProject } from '../workspace' 4 5 import { mkdir, writeFile } from 'node:fs/promises' 5 6 import { join } from 'pathe' ··· 20 21 ctx.snapshot.add(snapshot) 21 22 }, 22 23 resolveSnapshotPath(testPath: string) { 23 - return ctx.snapshot.resolvePath(testPath) 24 + return ctx.snapshot.resolvePath<ResolveSnapshotPathHandlerContext>(testPath, { 25 + config: project.getSerializableConfig(), 26 + }) 24 27 }, 25 28 async getSourceMap(id, force) { 26 29 if (force) {
+10 -1
packages/vitest/src/node/types/config.ts
··· 6 6 import type { AliasOptions, ConfigEnv, DepOptimizationConfig, ServerOptions, UserConfig as ViteUserConfig } from 'vite' 7 7 import type { ViteNodeServerOptions } from 'vite-node' 8 8 import type { ChaiConfig } from '../../integrations/chai/config' 9 + import type { SerializedConfig } from '../../runtime/config' 9 10 import type { EnvironmentOptions } from '../../types/environment' 10 11 import type { Arrayable, ErrorWithDiff, ParsedStack, ProvidedContext } from '../../types/general' 11 12 import type { HappyDOMOptions } from '../../types/happy-dom-options' ··· 224 225 ? [Name, object] 225 226 : [Name, Partial<BuiltinReporterOptions[Name]>] 226 227 : [Name, Record<string, unknown>] 228 + 229 + export interface ResolveSnapshotPathHandlerContext { config: SerializedConfig } 230 + 231 + export type ResolveSnapshotPathHandler = ( 232 + testPath: string, 233 + snapExtension: string, 234 + context: ResolveSnapshotPathHandlerContext 235 + ) => string 227 236 228 237 export interface InlineConfig { 229 238 /** ··· 574 583 /** 575 584 * Resolve custom snapshot path 576 585 */ 577 - resolveSnapshotPath?: (path: string, extension: string) => string 586 + resolveSnapshotPath?: ResolveSnapshotPathHandler 578 587 579 588 /** 580 589 * Path to a custom snapshot environment module that has a default export of `SnapshotEnvironment` object.
+3
test/config/fixtures/snapshot-path-context/__snapshots__/project1/basic.test.ts.snap
··· 1 + // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html 2 + 3 + exports[`basic 1`] = `"hello"`;
+3
test/config/fixtures/snapshot-path-context/__snapshots__/project2/basic.test.ts.snap
··· 1 + // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html 2 + 3 + exports[`basic 1`] = `"hello"`;