Skip to content

Commit

Permalink
feat(expect): compare URLs by href
Browse files Browse the repository at this point in the history
Change the "equal comparison" logic (as used by `toEqual`, `toStrictEqual`, etc.) to compare `URL` objects by their `href` property.

Closes #4614.
  • Loading branch information
kleinfreund committed Nov 28, 2023
1 parent 4166c41 commit 22fab12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ function eq(
if (a instanceof Error && b instanceof Error)
return a.message === b.message

if (a instanceof URL && b instanceof URL)
return a.href === b.href

if (Object.is(a, b))
return true

Expand Down
6 changes: 6 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ describe('jest-expect', () => {
expect(new Date(0)).toEqual(new Date(0))
expect(new Date('inValId')).toEqual(new Date('inValId'))

expect(new Error('message')).toEqual(new Error('message'))
expect(new Error('message')).not.toEqual(new Error('different message'))

expect(new URL('https://example.org')).toEqual(new URL('https://example.org'))
expect(new URL('https://example.org')).not.toEqual(new URL('https://different-example.org'))

expect(BigInt(1)).toBeGreaterThan(BigInt(0))
expect(1).toBeGreaterThan(BigInt(0))
expect(BigInt(1)).toBeGreaterThan(0)
Expand Down

0 comments on commit 22fab12

Please sign in to comment.