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

This commit is contained in:
Han 2024-02-27 20:16:53 +08:00 committed by GitHub
parent 5b58b3990e
commit b9d378f5a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View File

@ -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)

View 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]()
})

View File

@ -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