Skip to content

Commit 674851c

Browse files
authoredApr 2, 2024··
fix: the value received by toMatch should be a string (#5428)
1 parent d8a434a commit 674851c

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed
 

‎packages/expect/src/jest-expect.ts

+3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
171171
})
172172
def('toMatch', function (expected: string | RegExp) {
173173
const actual = this._obj as string
174+
if (typeof actual !== 'string')
175+
throw new TypeError(`.toMatch() expects to receive a string, but got ${typeof actual}`)
176+
174177
return this.assert(
175178
typeof expected === 'string'
176179
? actual.includes(expected)

‎test/core/test/jest-expect.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ describe('jest-expect', () => {
109109
expect(0.2 + 0.1).not.toBe(0.3)
110110
expect(0.2 + 0.1).toBeCloseTo(0.3, 5)
111111
expect(0.2 + 0.1).not.toBeCloseTo(0.3, 100) // expect.closeTo will fail in chai
112+
113+
expect(() => expect(1).toMatch(/\d/)).toThrowErrorMatchingInlineSnapshot(`[TypeError: .toMatch() expects to receive a string, but got number]`)
112114
})
113115

114116
it('asymmetric matchers (jest style)', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.