Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly compare TypedArrays of all types #15178

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -22,6 +22,7 @@
- `[@jest/environment-jsdom-abstract]` Introduce new package which abstracts over the `jsdom` environment, allowing usage of custom versions of JSDOM ([#14717](https://github.com/jestjs/jest/pull/14717))
- `[jest-environment-node]` Update jest environment with dispose symbols `Symbol` ([#14888](https://github.com/jestjs/jest/pull/14888) & [#14909](https://github.com/jestjs/jest/pull/14909))
- `[expect, @jest/expect]` [**BREAKING**] Add type inference for function parameters in `CalledWith` assertions ([#15129](https://github.com/facebook/jest/pull/15129))
- `[@jest/expect-utils]` Properly compare all types of `TypedArray`s ([#15178](https://github.com/facebook/jest/pull/15178))
- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))
- `[@jest/fake-timers]` Exposing new modern timers function `advanceTimersToFrame()` which advances all timers by the needed milliseconds to execute callbacks currently scheduled with `requestAnimationFrame` ([#14598](https://github.com/jestjs/jest/pull/14598))
- `[jest-matcher-utils]` Add `SERIALIZABLE_PROPERTIES` to allow custom serialization of objects ([#14893](https://github.com/jestjs/jest/pull/14893))
Expand Down
30 changes: 24 additions & 6 deletions packages/expect-utils/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,37 +670,55 @@ describe('arrayBufferEquality', () => {
test('returns false when given non-matching buffers', () => {
const a = Uint8Array.from([2, 4]).buffer;
const b = Uint16Array.from([1, 7]).buffer;
expect(arrayBufferEquality(a, b)).not.toBeTruthy();
expect(arrayBufferEquality(a, b)).toBe(false);
Copy link
Member Author

Choose a reason for hiding this comment

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

this was a separate bug - it returned undefined, not false since Uint16Array.from([1, 7]).buffer instanceOf ArrayBuffer for some reason is `false.

});

test('returns false when given matching buffers of different byte length', () => {
const a = Uint8Array.from([1, 2]).buffer;
const b = Uint16Array.from([1, 2]).buffer;
expect(arrayBufferEquality(a, b)).toBe(false);
});

test('returns true when given matching buffers', () => {
const a = Uint8Array.from([1, 2]).buffer;
const b = Uint8Array.from([1, 2]).buffer;
expect(arrayBufferEquality(a, b)).toBeTruthy();
expect(arrayBufferEquality(a, b)).toBe(true);
});

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();
expect(arrayBufferEquality(a, b)).toBe(true);
});

test('returns false when given non-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();
expect(arrayBufferEquality(a, b)).toBe(false);
});

test('returns true when given matching Float64Array', () => {
const a = Float64Array.from(Array.from({length: 10}));
const b = Float64Array.from(Array.from({length: 10}));
expect(arrayBufferEquality(a, b)).toBe(true);
});

test('returns false when given non-matching Float64Array', () => {
const a = Float64Array.from(Array.from({length: 10}));
const b = Float64Array.from(Array.from({length: 100}));
expect(arrayBufferEquality(a, b)).toBe(false);
});

test('returns true when given matching URL', () => {
const a = new URL('https://jestjs.io/');
const b = new URL('https://jestjs.io/');
expect(equals(a, b)).toBeTruthy();
expect(equals(a, b)).toBe(true);
});

test('returns false when given non-matching URL', () => {
const a = new URL('https://jestjs.io/docs/getting-started');
const b = new URL('https://jestjs.io/docs/getting-started#using-babel');
expect(equals(a, b)).toBeFalsy();
expect(equals(a, b)).toBe(false);
});
});

Expand Down
11 changes: 10 additions & 1 deletion packages/expect-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export const iterableEquality = (
typeof b !== 'object' ||
Array.isArray(a) ||
Array.isArray(b) ||
ArrayBuffer.isView(a) ||
ArrayBuffer.isView(b) ||
!hasIterator(a) ||
!hasIterator(b)
) {
Expand Down Expand Up @@ -413,9 +415,12 @@ export const arrayBufferEquality = (
let dataViewA = a;
let dataViewB = b;

if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) {
if (isArrayBuffer(a) && isArrayBuffer(b)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

dataViewA = new DataView(a);
dataViewB = new DataView(b);
} else if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
dataViewA = new DataView(a.buffer, a.byteOffset, a.byteLength);
dataViewB = new DataView(b.buffer, b.byteOffset, b.byteLength);
}

if (!(dataViewA instanceof DataView && dataViewB instanceof DataView)) {
Expand All @@ -437,6 +442,10 @@ export const arrayBufferEquality = (
return true;
};

function isArrayBuffer(obj: unknown): obj is ArrayBuffer {
return Object.prototype.toString.call(obj) === '[object ArrayBuffer]';
}

export const sparseArrayEquality = (
a: unknown,
b: unknown,
Expand Down
Loading