Skip to content

Commit

Permalink
feat: throw error when using snapshot assertion with not (#5294)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenghan34 committed Feb 27, 2024
1 parent 5b58b39 commit b9d378f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/vitest/src/integrations/snapshot/chai.ts
Expand Up @@ -52,6 +52,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
chai.Assertion.prototype,
key,
function (this: Record<string, unknown>, properties?: object, message?: string) {
const isNot = utils.flag(this, 'negate')
if (isNot)
throw new Error(`${key} cannot be used with "not"`)
const expected = utils.flag(this, 'object')
const test = utils.flag(this, 'vitest-test')
if (typeof properties === 'string' && typeof message === 'undefined') {
Expand All @@ -75,6 +78,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
chai.Assertion.prototype,
'toMatchFileSnapshot',
function (this: Record<string, unknown>, file: string, message?: string) {
const isNot = utils.flag(this, 'negate')
if (isNot)
throw new Error('toMatchFileSnapshot cannot be used with "not"')
const expected = utils.flag(this, 'object')
const test = utils.flag(this, 'vitest-test') as Test
const errorMessage = utils.flag(this, 'message')
Expand All @@ -98,6 +104,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
chai.Assertion.prototype,
'toMatchInlineSnapshot',
function __INLINE_SNAPSHOT__(this: Record<string, unknown>, properties?: object, inlineSnapshot?: string, message?: string) {
const isNot = utils.flag(this, 'negate')
if (isNot)
throw new Error('toMatchInlineSnapshot cannot be used with "not"')
const test = utils.flag(this, 'vitest-test')
const isInsideEach = test && (test.each || test.suite?.each)
if (isInsideEach)
Expand Down Expand Up @@ -129,6 +138,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
chai.Assertion.prototype,
'toThrowErrorMatchingSnapshot',
function (this: Record<string, unknown>, message?: string) {
const isNot = utils.flag(this, 'negate')
if (isNot)
throw new Error('toThrowErrorMatchingSnapshot cannot be used with "not"')
const expected = utils.flag(this, 'object')
const test = utils.flag(this, 'vitest-test')
const promise = utils.flag(this, 'promise') as string | undefined
Expand All @@ -145,6 +157,9 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
chai.Assertion.prototype,
'toThrowErrorMatchingInlineSnapshot',
function __INLINE_SNAPSHOT__(this: Record<string, unknown>, inlineSnapshot: string, message: string) {
const isNot = utils.flag(this, 'negate')
if (isNot)
throw new Error('toThrowErrorMatchingInlineSnapshot cannot be used with "not"')
const test = utils.flag(this, 'vitest-test')
const isInsideEach = test && (test.each || test.suite?.each)
if (isInsideEach)
Expand Down
11 changes: 11 additions & 0 deletions test/fails/fixtures/snapshot-with-not.test.ts
@@ -0,0 +1,11 @@
import { expect, test } from "vitest"

test.each([
'toMatchSnapshot',
'toMatchFileSnapshot',
'toMatchInlineSnapshot',
'toThrowErrorMatchingSnapshot',
'toThrowErrorMatchingInlineSnapshot',
])('%s should fail with not', (api) => {
(expect(0).not as any)[api]()
})
8 changes: 8 additions & 0 deletions test/fails/test/__snapshots__/runner.test.ts.snap
Expand Up @@ -33,6 +33,14 @@ exports[`should fail nested-suite.test.ts > nested-suite.test.ts 1`] = `"Asserti
exports[`should fail primitive-error.test.ts > primitive-error.test.ts 1`] = `"Unknown Error: 42"`;
exports[`should fail snapshot-with-not.test.ts > snapshot-with-not.test.ts 1`] = `
"Error: toThrowErrorMatchingInlineSnapshot cannot be used with "not"
Error: toThrowErrorMatchingSnapshot cannot be used with "not"
Error: toMatchInlineSnapshot cannot be used with "not"
Error: toMatchFileSnapshot cannot be used with "not"
Error: toMatchSnapshot cannot be used with "not""
`;
exports[`should fail stall.test.ts > stall.test.ts 1`] = `
"TypeError: failure
TypeError: failure
Expand Down

0 comments on commit b9d378f

Please sign in to comment.