Skip to content
This repository has been archived by the owner on Dec 27, 2020. It is now read-only.

Commit

Permalink
Upgrade Jest
Browse files Browse the repository at this point in the history
There was an unfortunate quirk of upgrading Jest. They changed their
node globals to include an `ArrayBuffer` override, synchronizing the
value with `Buffer.from([]).buffer.constructor`. I didn't dive too deep,
but what became clear was `new Uint8Array(0).buffer` and
`Buffer.from([]).buffer` reference different `ArrayBuffer` constructors.
This failed the tests because I was comparing the constructor from two
separate realms.

See: jestjs/jest#7626

This affects production code, not just tests. Because the global
constructor was different between environments, it forced me to check by
string tag instead of instance. That's probably a good change anyway.

I manually checked the string tag in Node, IE11, Firefox, and Chrome.
  • Loading branch information
PsychoLlama committed May 22, 2020
1 parent 12927c1 commit ffae625
Show file tree
Hide file tree
Showing 7 changed files with 796 additions and 760 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
## Unreleased
### Removed
- Dropped support for unmaintained node versions (node < 8).
- Dropped support for unmaintained node versions (node < 10).

### Fixed
- Improve cross-realm detection of `ArrayBuffer` instances.

## 0.3.1
### Changed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.5",
"jest": "^25.1.0",
"jest": "^26.0.1",
"lint-staged": "^10.2.6",
"prettier": "^2.0.5"
},
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { small, large } from '../strings';
import json from '../index';
import { NativeArrayBuffer } from '../test-utils';

const originalSecretKey = json.SECRET_KEY;

Expand Down Expand Up @@ -27,7 +28,7 @@ describe('bin-json', () => {
it('encodes to an ArrayBuffer', () => {
const buffer = json.encode('data');

expect(buffer).toEqual(expect.any(ArrayBuffer));
expect(buffer).toBeInstanceOf(NativeArrayBuffer);
});

it('works with boolean values', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/packager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readFileSync } from 'fs';

import { pack, unpack } from '../packager';
import { small, large } from '../strings';
import { NativeArrayBuffer } from '../test-utils';

const createBuffer = (arrayBuffer) => Buffer.from(arrayBuffer);

Expand All @@ -10,7 +11,7 @@ describe('Package', () => {
const data = Buffer.from('something');
const buffer = pack([data]);

expect(buffer).toEqual(expect.any(ArrayBuffer));
expect(buffer).toBeInstanceOf(NativeArrayBuffer);

const expected = Buffer.from(`${data.byteLength}\0something`);
expect(Buffer.from(buffer)).toEqual(expected);
Expand Down Expand Up @@ -53,7 +54,7 @@ describe('Package', () => {
const buffers = unpack(buffer);

expect(buffers).toEqual(expect.any(Array));
buffers.map((buffer) => expect(buffer).toEqual(expect.any(ArrayBuffer)));
buffers.map((buffer) => expect(buffer).toBeInstanceOf(NativeArrayBuffer));
expect(buffers.map(createBuffer)).toEqual([buff1, buff2]);
});

Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { pack, unpack } from './packager';
import { large } from './strings';

// Safer than comparing by reference - cross-realm constructors can bite you.
const ARRAY_BUFFER_TAG = '[object ArrayBuffer]';

let BufferType = null;

/**
Expand Down Expand Up @@ -36,7 +39,7 @@ const isBuffer = (data) => {
}

// Typed arrays.
if (data.buffer instanceof ArrayBuffer) {
if (Object.prototype.toString.call(data.buffer) === ARRAY_BUFFER_TAG) {
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is not the same as `global.ArrayBuffer`. See:
// https://github.com/facebook/jest/issues/7780
export const NativeArrayBuffer = new Uint8Array().buffer.constructor;
Loading

0 comments on commit ffae625

Please sign in to comment.