-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(snapshot)!: reset snapshot state for retry and repeats
#6817
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
Merged
Merged
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
db634ea
wip: rework snapshot state
hi-ogawa 7cf11f5
wip: remove more
hi-ogawa 047badf
wip: test
hi-ogawa 0aea548
wip: clear inline snapshots
hi-ogawa 4c835ed
wip: clear file
hi-ogawa 99abe4c
fix: reset to _initialData
hi-ogawa c174615
fix: fix retry partial
hi-ogawa 3dcf25d
chore: cleanup more
hi-ogawa 8817ac5
test: more
hi-ogawa c5e4527
wip: _testIdToKeys state
hi-ogawa 558ba71
test: more
hi-ogawa 5b1182b
wip: _testIdToKeys
hi-ogawa 940dbb3
fix: fix markSnapshotsAsCheckedForTest
hi-ogawa 83741e0
Merge branch 'main' into rework-snapshot-state
hi-ogawa 80accd6
refactor: minor
hi-ogawa 3fc11d5
fix: clear added/updated stats
hi-ogawa de9b88e
chore: comment
hi-ogawa 4a69ec1
test: move some snapshot test
hi-ogawa fd8d60c
test: test same title
hi-ogawa 1cdaa67
test: test retry
hi-ogawa e0768a1
test: cleanup
hi-ogawa 5bd6a34
test: test summary
hi-ogawa 09a4bcd
chore: cleanup
hi-ogawa 671344f
refactor: minor
hi-ogawa eae4ed7
Merge branch 'main' into rework-snapshot-state
hi-ogawa 63d9817
test: use import.meta.glob
hi-ogawa 99c64e6
fix: ensure test context
hi-ogawa ebbadda
fix: tweak api
hi-ogawa 04e1ddf
chore: make `testId` optional
hi-ogawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,8 +34,9 @@ export interface Context { | |
|
|
||
| interface AssertOptions { | ||
| received: unknown | ||
| filepath?: string | ||
| name?: string | ||
| filepath: string | ||
| name: string | ||
| testId: string | ||
| message?: string | ||
| isInline?: boolean | ||
| properties?: object | ||
|
|
@@ -50,51 +51,53 @@ 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)) { | ||
| throw new Error('already setup') | ||
| } | ||
| 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('snapshot state not initialized') | ||
|
||
| } | ||
| return state | ||
| } | ||
|
|
||
| assert(options: AssertOptions): void { | ||
| const { | ||
| filepath = this.filepath, | ||
| name = this.name, | ||
| filepath, | ||
| name, | ||
| testId, | ||
| message, | ||
| isInline = false, | ||
| properties, | ||
|
|
@@ -109,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( | ||
|
|
@@ -122,7 +127,7 @@ export class SnapshotClient { | |
| if (!pass) { | ||
| throw createMismatchError( | ||
| 'Snapshot properties mismatched', | ||
| this.snapshotState?.expand, | ||
| snapshotState.expand, | ||
| received, | ||
| properties, | ||
| ) | ||
|
|
@@ -139,9 +144,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, | ||
|
|
@@ -153,7 +157,7 @@ export class SnapshotClient { | |
| if (!pass) { | ||
| throw createMismatchError( | ||
| `Snapshot \`${key || 'unknown'}\` mismatched`, | ||
| this.snapshotState?.expand, | ||
| snapshotState.expand, | ||
| actual?.trim(), | ||
| expected?.trim(), | ||
| ) | ||
|
|
@@ -165,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) { | ||
|
|
@@ -189,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() | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.