Skip to content

Commit

Permalink
feat(expect): Add equality support for Array Buffers to ToStrictEqual…
Browse files Browse the repository at this point in the history
… method (#11805)
  • Loading branch information
khai93 authored Sep 29, 2021
1 parent 784b6a7 commit 94734e1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 94734e1

Please sign in to comment.