diff --git a/CHANGELOG.md b/CHANGELOG.md index 64178c38b6f2..4e91d3b6d9d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Fixes - `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109)) +- `[@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 diff --git a/packages/expect-utils/src/__tests__/utils.test.ts b/packages/expect-utils/src/__tests__/utils.test.ts index ce95e208599d..c4d7893e4298 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 a68354c4882e..c666e6697d9b 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -390,12 +390,17 @@ export const arrayBufferEquality = ( a: unknown, b: unknown, ): boolean | undefined => { - 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); } - const dataViewA = new DataView(a); - const dataViewB = new DataView(b); + if (!(dataViewA instanceof DataView && dataViewB instanceof DataView)) { + return undefined; + } // Buffers are not equal when they do not have the same byte length if (dataViewA.byteLength !== dataViewB.byteLength) {