Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/jest-cli/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import type {TestResult} from 'types/TestResult';
import type Resolver from 'jest-resolve';

const BufferedConsole = require('./lib/BufferedConsole');
const Console = require('jest-util').Console;
const NullConsole = require('jest-util').NullConsole;

const {
Console,
NullConsole,
setGlobal,
} = require('jest-util');
const getConsoleOutput = require('./reporters/getConsoleOutput');

function runTest(path: Path, config: Config, resolver: Resolver) {
Expand All @@ -35,7 +37,7 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
? NullConsole
: BufferedConsole
);
const testConsole = env.global.console = new TestConsole(
const testConsole = new TestConsole(
config.useStderr ? process.stderr : process.stdout,
process.stderr,
(type, message) => getConsoleOutput(
Expand All @@ -45,6 +47,7 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
BufferedConsole.write([], type, message, 4),
),
);
setGlobal(env.global, 'console', testConsole);
const runtime = new ModuleLoader(config, env, resolver);
const start = Date.now();
return TestRunner(config, env, runtime, path)
Expand Down
15 changes: 11 additions & 4 deletions packages/jest-runtime/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ const os = require('os');
const path = require('path');
const yargs = require('yargs');

const Console = require('jest-util').Console;
const getPackageRoot = require('jest-util').getPackageRoot;
const warnAboutUnrecognizedOptions = require('jest-util').warnAboutUnrecognizedOptions;
const {
Console,
getPackageRoot,
setGlobal,
warnAboutUnrecognizedOptions,
} = require('jest-util');
const readConfig = require('jest-config').readConfig;
const Runtime = require('../');

Expand Down Expand Up @@ -76,7 +79,11 @@ function run(cliArgv?: Object, cliInfo?: Array<string>) {
const TestEnvironment = require(config.testEnvironment);

const env = new TestEnvironment(config);
env.global.console = new Console(process.stdout, process.stderr);
setGlobal(
env.global,
'console',
new Console(process.stdout, process.stderr),
);
env.global.jestConfig = config;

const runtime = new Runtime(config, env, hasteMap.resolver);
Expand Down
33 changes: 19 additions & 14 deletions packages/jest-util/src/FakeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {Global} from 'types/Global';
import type ModuleMocker from 'jest-mock';

const {formatStackTrace} = require('./messages');
const setGlobal = require('./setGlobal');

type Callback = (...args: any) => void;

Expand Down Expand Up @@ -287,25 +288,29 @@ class FakeTimers {
}

useRealTimers() {
this._global.clearImmediate = this._timerAPIs.clearImmediate;
this._global.clearInterval = this._timerAPIs.clearInterval;
this._global.clearTimeout = this._timerAPIs.clearTimeout;
this._global.process.nextTick = this._timerAPIs.nextTick;
this._global.setImmediate = this._timerAPIs.setImmediate;
this._global.setInterval = this._timerAPIs.setInterval;
this._global.setTimeout = this._timerAPIs.setTimeout;
const global = this._global;
setGlobal(global, 'clearImmediate', this._timerAPIs.clearImmediate);
setGlobal(global, 'clearInterval', this._timerAPIs.clearInterval);
setGlobal(global, 'clearTimeout', this._timerAPIs.clearTimeout);
setGlobal(global, 'setImmediate', this._timerAPIs.setImmediate);
setGlobal(global, 'setInterval', this._timerAPIs.setInterval);
setGlobal(global, 'setTimeout', this._timerAPIs.setTimeout);

global.process.nextTick = this._timerAPIs.nextTick;
}

useFakeTimers() {
this._createMocks();

this._global.clearImmediate = this._fakeTimerAPIs.clearImmediate;
this._global.clearInterval = this._fakeTimerAPIs.clearInterval;
this._global.clearTimeout = this._fakeTimerAPIs.clearTimeout;
this._global.process.nextTick = this._fakeTimerAPIs.nextTick;
this._global.setImmediate = this._fakeTimerAPIs.setImmediate;
this._global.setInterval = this._fakeTimerAPIs.setInterval;
this._global.setTimeout = this._fakeTimerAPIs.setTimeout;
const global = this._global;
setGlobal(global, 'clearImmediate', this._fakeTimerAPIs.clearImmediate);
setGlobal(global, 'clearInterval', this._fakeTimerAPIs.clearInterval);
setGlobal(global, 'clearTimeout', this._fakeTimerAPIs.clearTimeout);
setGlobal(global, 'setImmediate', this._fakeTimerAPIs.setImmediate);
setGlobal(global, 'setInterval', this._fakeTimerAPIs.setInterval);
setGlobal(global, 'setTimeout', this._fakeTimerAPIs.setTimeout);

global.process.nextTick = this._fakeTimerAPIs.nextTick;
}

_checkFakeTimers() {
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ const {
formatResultsErrors,
formatStackTrace,
} = require('./messages');
const formatTestResults = require('./formatTestResults');
const clearLine = require('./clearLine');
const fileExists = require('jest-file-exists');
const formatTestResults = require('./formatTestResults');
const installCommonGlobals = require('./installCommonGlobals');
const mkdirp = require('mkdirp');
const path = require('path');
const separateMessageFromStack = require('./separateMessageFromStack');
const setGlobal = require('./setGlobal');

const escapePathForRegex = (dir: string) => {
if (path.sep === '\\') {
Expand Down Expand Up @@ -103,5 +104,6 @@ module.exports = {
installCommonGlobals,
replacePathSepForRegex,
separateMessageFromStack,
setGlobal,
warnAboutUnrecognizedOptions,
};
17 changes: 17 additions & 0 deletions packages/jest-util/src/setGlobal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

// This file must not use 'use strict'.
// See https://github.com/facebook/jest/pull/2457#issuecomment-269518622

import type {Global} from 'types/Global';

module.exports =
(global: Global, key: string, value: any) => global[key] = value;
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1308,9 +1308,9 @@ isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"

istanbul-api@^1.1.0-alpha.1:
version "1.1.0-candidate.0"
resolved "https://registry.yarnpkg.com/istanbul-api/-/istanbul-api-1.1.0-candidate.0.tgz#1b4d3f12baac020f3d172bb0a89970d65794aa53"
istanbul-api@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/istanbul-api/-/istanbul-api-1.1.0.tgz#fb3f62edd5bfc6ae09da09453ded6e10ae7e483b"
dependencies:
async "^2.1.4"
fileset "^2.0.2"
Expand Down