Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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;
Comment on lines +393 to +394
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From semantic point of view it feels wrong to name a variable dataView at the moment it is still not clear if that is a DataView instance.

Also the shape of the logic is very close to: #14408 (comment). Did I miss something in that proposal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of your implementation is also correct, except that I wanted to reduce the logical judgments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow.


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