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: upgrade to JSDOM 16 #9606

Merged
merged 19 commits into from May 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Features

- `[jest-environment-jsdom]` [**BREAKING**] Upgrade `jsdom` to v16 ([#9606](https://github.com/facebook/jest/pull/9606))

### Fixes

- `[jest-circus]` [**BREAKING**] Fail tests if a test takes a done callback and have return values ([#9129](https://github.com/facebook/jest/pull/9129))
Expand Down
Expand Up @@ -20,6 +20,6 @@ exports[`prints console.logs when run with forceExit 3`] = `
console.log
Hey

at Object.<anonymous> (__tests__/a-banana.js:1:1)
at Object.log (__tests__/a-banana.js:1:30)

`;
16 changes: 1 addition & 15 deletions e2e/__tests__/consoleLogOutputWhenRunInBand.test.ts
Expand Up @@ -15,8 +15,6 @@ const DIR = path.resolve(__dirname, '../console-log-output-when-run-in-band');
beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

const nodeMajorVersion = Number(process.versions.node.split('.')[0]);

test('prints console.logs when run with forceExit', () => {
writeFiles(DIR, {
'__tests__/a-banana.js': `
Expand All @@ -25,26 +23,14 @@ test('prints console.logs when run with forceExit', () => {
'package.json': '{}',
});

const {stderr, exitCode, ...res} = runJest(DIR, [
const {stderr, stdout, exitCode} = runJest(DIR, [
'-i',
'--ci=false',
'--forceExit',
]);
let {stdout} = res;

const {rest, summary} = extractSummary(stderr);

if (nodeMajorVersion < 12) {
expect(stdout).toContain(
'at Object.<anonymous>.test (__tests__/a-banana.js:1:1)',
);

stdout = stdout.replace(
'at Object.<anonymous>.test (__tests__/a-banana.js:1:1)',
'at Object.<anonymous> (__tests__/a-banana.js:1:1)',
);
}

expect(exitCode).toBe(0);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
Expand Down
28 changes: 28 additions & 0 deletions e2e/__tests__/jestEnvironmentJsdom.test.ts
@@ -0,0 +1,28 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import {tmpdir} from 'os';
import {cleanup, writeFiles} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(tmpdir(), 'jest_environment_jsdom_test');

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

test('check is not leaking memory', () => {
writeFiles(DIR, {
'__tests__/a.test.js': `test('a', () => console.log('a'));`,
'__tests__/b.test.js': `test('b', () => console.log('b'));`,
'package.json': JSON.stringify({jest: {testEnvironment: 'jsdom'}}),
});

const {stderr} = runJest(DIR, ['--detect-leaks', '--runInBand']);
expect(stderr).toMatch(/PASS\s__tests__\/a.test.js/);
expect(stderr).toMatch(/PASS\s__tests__\/b.test.js/);
});
4 changes: 2 additions & 2 deletions packages/jest-environment-jsdom/package.json
Expand Up @@ -15,10 +15,10 @@
"@jest/types": "^25.5.0",
"jest-mock": "^25.5.0",
"jest-util": "^25.5.0",
"jsdom": "^15.2.1"
"jsdom": "^16.2.2"
},
"devDependencies": {
"@types/jsdom": "^12.2.4"
"@types/jsdom": "^16.2.1"
},
"engines": {
"node": ">= 10.14.2"
Expand Down
13 changes: 12 additions & 1 deletion packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -47,6 +47,10 @@ class JSDOMEnvironment implements JestEnvironment {
throw new Error('JSDOM did not return a Window object');
}

// 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;
Comment on lines +50 to +52
Copy link
Contributor

@ExE-Boss ExE-Boss Nov 28, 2020

Choose a reason for hiding this comment

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

This change is wrong, since it causes the newly‑created global to have the ArrayBuffer constructor from the outer realm, whereas jsdom/jsdom@3a4fd62 made it so that the inner realm has its own ArrayBuffer constructor and prototype chain.


Refs: #10786, #10854

Copy link
Member

Choose a reason for hiding this comment

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

@ExE-Boss great! Wanna send a PR?

Copy link
Contributor

Choose a reason for hiding this comment

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


// 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 Expand Up @@ -126,7 +130,14 @@ class JSDOMEnvironment implements JestEnvironment {

runScript<T = unknown>(script: Script): T | null {
if (this.dom) {
return this.dom.runVMScript(script) as any;
return script.runInContext(this.dom.getInternalVMContext());
}
return null;
}

getVmContext() {
if (this.dom) {
return this.dom.getInternalVMContext();
}
return null;
}
Expand Down