mirror of
https://github.com/vitest-dev/vitest.git
synced 2026-02-01 17:36:51 +00:00
feat: throw error when using snapshot assertion with not (#5294)
This commit is contained in:
parent
5b58b3990e
commit
b9d378f5a0
@ -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') {
|
||||
@ -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')
|
||||
@ -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)
|
||||
@ -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
|
||||
@ -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)
|
||||
|
||||
11
test/fails/fixtures/snapshot-with-not.test.ts
Normal file
11
test/fails/fixtures/snapshot-with-not.test.ts
Normal file
@ -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]()
|
||||
})
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user