Skip to content

Commit

Permalink
fix(snapshot)!: reset snapshot state for retry and repeats (#6817)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 5, 2024
1 parent db7a888 commit e8ce94c
Show file tree
Hide file tree
Showing 31 changed files with 554 additions and 145 deletions.
88 changes: 44 additions & 44 deletions packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ export interface Context {

interface AssertOptions {
received: unknown
filepath?: string
name?: string
filepath: string
name: string
/**
* Not required but needed for `SnapshotClient.clearTest` to implement test-retry behavior.
* @default name
*/
testId?: string
message?: string
isInline?: boolean
properties?: object
Expand All @@ -50,51 +55,55 @@ export interface SnapshotClientOptions {
}

export class SnapshotClient {
filepath?: string
name?: string
snapshotState: SnapshotState | undefined
snapshotStateMap: Map<string, SnapshotState> = new Map()

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

async startCurrentRun(
async setup(
filepath: string,
name: string,
options: SnapshotStateOptions,
): Promise<void> {
this.filepath = filepath
this.name = name

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)
if (this.snapshotStateMap.has(filepath)) {
return
}
this.snapshotStateMap.set(
filepath,
await SnapshotState.create(filepath, options),
)
}

getSnapshotState(filepath: string): SnapshotState {
return this.snapshotStateMap.get(filepath)!
async finish(filepath: string): Promise<SnapshotResult> {
const state = this.getSnapshotState(filepath)
const result = await state.pack()
this.snapshotStateMap.delete(filepath)
return result
}

skipTest(filepath: string, testName: string): void {
const state = this.getSnapshotState(filepath)
state.markSnapshotsAsCheckedForTest(testName)
}

clearTest(): void {
this.filepath = undefined
this.name = undefined
clearTest(filepath: string, testId: string): void {
const state = this.getSnapshotState(filepath)
state.clearTest(testId)
}

skipTestSnapshots(name: string): void {
this.snapshotState?.markSnapshotsAsCheckedForTest(name)
getSnapshotState(filepath: string): SnapshotState {
const state = this.snapshotStateMap.get(filepath)
if (!state) {
throw new Error(
`The snapshot state for '${filepath}' is not found. Did you call 'SnapshotClient.setup()'?`,
)
}
return state
}

assert(options: AssertOptions): void {
const {
filepath = this.filepath,
name = this.name,
filepath,
name,
testId = name,
message,
isInline = false,
properties,
Expand All @@ -109,6 +118,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 @@ -122,7 +133,7 @@ export class SnapshotClient {
if (!pass) {
throw createMismatchError(
'Snapshot properties mismatched',
this.snapshotState?.expand,
snapshotState.expand,
received,
properties,
)
Expand All @@ -139,9 +150,8 @@ export class SnapshotClient {

const testName = [name, ...(message ? [message] : [])].join(' > ')

const snapshotState = this.getSnapshotState(filepath)

const { actual, expected, key, pass } = snapshotState.match({
testId,
testName,
received,
isInline,
Expand All @@ -153,7 +163,7 @@ export class SnapshotClient {
if (!pass) {
throw createMismatchError(
`Snapshot \`${key || 'unknown'}\` mismatched`,
this.snapshotState?.expand,
snapshotState.expand,
actual?.trim(),
expected?.trim(),
)
Expand All @@ -165,7 +175,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 @@ -189,16 +199,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
1 change: 1 addition & 0 deletions packages/snapshot/src/port/inlineSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

export interface InlineSnapshot {
snapshot: string
testId: string
file: string
line: number
column: number
Expand Down
Loading

0 comments on commit e8ce94c

Please sign in to comment.