Skip to content

Commit

Permalink
fix: the value received by toMatch should be a string (#5428)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea authored Apr 2, 2024
1 parent d8a434a commit 674851c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 receive a string, but got ${typeof actual}`)

return this.assert(
typeof expected === 'string'
? actual.includes(expected)
Expand Down
2 changes: 2 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 receive a string, but got number]`)
})

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

0 comments on commit 674851c

Please sign in to comment.