-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom "snapshotEnvironment" option (#5449)
- Loading branch information
1 parent
2f91322
commit 30f728b
Showing
22 changed files
with
195 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 @@ | ||
export * from './dist/snapshot.js' |
This file contains 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
11 changes: 4 additions & 7 deletions
11
packages/vitest/src/integrations/snapshot/environments/node.ts
This file contains 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 |
---|---|---|
@@ -1,16 +1,13 @@ | ||
import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment' | ||
import type { WorkerRPC } from '../../../types/worker' | ||
|
||
export class VitestSnapshotEnvironment extends NodeSnapshotEnvironment { | ||
constructor(private rpc: WorkerRPC) { | ||
super() | ||
} | ||
import { getWorkerState } from '../../../utils' | ||
|
||
export class VitestNodeSnapshotEnvironment extends NodeSnapshotEnvironment { | ||
getHeader(): string { | ||
return `// Vitest Snapshot v${this.getVersion()}, https://vitest.dev/guide/snapshot.html` | ||
} | ||
|
||
resolvePath(filepath: string): Promise<string> { | ||
return this.rpc.resolveSnapshotPath(filepath) | ||
const rpc = getWorkerState().rpc | ||
return rpc.resolveSnapshotPath(filepath) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/vitest/src/integrations/snapshot/environments/resolveSnapshotEnvironment.ts
This file contains 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 type { SnapshotEnvironment } from '@vitest/snapshot/environment' | ||
import type { VitestExecutor } from '../../../runtime/execute' | ||
import type { ResolvedConfig } from '../../../types' | ||
|
||
export async function resolveSnapshotEnvironment( | ||
config: ResolvedConfig, | ||
executor: VitestExecutor, | ||
): Promise<SnapshotEnvironment> { | ||
if (!config.snapshotEnvironment) { | ||
const { VitestNodeSnapshotEnvironment } = await import('./node') | ||
return new VitestNodeSnapshotEnvironment() | ||
} | ||
|
||
const mod = await executor.executeId(config.snapshotEnvironment) | ||
if (typeof mod.default !== 'object' || !mod.default) | ||
throw new Error('Snapshot environment module must have a default export object with a shape of `SnapshotEnvironment`') | ||
return mod.default | ||
} |
This file contains 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 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 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 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 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 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,4 @@ | ||
export type { SnapshotEnvironment } from '@vitest/snapshot/environment' | ||
export { | ||
VitestNodeSnapshotEnvironment as VitestSnapshotEnvironment, | ||
} from './integrations/snapshot/environments/node' |
This file contains 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 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 @@ | ||
throw new Error('This file should not be executed') |
This file contains 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 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,33 @@ | ||
import { readFileSync, rmSync, writeFileSync } from 'node:fs' | ||
import { afterEach, expect, test } from 'vitest' | ||
import { dirname, resolve } from 'pathe' | ||
import { runVitest } from '../../test-utils' | ||
|
||
const testFileName = resolve(import.meta.dirname, './fixtures/custom-snapshot-environment/test/snapshots.test.ts') | ||
const snapshotFile = resolve(dirname(testFileName), './__snapshots__/snapshots.test.ts.snap') | ||
const testFile = readFileSync(testFileName, 'utf-8') | ||
|
||
afterEach(() => { | ||
writeFileSync(testFileName, testFile) | ||
rmSync(snapshotFile) | ||
}) | ||
|
||
test('custom environment resolved correctly', async () => { | ||
const { stdout, stderr } = await runVitest({ | ||
root: 'test/fixtures/custom-snapshot-environment', | ||
update: true, | ||
}) | ||
|
||
const snapshotLogs = stdout.split('\n').filter(i => i.startsWith('## ')).join('\n') | ||
expect(stderr).toBe('') | ||
expect(snapshotLogs).toMatchInlineSnapshot(` | ||
"## resolvePath test/fixtures/custom-snapshot-environment/test/snapshots.test.ts | ||
## readSnapshotFile test/fixtures/custom-snapshot-environment/test/__snapshots__/snapshots.test.ts.snap | ||
## getHeader | ||
## getVersion | ||
## readSnapshotFile test/fixtures/custom-snapshot-environment/test/__snapshots__/snapshots.test.ts.snap | ||
## saveSnapshotFile test/fixtures/custom-snapshot-environment/test/__snapshots__/snapshots.test.ts.snap | ||
## readSnapshotFile test/fixtures/custom-snapshot-environment/test/snapshots.test.ts | ||
## saveSnapshotFile test/fixtures/custom-snapshot-environment/test/snapshots.test.ts" | ||
`) | ||
}) |
45 changes: 45 additions & 0 deletions
45
test/snapshots/test/fixtures/custom-snapshot-environment/snapshot-environment.ts
This file contains 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,45 @@ | ||
import { relative as _relative } from 'pathe' | ||
import { VitestSnapshotEnvironment } from 'vitest/snapshot' | ||
|
||
function relative(file: string) { | ||
return _relative(process.cwd(), file) | ||
} | ||
|
||
class CustomSnapshotEnvironment extends VitestSnapshotEnvironment { | ||
getVersion(): string { | ||
console.log('## getVersion') | ||
return super.getVersion() | ||
} | ||
|
||
getHeader() { | ||
console.log('## getHeader') | ||
return super.getHeader() | ||
} | ||
|
||
resolvePath(filepath: string) { | ||
console.log('## resolvePath', relative(filepath)) | ||
return super.resolvePath(filepath) | ||
} | ||
|
||
resolveRawPath(testPath: string, rawPath: string) { | ||
console.log('## resolveRawPath', relative(testPath), relative(rawPath)) | ||
return super.resolveRawPath(testPath, rawPath) | ||
} | ||
|
||
saveSnapshotFile(filepath: string, snapshot: string) { | ||
console.log('## saveSnapshotFile', relative(filepath)) | ||
return super.saveSnapshotFile(filepath, snapshot) | ||
} | ||
|
||
readSnapshotFile(filepath: string) { | ||
console.log('## readSnapshotFile', relative(filepath)) | ||
return super.readSnapshotFile(filepath) | ||
} | ||
|
||
removeSnapshotFile(filepath: string) { | ||
console.log('## removeSnapshotFile', relative(filepath)) | ||
return super.removeSnapshotFile(filepath) | ||
} | ||
} | ||
|
||
export default new CustomSnapshotEnvironment() |
9 changes: 9 additions & 0 deletions
9
test/snapshots/test/fixtures/custom-snapshot-environment/test/snapshots.test.ts
This file contains 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,9 @@ | ||
import {test, expect} from 'vitest' | ||
|
||
test('regular snapshot', () => { | ||
expect({ a: 1 }).toMatchSnapshot() | ||
}) | ||
|
||
test('inline snapshot', () => { | ||
expect({ a: 1 }).toMatchInlineSnapshot() | ||
}) |
7 changes: 7 additions & 0 deletions
7
test/snapshots/test/fixtures/custom-snapshot-environment/vitest.config.ts
This file contains 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,7 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
snapshotEnvironment: './snapshot-environment.ts' | ||
} | ||
}) |