From 7c37606672e066492b6e6ba8a99aeb1b2ea77b0e Mon Sep 17 00:00:00 2001 From: shulaoda Date: Tue, 15 Oct 2024 12:54:21 +0800 Subject: [PATCH] fix(expect): correct behavior of toThrowError with empty string parameter --- packages/expect/src/jest-expect.ts | 3 ++- test/core/test/jest-expect.test.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 3d8a232c06ef..56a1a83b3ef3 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -643,7 +643,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { || typeof expected === 'undefined' || expected instanceof RegExp ) { - return this.throws(expected) + // Fixes the issue related to `chai` + return this.throws(expected === '' ? /^$/ : expected) } const obj = this._obj diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 620e0240bc02..554bfa6982b4 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -106,6 +106,13 @@ describe('jest-expect', () => { // eslint-disable-next-line no-throw-literal throw '' }).toThrow(/^$/) + expect(() => { + // eslint-disable-next-line no-throw-literal + throw '' + }).toThrow('') + expect(() => { + throw new Error('error') + }).not.toThrowError('') expect([1, 2, 3]).toHaveLength(3) expect('abc').toHaveLength(3) expect('').not.toHaveLength(5)