@@ -34,8 +34,13 @@ export interface Context {
34
34
35
35
interface AssertOptions {
36
36
received : unknown
37
- filepath ?: string
38
- name ?: string
37
+ filepath : string
38
+ name : string
39
+ /**
40
+ * Not required but needed for `SnapshotClient.clearTest` to implement test-retry behavior.
41
+ * @default name
42
+ */
43
+ testId ?: string
39
44
message ?: string
40
45
isInline ?: boolean
41
46
properties ?: object
@@ -50,51 +55,55 @@ export interface SnapshotClientOptions {
50
55
}
51
56
52
57
export class SnapshotClient {
53
- filepath ?: string
54
- name ?: string
55
- snapshotState : SnapshotState | undefined
56
58
snapshotStateMap : Map < string , SnapshotState > = new Map ( )
57
59
58
60
constructor ( private options : SnapshotClientOptions = { } ) { }
59
61
60
- async startCurrentRun (
62
+ async setup (
61
63
filepath : string ,
62
- name : string ,
63
64
options : SnapshotStateOptions ,
64
65
) : Promise < void > {
65
- this . filepath = filepath
66
- this . name = name
67
-
68
- if ( this . snapshotState ?. testFilePath !== filepath ) {
69
- await this . finishCurrentRun ( )
70
-
71
- if ( ! this . getSnapshotState ( filepath ) ) {
72
- this . snapshotStateMap . set (
73
- filepath ,
74
- await SnapshotState . create ( filepath , options ) ,
75
- )
76
- }
77
- this . snapshotState = this . getSnapshotState ( filepath )
66
+ if ( this . snapshotStateMap . has ( filepath ) ) {
67
+ return
78
68
}
69
+ this . snapshotStateMap . set (
70
+ filepath ,
71
+ await SnapshotState . create ( filepath , options ) ,
72
+ )
79
73
}
80
74
81
- getSnapshotState ( filepath : string ) : SnapshotState {
82
- return this . snapshotStateMap . get ( filepath ) !
75
+ async finish ( filepath : string ) : Promise < SnapshotResult > {
76
+ const state = this . getSnapshotState ( filepath )
77
+ const result = await state . pack ( )
78
+ this . snapshotStateMap . delete ( filepath )
79
+ return result
80
+ }
81
+
82
+ skipTest ( filepath : string , testName : string ) : void {
83
+ const state = this . getSnapshotState ( filepath )
84
+ state . markSnapshotsAsCheckedForTest ( testName )
83
85
}
84
86
85
- clearTest ( ) : void {
86
- this . filepath = undefined
87
- this . name = undefined
87
+ clearTest ( filepath : string , testId : string ) : void {
88
+ const state = this . getSnapshotState ( filepath )
89
+ state . clearTest ( testId )
88
90
}
89
91
90
- skipTestSnapshots ( name : string ) : void {
91
- this . snapshotState ?. markSnapshotsAsCheckedForTest ( name )
92
+ getSnapshotState ( filepath : string ) : SnapshotState {
93
+ const state = this . snapshotStateMap . get ( filepath )
94
+ if ( ! state ) {
95
+ throw new Error (
96
+ `The snapshot state for '${ filepath } ' is not found. Did you call 'SnapshotClient.setup()'?` ,
97
+ )
98
+ }
99
+ return state
92
100
}
93
101
94
102
assert ( options : AssertOptions ) : void {
95
103
const {
96
- filepath = this . filepath ,
97
- name = this . name ,
104
+ filepath,
105
+ name,
106
+ testId = name ,
98
107
message,
99
108
isInline = false ,
100
109
properties,
@@ -109,6 +118,8 @@ export class SnapshotClient {
109
118
throw new Error ( 'Snapshot cannot be used outside of test' )
110
119
}
111
120
121
+ const snapshotState = this . getSnapshotState ( filepath )
122
+
112
123
if ( typeof properties === 'object' ) {
113
124
if ( typeof received !== 'object' || ! received ) {
114
125
throw new Error (
@@ -122,7 +133,7 @@ export class SnapshotClient {
122
133
if ( ! pass ) {
123
134
throw createMismatchError (
124
135
'Snapshot properties mismatched' ,
125
- this . snapshotState ? .expand ,
136
+ snapshotState . expand ,
126
137
received ,
127
138
properties ,
128
139
)
@@ -139,9 +150,8 @@ export class SnapshotClient {
139
150
140
151
const testName = [ name , ...( message ? [ message ] : [ ] ) ] . join ( ' > ' )
141
152
142
- const snapshotState = this . getSnapshotState ( filepath )
143
-
144
153
const { actual, expected, key, pass } = snapshotState . match ( {
154
+ testId,
145
155
testName,
146
156
received,
147
157
isInline,
@@ -153,7 +163,7 @@ export class SnapshotClient {
153
163
if ( ! pass ) {
154
164
throw createMismatchError (
155
165
`Snapshot \`${ key || 'unknown' } \` mismatched` ,
156
- this . snapshotState ? .expand ,
166
+ snapshotState . expand ,
157
167
actual ?. trim ( ) ,
158
168
expected ?. trim ( ) ,
159
169
)
@@ -165,7 +175,7 @@ export class SnapshotClient {
165
175
throw new Error ( 'Raw snapshot is required' )
166
176
}
167
177
168
- const { filepath = this . filepath , rawSnapshot } = options
178
+ const { filepath, rawSnapshot } = options
169
179
170
180
if ( rawSnapshot . content == null ) {
171
181
if ( ! filepath ) {
@@ -189,16 +199,6 @@ export class SnapshotClient {
189
199
return this . assert ( options )
190
200
}
191
201
192
- async finishCurrentRun ( ) : Promise < SnapshotResult | null > {
193
- if ( ! this . snapshotState ) {
194
- return null
195
- }
196
- const result = await this . snapshotState . pack ( )
197
-
198
- this . snapshotState = undefined
199
- return result
200
- }
201
-
202
202
clear ( ) : void {
203
203
this . snapshotStateMap . clear ( )
204
204
}
0 commit comments