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

[WIP] Fix Console Logging in jsdom Env #2457

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions integration_tests/__tests__/__snapshots__/console-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,35 @@ Time: <<REPLACED>>
Ran all test suites.
"
`;

exports[`test console printing with jsdom env 1`] = `
" console.log __tests__/console-test.js:11
This is a log message.

console.info __tests__/console-test.js:13
This is an info message.

console.warn __tests__/console-test.js:15
This is a warning message.

console.error __tests__/console-test.js:17
This is an error message.

"
`;

exports[`test console printing with jsdom env 2`] = `
" PASS __tests__/console-test.js
✓ works just fine

"
`;

exports[`test console printing with jsdom env 3`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
"
`;
11 changes: 11 additions & 0 deletions integration_tests/__tests__/console-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ test('console printing with --verbose', () => {
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});

test('console printing with jsdom env', () => {
const {stderr, stdout, status} =
runJest('console', ['--verbose', '--env=jsdom']);
const {summary, rest} = extractSummary(stderr);

expect(status).toBe(0);
expect(stdout).toMatchSnapshot();
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
26 changes: 14 additions & 12 deletions packages/jest-cli/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,25 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
/* $FlowFixMe */
const ModuleLoader = require(config.moduleLoader || 'jest-runtime');

const env = new TestEnvironment(config);
const TestConsole =
config.verbose
? Console
: (config.silent
? NullConsole
: BufferedConsole
);
const testConsole = env.global.console = new TestConsole(
config.useStderr ? process.stderr : process.stdout,
process.stderr,
(type, message) => getConsoleOutput(
config.rootDir,
!!config.verbose,
// 4 = the console call is burried 4 stack frames deep
BufferedConsole.write([], type, message, 4),
),
);
const createCustomConsole = (stackFrameDepth: number): Object => {
Copy link
Member

Choose a reason for hiding this comment

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

I think createConsole should be good enough of a name, it implies that it is "custom".

return new TestConsole(
config.useStderr ? process.stderr : process.stdout,
process.stderr,
(type, message) => getConsoleOutput(
config.rootDir,
!!config.verbose,
BufferedConsole.write([], type, message, stackFrameDepth),
),
);
};
const env = new TestEnvironment(config, createCustomConsole);
const runtime = new ModuleLoader(config, env, resolver);
const start = Date.now();
return TestRunner(config, env, runtime, path)
Expand All @@ -56,7 +57,8 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
result.perfStats = {end: Date.now(), start};
result.testFilePath = path;
result.coverage = runtime.getAllCoverageInfo();
result.console = testConsole.getBuffer();
result.console = env.console && env.console.getBuffer ?
env.console.getBuffer() : null;
result.skipped = testCount === result.numPendingTests;
return result;
})
Expand Down
12 changes: 10 additions & 2 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
'use strict';

import type {Console} from 'console';
import type {Config} from 'types/Config';
import type {Global} from 'types/Global';
import type {Script} from 'vm';
Expand All @@ -18,15 +19,21 @@ const ModuleMocker = require('jest-mock');

class JSDOMEnvironment {

console: ?Console;
document: ?Object;
fakeTimers: ?FakeTimers;
global: ?Global;
moduleMocker: ?ModuleMocker;

constructor(config: Config): void {
constructor(config: Config,
createCustomConsole: (x: number) => Console): void {
// lazy require
this.document = require('jsdom').jsdom(/* markup */undefined, {
const jsdom = require('jsdom');
const console = this.console = createCustomConsole(8);
const virtualConsole = jsdom.createVirtualConsole().sendTo(console);
this.document = jsdom.jsdom(/* markup */undefined, {
url: config.testURL,
virtualConsole,
});
const global = this.global = this.document.defaultView;
// Node's error-message stack size is limited at 10, but it's pretty useful
Expand All @@ -45,6 +52,7 @@ class JSDOMEnvironment {
if (this.global) {
this.global.close();
}
this.console = null;
this.global = null;
this.document = null;
this.fakeTimers = null;
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
'use strict';

import type {Console} from 'console';
import type {Config} from 'types/Config';
import type {Global} from 'types/Global';
import type {Script} from 'vm';
Expand All @@ -20,14 +21,16 @@ const vm = require('vm');

class NodeEnvironment {

console: ?Console;
context: ?vm$Context;
fakeTimers: ?FakeTimers;
global: ?Global;
moduleMocker: ?ModuleMocker;

constructor(config: Config) {
constructor(config: Config, createCustomConsole: (x: number) => Console) {
this.context = vm.createContext();
const global = this.global = vm.runInContext('this', this.context);
global.console = this.console = createCustomConsole(4);
global.global = global;
global.clearInterval = clearInterval;
global.clearTimeout = clearTimeout;
Expand All @@ -43,6 +46,7 @@ class NodeEnvironment {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
this.console = null;
this.context = null;
this.fakeTimers = null;
}
Expand Down