From 02c92f3f6d4383b6db575849674feda2414c674e Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 14 Aug 2023 15:41:30 +0800 Subject: [PATCH 1/4] fix(expect-util): DataView comparison does not work --- packages/expect-utils/src/__tests__/utils.test.ts | 12 ++++++++++++ packages/expect-utils/src/utils.ts | 14 +++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/expect-utils/src/__tests__/utils.test.ts b/packages/expect-utils/src/__tests__/utils.test.ts index fd1e2c588ed5..9bfaa69389fe 100644 --- a/packages/expect-utils/src/__tests__/utils.test.ts +++ b/packages/expect-utils/src/__tests__/utils.test.ts @@ -571,4 +571,16 @@ describe('arrayBufferEquality', () => { const b = Uint8Array.from([1, 2]).buffer; expect(arrayBufferEquality(a, b)).toBeTruthy(); }); + + test('returns true when given matching DataView', () => { + const a = new DataView(Uint8Array.from([1, 2, 3]).buffer); + const b = new DataView(Uint8Array.from([1, 2, 3]).buffer); + expect(arrayBufferEquality(a, b)).toBeTruthy(); + }); + + test('returns false when given matching DataView', () => { + const a = new DataView(Uint8Array.from([1, 2, 3]).buffer); + const b = new DataView(Uint8Array.from([3, 2, 1]).buffer); + expect(arrayBufferEquality(a, b)).toBeFalsy(); + }); }); diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts index 62c3085a56fa..be81b99d9ea9 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -390,12 +390,16 @@ export const arrayBufferEquality = ( a: unknown, b: unknown, ): boolean | undefined => { - if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) { - return undefined; - } + let dataViewA = a as DataView; + let dataViewB = b as DataView; - const dataViewA = new DataView(a); - const dataViewB = new DataView(b); + if (!(a instanceof DataView && b instanceof DataView)) { + if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) + return undefined; + + dataViewA = new DataView(a); + dataViewB = new DataView(b); + } // Buffers are not equal when they do not have the same byte length if (dataViewA.byteLength !== dataViewB.byteLength) { From 4b083db18d94a90fa5d7cf29aa18f01d42d17bec Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 14 Aug 2023 16:18:10 +0800 Subject: [PATCH 2/4] refactoring: reimplementation, never use `as`. --- packages/expect-utils/src/utils.ts | 31 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts index be81b99d9ea9..44e595f9e8f1 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -390,30 +390,31 @@ export const arrayBufferEquality = ( a: unknown, b: unknown, ): boolean | undefined => { - let dataViewA = a as DataView; - let dataViewB = b as DataView; - - if (!(a instanceof DataView && b instanceof DataView)) { - if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) - return undefined; + let dataViewA = a; + let dataViewB = b; + if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) { dataViewA = new DataView(a); dataViewB = new DataView(b); } - // Buffers are not equal when they do not have the same byte length - if (dataViewA.byteLength !== dataViewB.byteLength) { - return false; - } - - // Check if every byte value is equal to each other - for (let i = 0; i < dataViewA.byteLength; i++) { - if (dataViewA.getUint8(i) !== dataViewB.getUint8(i)) { + if (dataViewA instanceof DataView && dataViewB instanceof DataView) { + // Buffers are not equal when they do not have the same byte length + if (dataViewA.byteLength !== dataViewB.byteLength) { return false; } + + // Check if every byte value is equal to each other + for (let i = 0; i < dataViewA.byteLength; i++) { + if (dataViewA.getUint8(i) !== dataViewB.getUint8(i)) { + return false; + } + } + + return true; } - return true; + return undefined; }; export const sparseArrayEquality = ( From c201c66a1a83c261ff49a32c5e7fc694fa1b5a83 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 14 Aug 2023 16:24:39 +0800 Subject: [PATCH 3/4] feat: improve --- packages/expect-utils/src/utils.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts index 44e595f9e8f1..0283273944fd 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -398,23 +398,23 @@ export const arrayBufferEquality = ( dataViewB = new DataView(b); } - if (dataViewA instanceof DataView && dataViewB instanceof DataView) { - // Buffers are not equal when they do not have the same byte length - if (dataViewA.byteLength !== dataViewB.byteLength) { - return false; - } + if (!(dataViewA instanceof DataView && dataViewB instanceof DataView)) { + return undefined; + } - // Check if every byte value is equal to each other - for (let i = 0; i < dataViewA.byteLength; i++) { - if (dataViewA.getUint8(i) !== dataViewB.getUint8(i)) { - return false; - } - } + // Buffers are not equal when they do not have the same byte length + if (dataViewA.byteLength !== dataViewB.byteLength) { + return false; + } - return true; + // Check if every byte value is equal to each other + for (let i = 0; i < dataViewA.byteLength; i++) { + if (dataViewA.getUint8(i) !== dataViewB.getUint8(i)) { + return false; + } } - return undefined; + return true; }; export const sparseArrayEquality = ( From 32574757bd8af7da5e85c7421b705e06651cb2f0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 20 Sep 2023 09:19:07 +0200 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17e27fabd77b..3eea9f19d8cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixes +- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408)) - `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526)) ### Performance