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(jest‑environment‑jsdom): Use inner realm’s ArrayBuffer constructor #10885

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion e2e/env-test/__tests__/equivalent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,31 @@
*/
'use strict';

const {isArrayBuffer} = require('util').types;
const isJSDOM =
typeof window !== 'undefined' && typeof document !== 'undefined';

test('Buffer', () => {
const bufFromArray = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
expect(bufFromArray.buffer instanceof ArrayBuffer).toBeTruthy();
expect(isArrayBuffer(bufFromArray.buffer)).toBeTruthy();
const bufFromArrayBuffer = Buffer.from(new ArrayBuffer(6));
expect(bufFromArrayBuffer.buffer instanceof ArrayBuffer).toBeTruthy();
});

test.each([
['Int8Array', Int8Array, isJSDOM],
['Int16Array', Int16Array, isJSDOM],
['Int32Array', Int32Array, isJSDOM],
['Uint8Array', Uint8Array],
['Uint8ClampedArray', Uint8ClampedArray, isJSDOM],
['Uint16Array', Uint16Array, isJSDOM],
['Uint32Array', Uint32Array, isJSDOM],
['Float32Array', Float32Array, isJSDOM],
['Float64Array', Float64Array, isJSDOM],
])('%s', (name, ctor, testInstanceof = true) => {
const typedArray = ctor.of();
expect(isArrayBuffer(typedArray.buffer)).toBeTruthy();
if (testInstanceof) {
expect(typedArray.buffer instanceof ArrayBuffer).toBeTruthy();
}
});
4 changes: 0 additions & 4 deletions packages/jest-environment-jsdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class JSDOMEnvironment implements JestEnvironment {
// for "universal" code (code should use `globalThis`)
global.global = global;

// In the `jsdom@16`, ArrayBuffer was not added to Window, ref: https://github.com/jsdom/jsdom/commit/3a4fd6258e6b13e9cf8341ddba60a06b9b5c7b5b
// Install ArrayBuffer to Window to fix it. Make sure the test is passed, ref: https://github.com/facebook/jest/pull/7626
global.ArrayBuffer = ArrayBuffer;

// Node's error-message stack size is limited at 10, but it's pretty useful
// to see more than that when a test fails.
this.global.Error.stackTraceLimit = 100;
Expand Down