@@ -53,6 +53,14 @@ function getTestNames(test: Test) {
53
53
}
54
54
55
55
export const SnapshotPlugin : ChaiPlugin = ( chai , utils ) => {
56
+ function getTest ( assertionName : string , obj : object ) {
57
+ const test = utils . flag ( obj , 'vitest-test' )
58
+ if ( ! test ) {
59
+ throw new Error ( `'${ assertionName } ' cannot be used ` )
60
+ }
61
+ return test as Test
62
+ }
63
+
56
64
for ( const key of [ 'matchSnapshot' , 'toMatchSnapshot' ] ) {
57
65
utils . addMethod (
58
66
chai . Assertion . prototype ,
@@ -67,7 +75,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
67
75
throw new Error ( `${ key } cannot be used with "not"` )
68
76
}
69
77
const expected = utils . flag ( this , 'object' )
70
- const test : Test = utils . flag ( this , 'vitest-test' )
78
+ const test = getTest ( key , this )
71
79
if ( typeof properties === 'string' && typeof message === 'undefined' ) {
72
80
message = properties
73
81
properties = undefined
@@ -94,7 +102,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
94
102
throw new Error ( 'toMatchFileSnapshot cannot be used with "not"' )
95
103
}
96
104
const expected = utils . flag ( this , 'object' )
97
- const test : Test = utils . flag ( this , 'vitest-test' )
105
+ const test = getTest ( 'toMatchFileSnapshot' , this )
98
106
const errorMessage = utils . flag ( this , 'message' )
99
107
100
108
const promise = getSnapshotClient ( ) . assertRaw ( {
@@ -125,8 +133,8 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
125
133
if ( isNot ) {
126
134
throw new Error ( 'toMatchInlineSnapshot cannot be used with "not"' )
127
135
}
128
- const test : Test = utils . flag ( this , 'vitest-test' )
129
- const isInsideEach = test && ( test . each || test . suite ?. each )
136
+ const test = getTest ( 'toMatchInlineSnapshot' , this )
137
+ const isInsideEach = test . each || test . suite ?. each
130
138
if ( isInsideEach ) {
131
139
throw new Error (
132
140
'InlineSnapshot cannot be used inside of test.each or describe.each' ,
@@ -167,7 +175,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
167
175
)
168
176
}
169
177
const expected = utils . flag ( this , 'object' )
170
- const test : Test = utils . flag ( this , 'vitest-test' )
178
+ const test = getTest ( 'toThrowErrorMatchingSnapshot' , this )
171
179
const promise = utils . flag ( this , 'promise' ) as string | undefined
172
180
const errorMessage = utils . flag ( this , 'message' )
173
181
getSnapshotClient ( ) . assert ( {
@@ -192,8 +200,8 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
192
200
'toThrowErrorMatchingInlineSnapshot cannot be used with "not"' ,
193
201
)
194
202
}
195
- const test : Test = utils . flag ( this , 'vitest-test' )
196
- const isInsideEach = test && ( test . each || test . suite ?. each )
203
+ const test = getTest ( 'toThrowErrorMatchingInlineSnapshot' , this )
204
+ const isInsideEach = test . each || test . suite ?. each
197
205
if ( isInsideEach ) {
198
206
throw new Error (
199
207
'InlineSnapshot cannot be used inside of test.each or describe.each' ,
0 commit comments