From f31ceb95167918489d354a8ce5a439f4b41707a7 Mon Sep 17 00:00:00 2001 From: Helmi Pradita Date: Sun, 8 Nov 2026 18:40:55 +0700 Subject: [PATCH] fix(expect): handle ArrayBuffers, DataViews, error constructors, and boxed primitives in toStrictEqual Refs #7387 --- expect/_equal.ts | 61 +++++++++++++++++++++++--- expect/_to_strict_equal_test.ts | 73 +++++++++++++++++++++++++++++++ expect/_utils.ts | 77 ++++++++++++++++----------------- 3 files changed, 164 insertions(+), 47 deletions(-) diff --git a/expect/_equal.ts b/expect/_equal.ts index 10f7875e2986..4ad45291ea3e 100644 --- a/expect/_equal.ts +++ b/expect/_equal.ts @@ -10,12 +10,29 @@ function isKeyedCollection(x: unknown): x is KeyedCollection { return x instanceof Set || x instanceof Map; } -function constructorsEqual(a: object, b: object) { +function constructorsEqual(a: object, b: object, strictCheck?: boolean) { + if (strictCheck) { + return a.constructor === b.constructor; + } return a.constructor === b.constructor || a.constructor === Object && !b.constructor || !a.constructor && b.constructor === Object; } +function compareArrayBuffers(a: ArrayBufferLike, b: ArrayBufferLike) { + const u8a = new Uint8Array(a); + const u8b = new Uint8Array(b); + if (u8a.byteLength !== u8b.byteLength) return false; + for (let i = 0; i < u8a.byteLength; i++) { + if (u8a[i] !== u8b[i]) return false; + } + return true; +} + +function sameValueZero(a: unknown, b: unknown) { + return a === b || Object.is(a, b); +} + function asymmetricEqual(a: unknown, b: unknown) { const asymmetricA = a instanceof AsymmetricMatcher; const asymmetricB = b instanceof AsymmetricMatcher; @@ -83,8 +100,34 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean { return aTime === bTime; } if (a instanceof Error && b instanceof Error) { + if (strictCheck && !constructorsEqual(a, b, strictCheck)) { + return false; + } return a.message === b.message; } + if ( + (a instanceof Number && b instanceof Number) || + (a instanceof Boolean && b instanceof Boolean) || + (typeof Symbol === "function" && a instanceof Symbol && + b instanceof Symbol) || + (typeof BigInt === "function" && a instanceof BigInt && + b instanceof BigInt) + ) { + return sameValueZero(a.valueOf(), b.valueOf()); + } + if ( + (a instanceof ArrayBuffer && b instanceof ArrayBuffer) || + (globalThis.SharedArrayBuffer && a instanceof SharedArrayBuffer && + b instanceof SharedArrayBuffer) + ) { + return compareArrayBuffers(a, b); + } + if (a instanceof DataView && b instanceof DataView) { + if (a.byteLength !== b.byteLength || a.byteOffset !== b.byteOffset) { + return false; + } + return compareArrayBuffers(a.buffer, b.buffer); + } if (typeof a === "number" && typeof b === "number") { return Number.isNaN(a) && Number.isNaN(b) || a === b; } @@ -99,7 +142,7 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean { return true; } if (a && typeof a === "object" && b && typeof b === "object") { - if (strictCheck && a && b && !constructorsEqual(a, b)) { + if (strictCheck && a && b && !constructorsEqual(a, b, strictCheck)) { return false; } if (a instanceof WeakMap || b instanceof WeakMap) { @@ -180,10 +223,12 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean { return true; } - let unmatchedEntries = a.size; + const unmatchedB = new Set(b.entries()); for (const [aKey, aValue] of a.entries()) { - for (const [bKey, bValue] of b.entries()) { + let matched = false; + for (const bEntry of unmatchedB) { + const [bKey, bValue] = bEntry; /* Given that Map keys can be references, we need * to ensure that they are also deeply equal */ @@ -191,15 +236,17 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean { if ( (aKey === aValue && bKey === bValue) || - (compare(aValue, bValue)) + compare(aValue, bValue) ) { - unmatchedEntries--; + unmatchedB.delete(bEntry); + matched = true; break; } } + if (!matched) return false; } - return unmatchedEntries === 0; + return unmatchedB.size === 0; } const merged = { ...a, ...b }; for ( diff --git a/expect/_to_strict_equal_test.ts b/expect/_to_strict_equal_test.ts index 3e411db399d8..0e46a720e64b 100644 --- a/expect/_to_strict_equal_test.ts +++ b/expect/_to_strict_equal_test.ts @@ -34,4 +34,77 @@ Deno.test("expect().toStrictEqual()", () => { assertThrows(() => { expect(new A()).toStrictEqual(new B()); }, AssertionError); + + const ab = (...b: number[]) => new Uint8Array(b).buffer; + expect(ab(1, 2)).toStrictEqual(ab(1, 2)); + expect(ab(1, 2)).not.toStrictEqual(ab(1, 3)); + assertThrows(() => { + expect(ab(1, 2)).toStrictEqual(ab(1, 3)); + }, AssertionError); + assertThrows(() => { + expect({ b: ab(1) }).toStrictEqual({ b: ab(2) }); + }, AssertionError); + + expect(new DataView(ab(1, 2))).toStrictEqual(new DataView(ab(1, 2))); + expect(new DataView(ab(1))).not.toStrictEqual(new DataView(ab(2))); + assertThrows(() => { + expect(new DataView(ab(1))).toStrictEqual(new DataView(ab(2))); + }, AssertionError); + + expect(new Error("m")).toStrictEqual(new Error("m")); + expect(new Error("m")).not.toStrictEqual(new TypeError("m")); + assertThrows(() => { + expect(new Error("m")).toStrictEqual(new TypeError("m")); + }, AssertionError); + + expect(Object.assign(Object.create(null), { a: 1 })).not.toStrictEqual({ + a: 1, + }); + assertThrows(() => { + expect(Object.assign(Object.create(null), { a: 1 })).toStrictEqual({ + a: 1, + }); + }, AssertionError); + + expect(new Number(1)).toStrictEqual(new Number(1)); + expect(new Number(1)).not.toStrictEqual(new Number(2)); + assertThrows(() => { + expect(new Number(1)).toStrictEqual(new Number(2)); + }, AssertionError); + + expect(new Boolean(true)).toStrictEqual(new Boolean(true)); + expect(new Boolean(true)).not.toStrictEqual(new Boolean(false)); + assertThrows(() => { + expect(new Boolean(true)).toStrictEqual(new Boolean(false)); + }, AssertionError); + + expect(Object(1n)).toStrictEqual(Object(1n)); + expect(Object(1n)).not.toStrictEqual(Object(2n)); + assertThrows(() => { + expect(Object(1n)).toStrictEqual(Object(2n)); + }, AssertionError); + + expect(new Set([{ a: 1 }, { a: 2 }])).toStrictEqual( + new Set([{ a: 2 }, { a: 1 }]), + ); + expect(new Set([{ a: 1 }, { a: 1 }])).not.toStrictEqual( + new Set([{ a: 1 }, { a: 2 }]), + ); + assertThrows(() => { + expect(new Set([{ a: 1 }, { a: 1 }])).toStrictEqual( + new Set([{ a: 1 }, { a: 2 }]), + ); + }, AssertionError); + + expect(new Map([[{ k: 1 }, 1], [{ k: 2 }, 2]])).toStrictEqual( + new Map([[{ k: 2 }, 2], [{ k: 1 }, 1]]), + ); + expect(new Map([[{ k: 1 }, 1], [{ k: 1 }, 1]])).not.toStrictEqual( + new Map([[{ k: 1 }, 1], [{ k: 2 }, 1]]), + ); + assertThrows(() => { + expect(new Map([[{ k: 1 }, 1], [{ k: 1 }, 1]])).toStrictEqual( + new Map([[{ k: 1 }, 1], [{ k: 2 }, 1]]), + ); + }, AssertionError); }); diff --git a/expect/_utils.ts b/expect/_utils.ts index b43a35cbb284..6caed9ebb76a 100644 --- a/expect/_utils.ts +++ b/expect/_utils.ts @@ -142,63 +142,60 @@ export function iterableEquality( return false; } else if (isA>("Set", a)) { let allFound = true; + const unmatchedB = new Set(b); for (const aValue of a) { - if (!b.has(aValue)) { - let has = false; - for (const bValue of b) { - const isEqual = equal(aValue, bValue, { - customTesters: filteredCustomTesters, - }); - if (isEqual === true) { - has = true; - } - } - - if (has === false) { - allFound = false; + let has = false; + for (const bValue of unmatchedB) { + const isEqual = equal(aValue, bValue, { + customTesters: filteredCustomTesters, + }); + if (isEqual === true) { + unmatchedB.delete(bValue); + has = true; break; } } + + if (!has) { + allFound = false; + break; + } } // Remove the first value from the stack of traversed values. aStack.pop(); bStack.pop(); - return allFound; + return allFound && unmatchedB.size === 0; } else if (isA>("Map", a)) { let allFound = true; + const unmatchedB = new Set<[unknown, unknown]>(b.entries()); for (const aEntry of a) { - if ( - !b.has(aEntry[0]) || - !equal(aEntry[1], b.get(aEntry[0]), { - customTesters: filteredCustomTesters, - }) - ) { - let has = false; - for (const bEntry of b) { - const matchedKey = equal( - aEntry[0], - bEntry[0], + let has = false; + for (const bEntry of unmatchedB) { + const matchedKey = equal( + aEntry[0], + bEntry[0], + { customTesters: filteredCustomTesters }, + ); + + let matchedValue = false; + if (matchedKey === true) { + matchedValue = equal( + aEntry[1], + bEntry[1], { customTesters: filteredCustomTesters }, ); - - let matchedValue = false; - if (matchedKey === true) { - matchedValue = equal( - aEntry[1], - bEntry[1], - { customTesters: filteredCustomTesters }, - ); - } - if (matchedValue === true) { - has = true; - } } - - if (has === false) { - allFound = false; + if (matchedValue === true) { + unmatchedB.delete(bEntry); + has = true; break; } } + + if (!has) { + allFound = false; + break; + } } // Remove the first value from the stack of traversed values. aStack.pop();