From 8765594e854cffc05cbb6b560e2413a4b333010e Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 5 Dec 2023 18:47:23 +0900 Subject: [PATCH] fix(expect): apply URL equality check only when URL is available --- packages/expect/src/jest-utils.ts | 2 +- test/core/test/jest-expect-no-url.test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/core/test/jest-expect-no-url.test.ts diff --git a/packages/expect/src/jest-utils.ts b/packages/expect/src/jest-utils.ts index 63aa295b5a20..a6d842043ae1 100644 --- a/packages/expect/src/jest-utils.ts +++ b/packages/expect/src/jest-utils.ts @@ -96,7 +96,7 @@ function eq( if (a instanceof Error && b instanceof Error) return a.message === b.message - if (a instanceof URL && b instanceof URL) + if (typeof URL === 'function' && a instanceof URL && b instanceof URL) return a.href === b.href if (Object.is(a, b)) diff --git a/test/core/test/jest-expect-no-url.test.ts b/test/core/test/jest-expect-no-url.test.ts new file mode 100644 index 000000000000..6f5ae6ce3211 --- /dev/null +++ b/test/core/test/jest-expect-no-url.test.ts @@ -0,0 +1,10 @@ +import { expect, it } from 'vitest' + +// simulate odd environment where URL is monkey-patched or not available +it('jest-expect-no-url', () => { + (globalThis as any).URL = {} + expect('hello').toEqual('hello') + + delete (globalThis as any).URL + expect('hello').toEqual('hello') +})