Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw error when using snapshot assertion with not #5294

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/vitest/src/integrations/snapshot/chai.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading