Skip to content

Commit

Permalink
wip: remove more
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 30, 2024
1 parent db634ea commit 62cc7be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 83 deletions.
64 changes: 9 additions & 55 deletions packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, SnapshotState> = new Map()

constructor(private options: SnapshotClientOptions = {}) {}

async startCurrentRun(
filepath: string,
name: string,
options: SnapshotStateOptions,
): Promise<void> {
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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -162,7 +128,7 @@ export class SnapshotClient {
if (!pass) {
throw createMismatchError(
'Snapshot properties mismatched',
this.snapshotState?.expand,
snapshotState.expand,
received,
properties,
)
Expand All @@ -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,
Expand All @@ -193,7 +157,7 @@ export class SnapshotClient {
if (!pass) {
throw createMismatchError(
`Snapshot \`${key || 'unknown'}\` mismatched`,
this.snapshotState?.expand,
snapshotState.expand,
actual?.trim(),
expected?.trim(),
)
Expand All @@ -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) {
Expand All @@ -229,16 +193,6 @@ export class SnapshotClient {
return this.assert(options)
}

async finishCurrentRun(): Promise<SnapshotResult | null> {
if (!this.snapshotState) {
return null
}
const result = await this.snapshotState.pack()

this.snapshotState = undefined
return result
}

clear(): void {
this.snapshotStateMap.clear()
}
Expand Down
15 changes: 6 additions & 9 deletions packages/vitest/src/integrations/snapshot/chai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' > '),
Expand All @@ -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
Expand All @@ -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({
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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({
Expand All @@ -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(
Expand Down
20 changes: 1 addition & 19 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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],
)
Expand Down

0 comments on commit 62cc7be

Please sign in to comment.