Skip to content

Commit

Permalink
feat: upgrade to JSDOM 16 (#9606)
Browse files Browse the repository at this point in the history
  • Loading branch information
lh0x00 committed May 2, 2020
1 parent 77f7145 commit bc31829
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 128 deletions.
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;

// 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

0 comments on commit bc31829

Please sign in to comment.