Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshot)!: reset snapshot state for retry and repeats #6817

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
db634ea
wip: rework snapshot state
hi-ogawa Oct 30, 2024
7cf11f5
wip: remove more
hi-ogawa Oct 30, 2024
047badf
wip: test
hi-ogawa Oct 30, 2024
0aea548
wip: clear inline snapshots
hi-ogawa Oct 30, 2024
4c835ed
wip: clear file
hi-ogawa Oct 30, 2024
99abe4c
fix: reset to _initialData
hi-ogawa Oct 30, 2024
c174615
fix: fix retry partial
hi-ogawa Oct 30, 2024
3dcf25d
chore: cleanup more
hi-ogawa Oct 30, 2024
8817ac5
test: more
hi-ogawa Oct 30, 2024
c5e4527
wip: _testIdToKeys state
hi-ogawa Oct 30, 2024
558ba71
test: more
hi-ogawa Oct 30, 2024
5b1182b
wip: _testIdToKeys
hi-ogawa Oct 30, 2024
940dbb3
fix: fix markSnapshotsAsCheckedForTest
hi-ogawa Oct 30, 2024
83741e0
Merge branch 'main' into rework-snapshot-state
hi-ogawa Oct 31, 2024
80accd6
refactor: minor
hi-ogawa Oct 31, 2024
3fc11d5
fix: clear added/updated stats
hi-ogawa Oct 31, 2024
de9b88e
chore: comment
hi-ogawa Oct 31, 2024
4a69ec1
test: move some snapshot test
hi-ogawa Oct 31, 2024
fd8d60c
test: test same title
hi-ogawa Oct 31, 2024
1cdaa67
test: test retry
hi-ogawa Oct 31, 2024
e0768a1
test: cleanup
hi-ogawa Oct 31, 2024
5bd6a34
test: test summary
hi-ogawa Oct 31, 2024
09a4bcd
chore: cleanup
hi-ogawa Oct 31, 2024
671344f
refactor: minor
hi-ogawa Oct 31, 2024
eae4ed7
Merge branch 'main' into rework-snapshot-state
hi-ogawa Nov 4, 2024
63d9817
test: use import.meta.glob
hi-ogawa Nov 4, 2024
99c64e6
fix: ensure test context
hi-ogawa Nov 4, 2024
ebbadda
fix: tweak api
hi-ogawa Nov 4, 2024
04e1ddf
chore: make `testId` optional
hi-ogawa Nov 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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