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

feat(expect): Add equality support for Array Buffers to ToStrictEqual method #11805

Merged
merged 13 commits into from Sep 29, 2021
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Features

- `[expect]` Add equality checks for Array Buffers in `expect.ToStrictEqual()` ([#11805](https://github.com/facebook/jest/pull/11805))

### Fixes

### Chore & Maintenance
Expand Down
24 changes: 24 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -426,6 +426,30 @@ describe('.toStrictEqual()', () => {
it('does not pass when equally sparse arrays have different values', () => {
expect([, 1]).not.toStrictEqual([, 2]);
});

it('does not pass when ArrayBuffers are not equal', () => {
expect(Uint8Array.from([1, 2]).buffer).not.toStrictEqual(
Uint8Array.from([0, 0]).buffer,
);
expect(Uint8Array.from([2, 1]).buffer).not.toStrictEqual(
Uint8Array.from([2, 2]).buffer,
);
expect(Uint8Array.from([]).buffer).not.toStrictEqual(
Uint8Array.from([1]).buffer,
);
});

it('passes for matching buffers', () => {
expect(Uint8Array.from([1]).buffer).toStrictEqual(
Uint8Array.from([1]).buffer,
);
expect(Uint8Array.from([]).buffer).toStrictEqual(
Uint8Array.from([]).buffer,
);
expect(Uint8Array.from([9, 3]).buffer).toStrictEqual(
Uint8Array.from([9, 3]).buffer,
);
});
/* eslint-enable */
});

Expand Down
21 changes: 21 additions & 0 deletions packages/expect/src/__tests__/utils.test.ts
Expand Up @@ -8,6 +8,7 @@

import {stringify} from 'jest-matcher-utils';
import {
arrayBufferEquality,
emptyObject,
getObjectSubset,
getPath,
Expand Down Expand Up @@ -466,3 +467,23 @@ describe('iterableEquality', () => {
expect(iterableEquality(a, b)).toBe(true);
});
});

describe('arrayBufferEquality', () => {
test('returns undefined if given a non instance of ArrayBuffer', () => {
expect(arrayBufferEquality(2, 's')).toBeUndefined();
expect(arrayBufferEquality(undefined, 2)).toBeUndefined();
expect(arrayBufferEquality(new Date(), new ArrayBuffer(2))).toBeUndefined();
});

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();
});

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();
});
});
2 changes: 2 additions & 0 deletions packages/expect/src/matchers.ts
Expand Up @@ -40,6 +40,7 @@ import {
} from './print';
import type {MatcherState, MatchersObject} from './types';
import {
arrayBufferEquality,
getObjectSubset,
getPath,
iterableEquality,
Expand All @@ -61,6 +62,7 @@ const toStrictEqualTesters = [
iterableEquality,
typeEquality,
sparseArrayEquality,
arrayBufferEquality,
];

type ContainIterable =
Expand Down
26 changes: 26 additions & 0 deletions packages/expect/src/utils.ts
Expand Up @@ -319,6 +319,32 @@ export const typeEquality = (a: any, b: any): boolean | undefined => {
return false;
};

export const arrayBufferEquality = (
a: unknown,
b: unknown,
): boolean | undefined => {
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) {
return undefined;
}

const dataViewA = new DataView(a);
const 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)) {
return false;
}
}

return true;
};

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