Skip to content

Commit d62e9ab

Browse files
author
Kent C. Dodds
committed
pass testConsole to TestEnvironment
1 parent 9cbc26b commit d62e9ab

8 files changed

Lines changed: 96 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
* `[docs]` Add documentation for .toHaveProperty matcher to accept the keyPath
2525
argument as an array of properties/indices.
2626
([#5220](https://github.com/facebook/jest/pull/5220))
27+
* `[jest-runner]` test environments are now passed a new `options` parameter.
28+
Currently this only has the `console` which is the test console that Jest will
29+
expose to tests. ([#5223](https://github.com/facebook/jest/issues/5223))
30+
* `[jest-environment-jsdom]` pass the `options.console` to a custom
31+
instance of `virtualConsole` so jsdom is using the same console as the
32+
test. ([#5223](https://github.com/facebook/jest/issues/5223))
2733

2834
### Chore & Maintenance
2935

integration_tests/__tests__/__snapshots__/console.test.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,21 @@ Snapshots: 0 total
7373
Time: <<REPLACED>>
7474
"
7575
`;
76+
77+
exports[`the jsdom console is the same as the test console 1`] = `""`;
78+
79+
exports[`the jsdom console is the same as the test console 2`] = `
80+
"PASS __tests__/console.test.js
81+
✓ can mock console.error calls from jsdom
82+
83+
"
84+
`;
85+
86+
exports[`the jsdom console is the same as the test console 3`] = `
87+
"Test Suites: 1 passed, 1 total
88+
Tests: 1 passed, 1 total
89+
Snapshots: 0 total
90+
Time: <<REPLACED>>
91+
Ran all test suites.
92+
"
93+
`;

integration_tests/__tests__/console.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,14 @@ test('does not print to console with --silent', () => {
5454
expect(rest).toMatchSnapshot();
5555
expect(summary).toMatchSnapshot();
5656
});
57+
58+
// issue: https://github.com/facebook/jest/issues/5223
59+
test('the jsdom console is the same as the test console', () => {
60+
const {stderr, stdout, status} = runJest('console-jsdom');
61+
const {summary, rest} = extractSummary(stderr);
62+
63+
expect(status).toBe(0);
64+
expect(stdout).toMatchSnapshot();
65+
expect(rest).toMatchSnapshot();
66+
expect(summary).toMatchSnapshot();
67+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
/* eslint-env browser */
8+
'use strict';
9+
10+
beforeEach(() => {
11+
jest.spyOn(console, 'error');
12+
console.error.mockImplementation(() => {});
13+
});
14+
15+
afterEach(() => {
16+
console.error.mockRestore();
17+
});
18+
19+
test('can mock console.error calls from jsdom', () => {
20+
// copied and modified for tests from:
21+
// https://github.com/facebook/react/blob/46b3c3e4ae0d52565f7ed2344036a22016781ca0/packages/shared/invokeGuardedCallback.js#L62-L147
22+
const fakeNode = document.createElement('react');
23+
24+
const evt = document.createEvent('Event');
25+
const evtType = 'react-invokeguardedcallback';
26+
function callCallback() {
27+
fakeNode.removeEventListener(evtType, callCallback, false);
28+
throw new Error('this is an error in an event callback');
29+
}
30+
31+
function onError(event) {}
32+
33+
window.addEventListener('error', onError);
34+
fakeNode.addEventListener(evtType, callCallback, false);
35+
evt.initEvent(evtType, false, false);
36+
fakeNode.dispatchEvent(evt);
37+
window.removeEventListener('error', onError);
38+
39+
expect(console.error).toHaveBeenCalledTimes(1);
40+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "jsdom"
4+
}
5+
}

packages/jest-environment-jsdom/src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
import type {Script} from 'vm';
1010
import type {ProjectConfig} from 'types/Config';
11+
import type {EnvironmentOptions} from 'types/Environment';
1112
import type {Global} from 'types/Global';
1213
import type {ModuleMocker} from 'jest-mock';
1314

1415
import {FakeTimers, installCommonGlobals} from 'jest-util';
1516
import mock from 'jest-mock';
16-
import {JSDOM} from 'jsdom';
17+
import {JSDOM, VirtualConsole} from 'jsdom';
1718

1819
class JSDOMEnvironment {
1920
dom: ?Object;
@@ -22,14 +23,17 @@ class JSDOMEnvironment {
2223
errorEventListener: ?Function;
2324
moduleMocker: ?ModuleMocker;
2425

25-
constructor(config: ProjectConfig) {
26+
constructor(config: ProjectConfig, options: EnvironmentOptions = {}) {
2627
this.dom = new JSDOM(
2728
'<!DOCTYPE html>',
2829
Object.assign(
2930
{
3031
pretendToBeVisual: true,
3132
runScripts: 'dangerously',
3233
url: config.testURL,
34+
virtualConsole: new VirtualConsole().sendTo(
35+
options.console || console,
36+
),
3337
},
3438
config.testEnvironmentOptions,
3539
),

packages/jest-runner/src/run_test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ async function runTestInternal(
7474
RuntimeClass,
7575
>);
7676

77-
const environment = new TestEnvironment(config);
78-
const leakDetector = config.detectLeaks
79-
? new LeakDetector(environment)
80-
: null;
81-
8277
const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
8378
const consoleFormatter = (type, message) =>
8479
getConsoleOutput(
@@ -98,6 +93,11 @@ async function runTestInternal(
9893
testConsole = new BufferedConsole();
9994
}
10095

96+
const environment = new TestEnvironment(config, {console: testConsole});
97+
const leakDetector = config.detectLeaks
98+
? new LeakDetector(environment)
99+
: null;
100+
101101
const cacheFS = {[path]: testSource};
102102
setGlobal(environment.global, 'console', testConsole);
103103

types/Environment.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import type {Global} from './Global';
1212
import type {Script} from 'vm';
1313
import type {ModuleMocker} from 'jest-mock';
1414

15+
export type EnvironmentOptions = {
16+
console?: Object,
17+
};
18+
1519
declare class $JestEnvironment {
16-
constructor(config: ProjectConfig): void;
20+
constructor(config: ProjectConfig, options?: EnvironmentOptions): void;
1721
runScript(script: Script): any;
1822
global: Global;
1923
fakeTimers: {

0 commit comments

Comments
 (0)