Skip to content

Commit 5361505

Browse files
committed
Add a non-strict module to set fields on the global object for node 7.3.
1 parent c132921 commit 5361505

File tree

6 files changed

+60
-26
lines changed

6 files changed

+60
-26
lines changed

packages/jest-cli/src/runTest.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import type {TestResult} from 'types/TestResult';
1414
import type Resolver from 'jest-resolve';
1515

1616
const BufferedConsole = require('./lib/BufferedConsole');
17-
const Console = require('jest-util').Console;
18-
const NullConsole = require('jest-util').NullConsole;
19-
17+
const {
18+
Console,
19+
NullConsole,
20+
setGlobal,
21+
} = require('jest-util');
2022
const getConsoleOutput = require('./reporters/getConsoleOutput');
2123

2224
function runTest(path: Path, config: Config, resolver: Resolver) {
@@ -35,7 +37,7 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
3537
? NullConsole
3638
: BufferedConsole
3739
);
38-
const testConsole = env.global.console = new TestConsole(
40+
const testConsole = new TestConsole(
3941
config.useStderr ? process.stderr : process.stdout,
4042
process.stderr,
4143
(type, message) => getConsoleOutput(
@@ -45,6 +47,7 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
4547
BufferedConsole.write([], type, message, 4),
4648
),
4749
);
50+
setGlobal(env.global, 'console', testConsole);
4851
const runtime = new ModuleLoader(config, env, resolver);
4952
const start = Date.now();
5053
return TestRunner(config, env, runtime, path)

packages/jest-runtime/src/cli/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ const os = require('os');
1616
const path = require('path');
1717
const yargs = require('yargs');
1818

19-
const Console = require('jest-util').Console;
20-
const getPackageRoot = require('jest-util').getPackageRoot;
21-
const warnAboutUnrecognizedOptions = require('jest-util').warnAboutUnrecognizedOptions;
19+
const {
20+
Console,
21+
getPackageRoot,
22+
setGlobal,
23+
warnAboutUnrecognizedOptions,
24+
} = require('jest-util');
2225
const readConfig = require('jest-config').readConfig;
2326
const Runtime = require('../');
2427

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

7881
const env = new TestEnvironment(config);
79-
env.global.console = new Console(process.stdout, process.stderr);
82+
setGlobal(
83+
env.global,
84+
'console',
85+
new Console(process.stdout, process.stderr),
86+
);
8087
env.global.jestConfig = config;
8188

8289
const runtime = new Runtime(config, env, hasteMap.resolver);

packages/jest-util/src/FakeTimers.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {Global} from 'types/Global';
1414
import type ModuleMocker from 'jest-mock';
1515

1616
const {formatStackTrace} = require('./messages');
17+
const setGlobal = require('./setGlobal');
1718

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

@@ -287,25 +288,29 @@ class FakeTimers {
287288
}
288289

289290
useRealTimers() {
290-
this._global.clearImmediate = this._timerAPIs.clearImmediate;
291-
this._global.clearInterval = this._timerAPIs.clearInterval;
292-
this._global.clearTimeout = this._timerAPIs.clearTimeout;
293-
this._global.process.nextTick = this._timerAPIs.nextTick;
294-
this._global.setImmediate = this._timerAPIs.setImmediate;
295-
this._global.setInterval = this._timerAPIs.setInterval;
296-
this._global.setTimeout = this._timerAPIs.setTimeout;
291+
const global = this._global;
292+
setGlobal(global, 'clearImmediate', this._timerAPIs.clearImmediate);
293+
setGlobal(global, 'clearInterval', this._timerAPIs.clearInterval);
294+
setGlobal(global, 'clearTimeout', this._timerAPIs.clearTimeout);
295+
setGlobal(global, 'setImmediate', this._timerAPIs.setImmediate);
296+
setGlobal(global, 'setInterval', this._timerAPIs.setInterval);
297+
setGlobal(global, 'setTimeout', this._timerAPIs.setTimeout);
298+
299+
global.process.nextTick = this._timerAPIs.nextTick;
297300
}
298301

299302
useFakeTimers() {
300303
this._createMocks();
301304

302-
this._global.clearImmediate = this._fakeTimerAPIs.clearImmediate;
303-
this._global.clearInterval = this._fakeTimerAPIs.clearInterval;
304-
this._global.clearTimeout = this._fakeTimerAPIs.clearTimeout;
305-
this._global.process.nextTick = this._fakeTimerAPIs.nextTick;
306-
this._global.setImmediate = this._fakeTimerAPIs.setImmediate;
307-
this._global.setInterval = this._fakeTimerAPIs.setInterval;
308-
this._global.setTimeout = this._fakeTimerAPIs.setTimeout;
305+
const global = this._global;
306+
setGlobal(global, 'clearImmediate', this._fakeTimerAPIs.clearImmediate);
307+
setGlobal(global, 'clearInterval', this._fakeTimerAPIs.clearInterval);
308+
setGlobal(global, 'clearTimeout', this._fakeTimerAPIs.clearTimeout);
309+
setGlobal(global, 'setImmediate', this._fakeTimerAPIs.setImmediate);
310+
setGlobal(global, 'setInterval', this._fakeTimerAPIs.setInterval);
311+
setGlobal(global, 'setTimeout', this._fakeTimerAPIs.setTimeout);
312+
313+
global.process.nextTick = this._fakeTimerAPIs.nextTick;
309314
}
310315

311316
_checkFakeTimers() {

packages/jest-util/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ const {
1919
formatResultsErrors,
2020
formatStackTrace,
2121
} = require('./messages');
22-
const formatTestResults = require('./formatTestResults');
2322
const clearLine = require('./clearLine');
2423
const fileExists = require('jest-file-exists');
24+
const formatTestResults = require('./formatTestResults');
2525
const installCommonGlobals = require('./installCommonGlobals');
2626
const mkdirp = require('mkdirp');
2727
const path = require('path');
2828
const separateMessageFromStack = require('./separateMessageFromStack');
29+
const setGlobal = require('./setGlobal');
2930

3031
const escapePathForRegex = (dir: string) => {
3132
if (path.sep === '\\') {
@@ -103,5 +104,6 @@ module.exports = {
103104
installCommonGlobals,
104105
replacePathSepForRegex,
105106
separateMessageFromStack,
107+
setGlobal,
106108
warnAboutUnrecognizedOptions,
107109
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
// This file must not use 'use strict'.
12+
// See https://github.com/facebook/jest/pull/2457#issuecomment-269518622
13+
14+
import type {Global} from 'types/Global';
15+
16+
module.exports =
17+
(global: Global, key: string, value: any) => global[key] = value;

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,9 +1308,9 @@ isstream@~0.1.2:
13081308
version "0.1.2"
13091309
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
13101310

1311-
istanbul-api@^1.1.0-alpha.1:
1312-
version "1.1.0-candidate.0"
1313-
resolved "https://registry.yarnpkg.com/istanbul-api/-/istanbul-api-1.1.0-candidate.0.tgz#1b4d3f12baac020f3d172bb0a89970d65794aa53"
1311+
istanbul-api@^1.1.0:
1312+
version "1.1.0"
1313+
resolved "https://registry.yarnpkg.com/istanbul-api/-/istanbul-api-1.1.0.tgz#fb3f62edd5bfc6ae09da09453ded6e10ae7e483b"
13141314
dependencies:
13151315
async "^2.1.4"
13161316
fileset "^2.0.2"

0 commit comments

Comments
 (0)