Skip to content

Commit 6aff017

Browse files
authoredMar 29, 2023
feat!: move snapshot implementation into @vitest/snapshot (#3032)
1 parent 446308d commit 6aff017

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1197
-1082
lines changed
 

‎packages/browser/src/client/main.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { createClient } from '@vitest/ws-client'
33
import type { ResolvedConfig } from 'vitest'
44
import type { VitestRunner } from '@vitest/runner'
55
import { createBrowserRunner } from './runner'
6-
import { BrowserSnapshotEnvironment } from './snapshot'
76
import { importId } from './utils'
87
import { setupConsoleLogSpy } from './logger'
98
import { createSafeRpc, rpc, rpcDone } from './rpc'
109
import { setupDialogsSpy } from './dialog'
10+
import { BrowserSnapshotEnvironment } from './snapshot'
1111

1212
// @ts-expect-error mocking some node apis
1313
globalThis.process = { env: {}, argv: [], cwd: () => '/', stdout: { write: () => {} }, nextTick: cb => cb() }
@@ -75,19 +75,17 @@ ws.addEventListener('open', async () => {
7575

7676
await setupConsoleLogSpy()
7777
setupDialogsSpy()
78-
await runTests(paths, config)
78+
await runTests(paths, config!)
7979
})
8080

81-
let hasSnapshot = false
82-
async function runTests(paths: string[], config: any) {
81+
async function runTests(paths: string[], config: ResolvedConfig) {
8382
// need to import it before any other import, otherwise Vite optimizer will hang
8483
const viteClientPath = '/@vite/client'
8584
await import(viteClientPath)
8685

8786
const {
8887
startTests,
8988
setupCommonEnv,
90-
setupSnapshotEnvironment,
9189
takeCoverageInsideWorker,
9290
} = await importId('vitest/browser') as typeof import('vitest/browser')
9391

@@ -101,10 +99,8 @@ async function runTests(paths: string[], config: any) {
10199
runner = new BrowserRunner({ config, browserHashMap })
102100
}
103101

104-
if (!hasSnapshot) {
105-
setupSnapshotEnvironment(new BrowserSnapshotEnvironment())
106-
hasSnapshot = true
107-
}
102+
if (!config.snapshotOptions.snapshotEnvironment)
103+
config.snapshotOptions.snapshotEnvironment = new BrowserSnapshotEnvironment()
108104

109105
try {
110106
await setupCommonEnv(config)

‎packages/browser/src/client/snapshot.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { rpc } from './rpc'
22
import type { SnapshotEnvironment } from '#types'
33

44
export class BrowserSnapshotEnvironment implements SnapshotEnvironment {
5+
getVersion(): string {
6+
return '1'
7+
}
8+
9+
getHeader(): string {
10+
return `// Vitest Snapshot v${this.getVersion()}, https://vitest.dev/guide/snapshot.html`
11+
}
12+
513
readSnapshotFile(filepath: string): Promise<string | null> {
614
return rpc().readFile(filepath)
715
}

0 commit comments

Comments
 (0)
Please sign in to comment.