Skip to content

Commit

Permalink
wip: rework snapshot state
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 30, 2024
1 parent e934f8d commit db634ea
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
42 changes: 41 additions & 1 deletion packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export interface SnapshotClientOptions {
}

export class SnapshotClient {
// TODO: remove state
filepath?: string
name?: string
// TODO: do we need file map here?
snapshotState: SnapshotState | undefined
snapshotStateMap: Map<string, SnapshotState> = new Map()

Expand All @@ -65,6 +67,7 @@ export class SnapshotClient {
this.filepath = filepath
this.name = name

// TODO: remove and make it explicit
if (this.snapshotState?.testFilePath !== filepath) {
await this.finishCurrentRun()

Expand All @@ -78,8 +81,44 @@ export class SnapshotClient {
}
}

async setup(
filepath: string,
options: SnapshotStateOptions,
): Promise<void> {
if (this.snapshotStateMap.has(filepath)) {
throw new Error('already setup')
}
this.snapshotStateMap.set(
filepath,
await SnapshotState.create(filepath, options),
)
}

async finish(filepath: string): Promise<SnapshotResult> {
const state = this.snapshotStateMap.get(filepath)
if (!state) {
throw new Error('missing setup')
}
const result = await state.pack()
this.snapshotStateMap.delete(filepath)
return result
}

// TODO: by test id
skip(filepath: string, name: string): void {
const state = this.snapshotStateMap.get(filepath)
if (!state) {
throw new Error('missing setup')
}
state.markSnapshotsAsCheckedForTest(name)
}

getSnapshotState(filepath: string): SnapshotState {
return this.snapshotStateMap.get(filepath)!
const state = this.snapshotStateMap.get(filepath)
if (!state) {
throw new Error('snapshot state not initialized')
}
return state
}

clearTest(): void {
Expand All @@ -91,6 +130,7 @@ export class SnapshotClient {
this.snapshotState?.markSnapshotsAsCheckedForTest(name)
}

// TODO: add test id
assert(options: AssertOptions): void {
const {
filepath = this.filepath,
Expand Down
1 change: 1 addition & 0 deletions packages/snapshot/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class SnapshotManager {
addSnapshotResult(this.summary, result)
}

// TODO: can remove in favor of SnapshotEnvironment.resolvePath?
resolvePath(testPath: string): string {
const resolver
= this.options.resolveSnapshotPath || (() => {
Expand Down
1 change: 1 addition & 0 deletions packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class SnapshotState {
private _counters: Map<string, number>
private _dirty: boolean
private _updateSnapshot: SnapshotUpdateState
// TODO: key by test id. how to match with `initialData`?
private _snapshotData: SnapshotData
private _initialData: SnapshotData
private _inlineSnapshots: Array<InlineSnapshot>
Expand Down
34 changes: 21 additions & 13 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class VitestTestRunner implements VitestRunner {
}

onBeforeRunFiles() {
this.snapshotClient.clear()
// this.snapshotClient.clear()
}

onAfterRunFiles() {
Expand All @@ -62,21 +62,24 @@ export class VitestTestRunner implements VitestRunner {
for (const test of getTests(suite)) {
if (test.mode === 'skip') {
const name = getNames(test).slice(1).join(' > ')
this.snapshotClient.skipTestSnapshots(name)
// this.snapshotClient.skipTestSnapshots(name)
this.snapshotClient.skip(suite.file.filepath, name)
}
}

const result = await this.snapshotClient.finishCurrentRun()
if (result) {
await rpc().snapshotSaved(result)
}
const result = await this.snapshotClient.finish(suite.file.filepath)
await rpc().snapshotSaved(result)
// const result = await this.snapshotClient.finishCurrentRun()
// if (result) {
// await rpc().snapshotSaved(result)
// }
}

this.workerState.current = suite.suite || suite.file
}

onAfterRunTask(test: Task) {
this.snapshotClient.clearTest()
// this.snapshotClient.clearTest()

if (this.config.logHeapUsage && typeof process !== 'undefined') {
test.result!.heap = process.memoryUsage().heapUsed
Expand Down Expand Up @@ -110,13 +113,17 @@ export class VitestTestRunner implements VitestRunner {

// initialize snapshot state before running file suite
if (suite.mode !== 'skip' && 'filepath' in suite) {
// default "name" is irrelevant for Vitest since each snapshot assertion
// (e.g. `toMatchSnapshot`) specifies "filepath" / "name" pair explicitly
await this.snapshotClient.startCurrentRun(
(suite as File).filepath,
'__default_name_',
await this.snapshotClient.setup(
suite.file.filepath,
this.workerState.config.snapshotOptions,
)
// // default "name" is irrelevant for Vitest since each snapshot assertion
// // (e.g. `toMatchSnapshot`) specifies "filepath" / "name" pair explicitly
// await this.snapshotClient.startCurrentRun(
// (suite as File).filepath,
// '__default_name_',
// this.workerState.config.snapshotOptions,
// )
}

this.workerState.current = suite
Expand All @@ -132,7 +139,8 @@ export class VitestTestRunner implements VitestRunner {
expectedAssertionsNumberErrorGen: null,
testPath: test.file.filepath,
currentTestName: getTestName(test),
snapshotState: this.snapshotClient.snapshotState,
snapshotState: this.snapshotClient.getSnapshotState(test.file.filepath),
// snapshotState: this.snapshotClient.snapshotState,
},
(globalThis as any)[GLOBAL_EXPECT],
)
Expand Down

0 comments on commit db634ea

Please sign in to comment.