-
-
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.
refactor: add vitest/snapshot entry point
- Loading branch information
1 parent
31300c8
commit a54ed70
Showing
16 changed files
with
131 additions
and
11 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
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
6 changes: 3 additions & 3 deletions
6
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import type { SnapshotEnvironment } from '@vitest/snapshot/environment' | ||
import type { VitestExecutor } from '../../../runtime/execute' | ||
import type { ResolvedConfig } from '../../../types' | ||
import { VitestSnapshotEnvironment } from './node' | ||
import { VitestNodeSnapshotEnvironment } from './node' | ||
|
||
export async function resolveSnapshotEnvironment( | ||
config: ResolvedConfig, | ||
executor: VitestExecutor, | ||
): Promise<SnapshotEnvironment> { | ||
if (!config.snapshotEnvironment) | ||
return new VitestSnapshotEnvironment() | ||
return new VitestNodeSnapshotEnvironment() | ||
|
||
const mod = await executor.executeId(config.snapshotEnvironment) | ||
if (typeof mod.default !== 'object' && !mod.default) | ||
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
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
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' | ||
} | ||
}) |