diff --git a/packages/jest-cli/src/runTest.js b/packages/jest-cli/src/runTest.js index a7d0f99acda9..587d3d0569d1 100644 --- a/packages/jest-cli/src/runTest.js +++ b/packages/jest-cli/src/runTest.js @@ -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) { @@ -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( @@ -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) diff --git a/packages/jest-runtime/src/cli/index.js b/packages/jest-runtime/src/cli/index.js index 640d4d5408f2..f889e348d090 100644 --- a/packages/jest-runtime/src/cli/index.js +++ b/packages/jest-runtime/src/cli/index.js @@ -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('../'); @@ -76,7 +79,11 @@ function run(cliArgv?: Object, cliInfo?: Array) { 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); diff --git a/packages/jest-util/src/FakeTimers.js b/packages/jest-util/src/FakeTimers.js index c0bcabd3cd51..f94ab131531a 100644 --- a/packages/jest-util/src/FakeTimers.js +++ b/packages/jest-util/src/FakeTimers.js @@ -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; @@ -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() { diff --git a/packages/jest-util/src/index.js b/packages/jest-util/src/index.js index 7a7216083faa..6d29a270f900 100644 --- a/packages/jest-util/src/index.js +++ b/packages/jest-util/src/index.js @@ -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 === '\\') { @@ -103,5 +104,6 @@ module.exports = { installCommonGlobals, replacePathSepForRegex, separateMessageFromStack, + setGlobal, warnAboutUnrecognizedOptions, }; diff --git a/packages/jest-util/src/setGlobal.js b/packages/jest-util/src/setGlobal.js new file mode 100644 index 000000000000..dae00059cf8e --- /dev/null +++ b/packages/jest-util/src/setGlobal.js @@ -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; diff --git a/yarn.lock b/yarn.lock index 942007dec371..de05a831523b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"