From 2251ee6c141ff11040bb82d0361f206e9642ee9f Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 25 Mar 2024 18:25:57 +0800 Subject: [PATCH 1/2] fix: the value received by toMatch should be a string --- packages/expect/src/jest-expect.ts | 3 +++ test/core/test/jest-expect.test.ts | 2 ++ 2 files changed, 5 insertions(+) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 1df2260926e8..3abd24bd59b2 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -171,6 +171,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { }) def('toMatch', function (expected: string | RegExp) { const actual = this._obj as string + if (typeof actual !== 'string') + throw new TypeError(`.toMatch() expects to received a string, got ${typeof actual}`) + return this.assert( typeof expected === 'string' ? actual.includes(expected) diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 990361d41146..e9fe19fcc986 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -109,6 +109,8 @@ describe('jest-expect', () => { expect(0.2 + 0.1).not.toBe(0.3) expect(0.2 + 0.1).toBeCloseTo(0.3, 5) expect(0.2 + 0.1).not.toBeCloseTo(0.3, 100) // expect.closeTo will fail in chai + + expect(() => expect(1).toMatch(/\d/)).toThrowErrorMatchingInlineSnapshot(`[TypeError: .toMatch() expects to received a string, got number]`) }) it('asymmetric matchers (jest style)', () => { From b00fc372bcb3ba507702d0d07b8f29e556b55693 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Tue, 26 Mar 2024 08:12:24 +0800 Subject: [PATCH 2/2] fix: update message --- packages/expect/src/jest-expect.ts | 2 +- test/core/test/jest-expect.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 3abd24bd59b2..37cdf0e80b39 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -172,7 +172,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { def('toMatch', function (expected: string | RegExp) { const actual = this._obj as string if (typeof actual !== 'string') - throw new TypeError(`.toMatch() expects to received a string, got ${typeof actual}`) + throw new TypeError(`.toMatch() expects to receive a string, but got ${typeof actual}`) return this.assert( typeof expected === 'string' diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index e9fe19fcc986..c0b293f13604 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -110,7 +110,7 @@ describe('jest-expect', () => { expect(0.2 + 0.1).toBeCloseTo(0.3, 5) expect(0.2 + 0.1).not.toBeCloseTo(0.3, 100) // expect.closeTo will fail in chai - expect(() => expect(1).toMatch(/\d/)).toThrowErrorMatchingInlineSnapshot(`[TypeError: .toMatch() expects to received a string, got number]`) + expect(() => expect(1).toMatch(/\d/)).toThrowErrorMatchingInlineSnapshot(`[TypeError: .toMatch() expects to receive a string, but got number]`) }) it('asymmetric matchers (jest style)', () => {