diff --git a/packages/snapshot/src/client.ts b/packages/snapshot/src/client.ts index 00a5e57fe8d92..76549fc2fabe9 100644 --- a/packages/snapshot/src/client.ts +++ b/packages/snapshot/src/client.ts @@ -34,8 +34,8 @@ export interface Context { interface AssertOptions { received: unknown - filepath?: string - name?: string + filepath: string + name: string message?: string isInline?: boolean properties?: object @@ -50,37 +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 = new Map() constructor(private options: SnapshotClientOptions = {}) {} - async startCurrentRun( - filepath: string, - name: string, - options: SnapshotStateOptions, - ): Promise { - this.filepath = filepath - this.name = name - - // TODO: remove and make it explicit - if (this.snapshotState?.testFilePath !== filepath) { - await this.finishCurrentRun() - - if (!this.getSnapshotState(filepath)) { - this.snapshotStateMap.set( - filepath, - await SnapshotState.create(filepath, options), - ) - } - this.snapshotState = this.getSnapshotState(filepath) - } - } - async setup( filepath: string, options: SnapshotStateOptions, @@ -95,10 +68,7 @@ export class SnapshotClient { } async finish(filepath: string): Promise { - const state = this.snapshotStateMap.get(filepath) - if (!state) { - throw new Error('missing setup') - } + const state = this.getSnapshotState(filepath) const result = await state.pack() this.snapshotStateMap.delete(filepath) return result @@ -106,13 +76,15 @@ export class SnapshotClient { // TODO: by test id skip(filepath: string, name: string): void { - const state = this.snapshotStateMap.get(filepath) - if (!state) { - throw new Error('missing setup') - } + const state = this.getSnapshotState(filepath) state.markSnapshotsAsCheckedForTest(name) } + // TODO + // reset(filepath: string, id: string): void { + // const state = this.getSnapshotState(filepath) + // } + getSnapshotState(filepath: string): SnapshotState { const state = this.snapshotStateMap.get(filepath) if (!state) { @@ -121,20 +93,11 @@ export class SnapshotClient { return state } - clearTest(): void { - this.filepath = undefined - this.name = undefined - } - - skipTestSnapshots(name: string): void { - this.snapshotState?.markSnapshotsAsCheckedForTest(name) - } - // TODO: add test id assert(options: AssertOptions): void { const { - filepath = this.filepath, - name = this.name, + filepath, + name, message, isInline = false, properties, @@ -149,6 +112,8 @@ export class SnapshotClient { throw new Error('Snapshot cannot be used outside of test') } + const snapshotState = this.getSnapshotState(filepath) + if (typeof properties === 'object') { if (typeof received !== 'object' || !received) { throw new Error( @@ -162,7 +127,7 @@ export class SnapshotClient { if (!pass) { throw createMismatchError( 'Snapshot properties mismatched', - this.snapshotState?.expand, + snapshotState.expand, received, properties, ) @@ -179,8 +144,6 @@ export class SnapshotClient { const testName = [name, ...(message ? [message] : [])].join(' > ') - const snapshotState = this.getSnapshotState(filepath) - const { actual, expected, key, pass } = snapshotState.match({ testName, received, @@ -193,7 +156,7 @@ export class SnapshotClient { if (!pass) { throw createMismatchError( `Snapshot \`${key || 'unknown'}\` mismatched`, - this.snapshotState?.expand, + snapshotState.expand, actual?.trim(), expected?.trim(), ) @@ -205,7 +168,7 @@ export class SnapshotClient { throw new Error('Raw snapshot is required') } - const { filepath = this.filepath, rawSnapshot } = options + const { filepath, rawSnapshot } = options if (rawSnapshot.content == null) { if (!filepath) { @@ -229,16 +192,6 @@ export class SnapshotClient { return this.assert(options) } - async finishCurrentRun(): Promise { - if (!this.snapshotState) { - return null - } - const result = await this.snapshotState.pack() - - this.snapshotState = undefined - return result - } - clear(): void { this.snapshotStateMap.clear() } diff --git a/packages/vitest/src/integrations/snapshot/chai.ts b/packages/vitest/src/integrations/snapshot/chai.ts index 3837d2b88d20c..b7e89077bb69d 100644 --- a/packages/vitest/src/integrations/snapshot/chai.ts +++ b/packages/vitest/src/integrations/snapshot/chai.ts @@ -44,10 +44,7 @@ function getError(expected: () => void | Error, promise: string | undefined) { throw new Error('snapshot function didn\'t throw') } -function getTestNames(test?: Test) { - if (!test) { - return {} - } +function getTestNames(test: Test) { return { filepath: test.file.filepath, name: getNames(test).slice(1).join(' > '), @@ -69,7 +66,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { throw new Error(`${key} cannot be used with "not"`) } const expected = utils.flag(this, 'object') - const test = utils.flag(this, 'vitest-test') + const test: Test = utils.flag(this, 'vitest-test') if (typeof properties === 'string' && typeof message === 'undefined') { message = properties properties = undefined @@ -96,7 +93,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { throw new Error('toMatchFileSnapshot cannot be used with "not"') } const expected = utils.flag(this, 'object') - const test = utils.flag(this, 'vitest-test') as Test + const test: Test = utils.flag(this, 'vitest-test') const errorMessage = utils.flag(this, 'message') const promise = getSnapshotClient().assertRaw({ @@ -127,7 +124,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { if (isNot) { throw new Error('toMatchInlineSnapshot cannot be used with "not"') } - const test = utils.flag(this, 'vitest-test') + const test: Test = utils.flag(this, 'vitest-test') const isInsideEach = test && (test.each || test.suite?.each) if (isInsideEach) { throw new Error( @@ -169,7 +166,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { ) } const expected = utils.flag(this, 'object') - const test = utils.flag(this, 'vitest-test') + const test: Test = utils.flag(this, 'vitest-test') const promise = utils.flag(this, 'promise') as string | undefined const errorMessage = utils.flag(this, 'message') getSnapshotClient().assert({ @@ -194,7 +191,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { 'toThrowErrorMatchingInlineSnapshot cannot be used with "not"', ) } - const test = utils.flag(this, 'vitest-test') + const test: Test = utils.flag(this, 'vitest-test') const isInsideEach = test && (test.each || test.suite?.each) if (isInsideEach) { throw new Error( diff --git a/packages/vitest/src/runtime/runners/test.ts b/packages/vitest/src/runtime/runners/test.ts index 9286f4b6c37e8..48a7c878755e6 100644 --- a/packages/vitest/src/runtime/runners/test.ts +++ b/packages/vitest/src/runtime/runners/test.ts @@ -44,11 +44,8 @@ export class VitestTestRunner implements VitestRunner { this.workerState.current = file } - onBeforeRunFiles() { - // this.snapshotClient.clear() - } - onAfterRunFiles() { + this.snapshotClient.clear() this.workerState.current = undefined } @@ -62,25 +59,19 @@ 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) + // TODO: skip by test.id this.snapshotClient.skip(suite.file.filepath, name) } } 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() - if (this.config.logHeapUsage && typeof process !== 'undefined') { test.result!.heap = process.memoryUsage().heapUsed } @@ -117,19 +108,14 @@ export class VitestTestRunner implements VitestRunner { 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 } onBeforeTryTask(test: Task) { + // TODO: + // this.snapshotClient.reset(test.file.filepath, test.id) setState( { assertionCalls: 0, @@ -140,7 +126,6 @@ export class VitestTestRunner implements VitestRunner { testPath: test.file.filepath, currentTestName: getTestName(test), snapshotState: this.snapshotClient.getSnapshotState(test.file.filepath), - // snapshotState: this.snapshotClient.snapshotState, }, (globalThis as any)[GLOBAL_EXPECT], )