From 62cc7bec431489b55f8f9cca12734e4673dea00d Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 30 Oct 2024 12:46:05 +0900 Subject: [PATCH] wip: remove more --- packages/snapshot/src/client.ts | 64 +++---------------- .../vitest/src/integrations/snapshot/chai.ts | 15 ++--- packages/vitest/src/runtime/runners/test.ts | 20 +----- 3 files changed, 16 insertions(+), 83 deletions(-) diff --git a/packages/snapshot/src/client.ts b/packages/snapshot/src/client.ts index 00a5e57fe8d92..86304054c3e59 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, @@ -121,20 +94,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 +113,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 +128,7 @@ export class SnapshotClient { if (!pass) { throw createMismatchError( 'Snapshot properties mismatched', - this.snapshotState?.expand, + snapshotState.expand, received, properties, ) @@ -179,8 +145,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 +157,7 @@ export class SnapshotClient { if (!pass) { throw createMismatchError( `Snapshot \`${key || 'unknown'}\` mismatched`, - this.snapshotState?.expand, + snapshotState.expand, actual?.trim(), expected?.trim(), ) @@ -205,7 +169,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 +193,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..ba58ee4cffa94 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,18 @@ 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.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,13 +107,6 @@ 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 @@ -140,7 +123,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], )