-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(snapshot): provide config
to resolveSnapshotPath
#6800
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
Merged
sheremet-va
merged 13 commits into
vitest-dev:main
from
hi-ogawa:feat-resolveSnapshotPath-context
Nov 13, 2024
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
42e96b3
feat(vitest,snapshot): provide `config` to `resolveSnapshotPath`
hi-ogawa d3dea04
test: add test
hi-ogawa db8aeef
chore: unknown -> any
hi-ogawa bbb4d64
chore: lint
hi-ogawa 8014420
Merge branch 'main' into feat-resolveSnapshotPath-context
hi-ogawa bb40570
fix: export types
hi-ogawa 806c966
chore: cleanup
hi-ogawa e20cc25
chore: generics
hi-ogawa 5aadc11
Revert "chore: generics"
hi-ogawa 63ea057
Merge branch 'main' into feat-resolveSnapshotPath-context
hi-ogawa 67fa382
chore: try T again
hi-ogawa 55f6a39
chore: add ResolveSnapshotPathHandlerContext
hi-ogawa a349eac
chore: broken linter...
hi-ogawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import type { SnapshotStateOptions } from '@vitest/snapshot' | |
import type { AliasOptions, ConfigEnv, DepOptimizationConfig, ServerOptions, UserConfig as ViteUserConfig } from 'vite' | ||
import type { ViteNodeServerOptions } from 'vite-node' | ||
import type { ChaiConfig } from '../../integrations/chai/config' | ||
import type { SerializedConfig } from '../../runtime/config' | ||
import type { EnvironmentOptions } from '../../types/environment' | ||
import type { Arrayable, ErrorWithDiff, ParsedStack, ProvidedContext } from '../../types/general' | ||
import type { HappyDOMOptions } from '../../types/happy-dom-options' | ||
|
@@ -573,7 +574,11 @@ export interface InlineConfig { | |
/** | ||
* Resolve custom snapshot path | ||
*/ | ||
resolveSnapshotPath?: (path: string, extension: string) => string | ||
resolveSnapshotPath?: ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also expose a type from |
||
path: string, | ||
extension: string, | ||
context: { config: SerializedConfig } | ||
) => string | ||
|
||
/** | ||
* Path to a custom snapshot environment module that has a default export of `SnapshotEnvironment` object. | ||
|
3 changes: 3 additions & 0 deletions
3
test/config/fixtures/snapshot-path-context/__snapshots__/project1/basic.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`basic 1`] = `"hello"`; |
3 changes: 3 additions & 0 deletions
3
test/config/fixtures/snapshot-path-context/__snapshots__/project2/basic.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`basic 1`] = `"hello"`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { test, expect } from 'vitest' | ||
|
||
test('basic', () => { | ||
expect('hello').toMatchSnapshot() | ||
}) |
15 changes: 15 additions & 0 deletions
15
test/config/fixtures/snapshot-path-context/vitest.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { join, dirname, basename } from 'node:path'; | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
resolveSnapshotPath(path, extension, context) { | ||
return join( | ||
dirname(path), | ||
'__snapshots__', | ||
context.config.name ?? 'na', | ||
basename(path) + extension | ||
); | ||
}, | ||
}, | ||
}); |
18 changes: 18 additions & 0 deletions
18
test/config/fixtures/snapshot-path-context/vitest.workspace.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { defineWorkspace } from 'vitest/config' | ||
|
||
export default defineWorkspace([ | ||
{ | ||
extends: './vitest.config.ts', | ||
test: { | ||
name: 'project1', | ||
root: import.meta.dirname, | ||
} | ||
}, | ||
{ | ||
extends: './vitest.config.ts', | ||
test: { | ||
name: 'project2', | ||
root: import.meta.dirname, | ||
} | ||
} | ||
]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { expect, test } from 'vitest' | ||
import { runVitest } from '../../test-utils' | ||
|
||
test('resolveSnapshotPath context', async () => { | ||
const { stderr, ctx } = await runVitest({ | ||
root: './fixtures/snapshot-path-context', | ||
}) | ||
expect(stderr).toBe('') | ||
expect( | ||
Object.fromEntries( | ||
ctx!.state.getFiles().map(f => [`${f.projectName}|${f.name}`, f.result?.state]), | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"project1|basic.test.ts": "pass", | ||
"project2|basic.test.ts": "pass", | ||
} | ||
`) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.