Skip to content

Commit b9d378f

Browse files
authoredFeb 27, 2024··
feat: throw error when using snapshot assertion with not (#5294)
1 parent 5b58b39 commit b9d378f

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
 

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

+15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
5252
chai.Assertion.prototype,
5353
key,
5454
function (this: Record<string, unknown>, properties?: object, message?: string) {
55+
const isNot = utils.flag(this, 'negate')
56+
if (isNot)
57+
throw new Error(`${key} cannot be used with "not"`)
5558
const expected = utils.flag(this, 'object')
5659
const test = utils.flag(this, 'vitest-test')
5760
if (typeof properties === 'string' && typeof message === 'undefined') {
@@ -75,6 +78,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
7578
chai.Assertion.prototype,
7679
'toMatchFileSnapshot',
7780
function (this: Record<string, unknown>, file: string, message?: string) {
81+
const isNot = utils.flag(this, 'negate')
82+
if (isNot)
83+
throw new Error('toMatchFileSnapshot cannot be used with "not"')
7884
const expected = utils.flag(this, 'object')
7985
const test = utils.flag(this, 'vitest-test') as Test
8086
const errorMessage = utils.flag(this, 'message')
@@ -98,6 +104,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
98104
chai.Assertion.prototype,
99105
'toMatchInlineSnapshot',
100106
function __INLINE_SNAPSHOT__(this: Record<string, unknown>, properties?: object, inlineSnapshot?: string, message?: string) {
107+
const isNot = utils.flag(this, 'negate')
108+
if (isNot)
109+
throw new Error('toMatchInlineSnapshot cannot be used with "not"')
101110
const test = utils.flag(this, 'vitest-test')
102111
const isInsideEach = test && (test.each || test.suite?.each)
103112
if (isInsideEach)
@@ -129,6 +138,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
129138
chai.Assertion.prototype,
130139
'toThrowErrorMatchingSnapshot',
131140
function (this: Record<string, unknown>, message?: string) {
141+
const isNot = utils.flag(this, 'negate')
142+
if (isNot)
143+
throw new Error('toThrowErrorMatchingSnapshot cannot be used with "not"')
132144
const expected = utils.flag(this, 'object')
133145
const test = utils.flag(this, 'vitest-test')
134146
const promise = utils.flag(this, 'promise') as string | undefined
@@ -145,6 +157,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
145157
chai.Assertion.prototype,
146158
'toThrowErrorMatchingInlineSnapshot',
147159
function __INLINE_SNAPSHOT__(this: Record<string, unknown>, inlineSnapshot: string, message: string) {
160+
const isNot = utils.flag(this, 'negate')
161+
if (isNot)
162+
throw new Error('toThrowErrorMatchingInlineSnapshot cannot be used with "not"')
148163
const test = utils.flag(this, 'vitest-test')
149164
const isInsideEach = test && (test.each || test.suite?.each)
150165
if (isInsideEach)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from "vitest"
2+
3+
test.each([
4+
'toMatchSnapshot',
5+
'toMatchFileSnapshot',
6+
'toMatchInlineSnapshot',
7+
'toThrowErrorMatchingSnapshot',
8+
'toThrowErrorMatchingInlineSnapshot',
9+
])('%s should fail with not', (api) => {
10+
(expect(0).not as any)[api]()
11+
})

‎test/fails/test/__snapshots__/runner.test.ts.snap

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ exports[`should fail nested-suite.test.ts > nested-suite.test.ts 1`] = `"Asserti
3333
3434
exports[`should fail primitive-error.test.ts > primitive-error.test.ts 1`] = `"Unknown Error: 42"`;
3535
36+
exports[`should fail snapshot-with-not.test.ts > snapshot-with-not.test.ts 1`] = `
37+
"Error: toThrowErrorMatchingInlineSnapshot cannot be used with "not"
38+
Error: toThrowErrorMatchingSnapshot cannot be used with "not"
39+
Error: toMatchInlineSnapshot cannot be used with "not"
40+
Error: toMatchFileSnapshot cannot be used with "not"
41+
Error: toMatchSnapshot cannot be used with "not""
42+
`;
43+
3644
exports[`should fail stall.test.ts > stall.test.ts 1`] = `
3745
"TypeError: failure
3846
TypeError: failure

0 commit comments

Comments
 (0)
Please sign in to comment.