Skip to content

Commit

Permalink
fix(expect-util): fix comparison of DataView (#14408)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Sep 20, 2023
1 parent c13c649 commit 4f35f1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
- `[pretty-format]` [**BREAKING**] Print `ArrayBuffer` and `DataView` correctly ([#14290](https://github.com/facebook/jest/pull/14290))

Expand Down
12 changes: 12 additions & 0 deletions packages/expect-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
13 changes: 9 additions & 4 deletions packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4f35f1f

Please sign in to comment.