Skip to content

Commit 08db7bb

Browse files
committed
fix: ensure test context
1 parent 63d9817 commit 08db7bb

File tree

1 file changed

+15
-7
lines changed
  • packages/vitest/src/integrations/snapshot

1 file changed

+15
-7
lines changed

packages/vitest/src/integrations/snapshot/chai.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ function getTestNames(test: Test) {
5353
}
5454

5555
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+
5664
for (const key of ['matchSnapshot', 'toMatchSnapshot']) {
5765
utils.addMethod(
5866
chai.Assertion.prototype,
@@ -67,7 +75,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
6775
throw new Error(`${key} cannot be used with "not"`)
6876
}
6977
const expected = utils.flag(this, 'object')
70-
const test: Test = utils.flag(this, 'vitest-test')
78+
const test = getTest(key, this)
7179
if (typeof properties === 'string' && typeof message === 'undefined') {
7280
message = properties
7381
properties = undefined
@@ -94,7 +102,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
94102
throw new Error('toMatchFileSnapshot cannot be used with "not"')
95103
}
96104
const expected = utils.flag(this, 'object')
97-
const test: Test = utils.flag(this, 'vitest-test')
105+
const test = getTest('toMatchFileSnapshot', this)
98106
const errorMessage = utils.flag(this, 'message')
99107

100108
const promise = getSnapshotClient().assertRaw({
@@ -125,8 +133,8 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
125133
if (isNot) {
126134
throw new Error('toMatchInlineSnapshot cannot be used with "not"')
127135
}
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
130138
if (isInsideEach) {
131139
throw new Error(
132140
'InlineSnapshot cannot be used inside of test.each or describe.each',
@@ -167,7 +175,7 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
167175
)
168176
}
169177
const expected = utils.flag(this, 'object')
170-
const test: Test = utils.flag(this, 'vitest-test')
178+
const test = getTest('toThrowErrorMatchingSnapshot', this)
171179
const promise = utils.flag(this, 'promise') as string | undefined
172180
const errorMessage = utils.flag(this, 'message')
173181
getSnapshotClient().assert({
@@ -192,8 +200,8 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
192200
'toThrowErrorMatchingInlineSnapshot cannot be used with "not"',
193201
)
194202
}
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
197205
if (isInsideEach) {
198206
throw new Error(
199207
'InlineSnapshot cannot be used inside of test.each or describe.each',

0 commit comments

Comments
 (0)