Skip to content

Commit a901f36

Browse files
author
Kent C. Dodds
committed
pass testConsole to TestEnvironment
1 parent e25eceb commit a901f36

10 files changed

Lines changed: 97 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
* `[jest-config]` fix unexpected condition to avoid infinite recursion in
66
Windows platform. ([#5161](https://github.com/facebook/jest/pull/5161))
7+
* `[jest-runner]` pass `console` to environments so jsdom can use the same
8+
console as the test. ([#5223](https://github.com/facebook/jest/issues/5223))
9+
* `[jest-environment-jsdom]` pass the `config.console` to a custom
10+
instance of `virtualConsole` so jsdom is using the same console as the
11+
test. ([#5223](https://github.com/facebook/jest/issues/5223))
712

813
### Features
914

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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`escape strings 1`] = `"one: \\\\'"`;
4+
5+
exports[`escape strings two 1`] = `"two: '\\""`;

integration_tests/snapshot-escape/__tests__/snapshot.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
// prettier-ignore
1010
test('escape strings', () => expect('one: \\\'').toMatchSnapshot());
11+
test('escape strings two', () => expect('two: \'"').toMatchSnapshot());

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {ModuleMocker} from 'jest-mock';
1313

1414
import {FakeTimers, installCommonGlobals} from 'jest-util';
1515
import mock from 'jest-mock';
16-
import {JSDOM} from 'jsdom';
16+
import {JSDOM, VirtualConsole} from 'jsdom';
1717

1818
class JSDOMEnvironment {
1919
dom: ?Object;
@@ -30,6 +30,9 @@ class JSDOMEnvironment {
3030
pretendToBeVisual: true,
3131
runScripts: 'dangerously',
3232
url: config.testURL,
33+
virtualConsole: new VirtualConsole().sendTo(
34+
config.console || console,
35+
),
3336
},
3437
config.testEnvironmentOptions,
3538
),

packages/jest-runner/src/run_test.js

Lines changed: 7 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,13 @@ async function runTestInternal(
9893
testConsole = new BufferedConsole();
9994
}
10095

96+
const environment = new TestEnvironment(
97+
Object.assign({}, config, {console: testConsole}),
98+
);
99+
const leakDetector = config.detectLeaks
100+
? new LeakDetector(environment)
101+
: null;
102+
101103
const cacheFS = {[path]: testSource};
102104
setGlobal(environment.global, 'console', testConsole);
103105

types/Config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export type ProjectConfig = {|
210210
cache: boolean,
211211
cacheDirectory: Path,
212212
clearMocks: boolean,
213+
console?: Object,
213214
coveragePathIgnorePatterns: Array<string>,
214215
cwd: Path,
215216
detectLeaks: boolean,

0 commit comments

Comments
 (0)