Skip to content

Commit 64b0edd

Browse files
cpojerFacebook Github Bot 1
authored andcommitted
Add jest-environments.
Summary:Supersedes jestjs#873. Moved a bunch more things around, moved common helpers to `jest-util`, added resolution for the `testEnvironment` config option and added documentation. Closes jestjs#881 Differential Revision: D3162251 fb-gh-sync-id: 721b201e089ac5043367d52150f0c0e9f012217f fbshipit-source-id: 721b201e089ac5043367d52150f0c0e9f012217f
1 parent 0397c4f commit 64b0edd

37 files changed

+230
-107
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[#599](https://github.com/facebook/jest/pull/599)
88
* Jasmine 2 is now the default test runner. To keep using Jasmine 1, put
99
`testRunner: "jasmine1"` into your configuration.
10-
* Added `jest-util`, `jest-mock`, `jest-jasmine1`, `jest-jasmine2` packages.
10+
* Added `jest-util`, `jest-mock`, `jest-jasmine1`, `jest-jasmine2`,
11+
`jest-environment-node`, `jest-environment-jsdom` packages.
1112
* Added `babel-jest-preset` and `babel-jest` as packages. `babel-jest` is now
1213
being auto-detected.
1314
* Added `babel-plugin-jest-hoist` which hoists `jest.unmock`, `jest.mock` and
@@ -41,6 +42,7 @@
4142
* Improved `toBeCalled` Jasmine 2 custom matcher messages.
4243
* Improved error reporting when a syntax error occurs.
4344
* Renamed HasteModuleLoader to Runtime.
45+
* Updated jsdom to 8.3.x.
4446
* Jest now properly reports pending tests disabled with `xit` and `xdescribe`.
4547
* Added a `testEnvironment` option to customize the sandbox environment.
4648
* Added support for `@scoped/name` npm packages.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10.0.0
1+
10.0.2

docs/API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Jest uses Jasmine 2 by default. An introduction to Jasmine 2 can be found
6464
- [`config.setupFiles` [array]](#config-setupfiles-array)
6565
- [`config.setupTestFrameworkScriptFile` [string]](#config-setuptestframeworkscriptfile-string)
6666
- [`config.testDirectoryName` [string]](#config-testdirectoryname-string)
67+
- [`config.testEnvironment` [string]](#config-testenvironment-string)
6768
- [`config.testFileExtensions` [array<string>]](#config-testfileextensions-array-string)
6869
- [`config.testPathDirs` [array<string>]](#config-testpathdirs-array-string)
6970
- [`config.testPathIgnorePatterns` [array<string>]](#config-testpathignorepatterns-array-string)
@@ -483,6 +484,11 @@ The name of directories that Jest should expect to find tests in.
483484

484485
For example, many node projects prefer to put their tests in a `tests` directory.
485486

487+
### `config.testEnvironment` [string]
488+
(default: `'jsdom'`)
489+
490+
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/tmpvar/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead.
491+
486492
### `config.testFileExtensions` [array<string>]
487493
(default: `['js']`)
488494

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
"diff": "^2.1.1",
1010
"graceful-fs": "^4.1.3",
1111
"istanbul": "^0.4.2",
12+
"jest-environment-jsdom": "^10.0.1",
13+
"jest-environment-node": "^10.0.1",
1214
"jest-jasmine1": "^10.0.1",
1315
"jest-jasmine2": "^10.0.1",
1416
"jest-mock": "^10.0.1",
1517
"jest-util": "^10.0.1",
16-
"jsdom": "^7.2.0",
1718
"json-stable-stringify": "^1.0.0",
1819
"lodash.template": "^3.6.2",
1920
"mkdirp": "^0.5.1",
@@ -50,7 +51,7 @@
5051
"jest": {
5152
"rootDir": "src",
5253
"testPathIgnorePatterns": ["/__tests__/[^/]*/.+"],
53-
"testEnvironment": "<rootDir>/environments/NodeEnvironment",
54+
"testEnvironment": "jest-environment-node",
5455
"globals": {
5556
"CACHE_DIRECTORY": "<rootDir>/../.haste_cache"
5657
}

packages/babel-jest/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-jest",
3-
"version": "10.0.1",
3+
"version": "10.0.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/facebook/jest.git"
@@ -9,6 +9,6 @@
99
"main": "src/index.js",
1010
"dependencies": {
1111
"babel-core": "^6.0.0",
12-
"babel-preset-jest": "^10.0.1"
12+
"babel-preset-jest": "^10.0.2"
1313
}
1414
}

packages/babel-plugin-jest-hoist/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-jest-hoist",
3-
"version": "10.0.1",
3+
"version": "10.0.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/facebook/jest.git"
@@ -13,7 +13,7 @@
1313
},
1414
"jest": {
1515
"scriptPreprocessor": "../babel-jest",
16-
"testEnvironment": "<rootDir>/../../src/environments/NodeEnvironment"
16+
"testEnvironment": "../jest-environment-node"
1717
},
1818
"scripts": {
1919
"prepublish": "npm test",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "babel-preset-jest",
3-
"version": "10.0.1",
3+
"version": "10.0.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/facebook/jest.git"
77
},
88
"license": "BSD-3-Clause",
99
"main": "index.js",
1010
"dependencies": {
11-
"babel-plugin-jest-hoist": "^10.0.1"
11+
"babel-plugin-jest-hoist": "^10.0.2"
1212
}
1313
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "jest-environment-jsdom",
3+
"version": "10.0.2",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/facebook/jest.git"
7+
},
8+
"license": "BSD-3-Clause",
9+
"main": "src/index.js",
10+
"dependencies": {
11+
"jsdom": "^8.3.1",
12+
"jest-util": "^10.0.2"
13+
}
14+
}

src/environments/__mocks__/JSDOMEnvironment.js renamed to packages/jest-environment-jsdom/src/__mocks__/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
// Copyright 2004-present Facebook. All Rights Reserved.
2-
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+
*/
38
'use strict';
49

5-
const utils = require.requireActual('jest-util');
610
const vm = require.requireActual('vm');
711

8-
const JSDOMEnvironment = jest.genMockFromModule('../JSDOMEnvironment');
12+
const JSDOMEnvironment = jest.genMockFromModule('../index');
913

1014
JSDOMEnvironment.mockImplementation(function(config) {
1115
this.global = {
@@ -14,7 +18,7 @@ JSDOMEnvironment.mockImplementation(function(config) {
1418
JSON,
1519
};
1620

17-
const globalValues = utils.deepCopy(config.globals);
21+
const globalValues = Object.assign({}, config.globals);
1822
for (const customGlobalKey in globalValues) {
1923
this.global[customGlobalKey] = globalValues[customGlobalKey];
2024
}

src/environments/JSDOMEnvironment.js renamed to packages/jest-environment-jsdom/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
'use strict';
99

10-
const FakeTimers = require('../lib/FakeTimers');
11-
const installCommonGlobals = require('./installCommonGlobals');
10+
const FakeTimers = require('jest-util/lib/FakeTimers');
11+
const installCommonGlobals = require('jest-util/lib/installCommonGlobals');
1212

1313
class JSDOMEnvironment {
1414

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "jest-environment-node",
3+
"version": "10.0.2",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/facebook/jest.git"
7+
},
8+
"license": "BSD-3-Clause",
9+
"main": "src/index.js",
10+
"dependencies": {
11+
"jest-util": "^10.0.2"
12+
},
13+
"scripts": {
14+
"test": "../../bin/jest.js"
15+
}
16+
}

src/environments/__tests__/NodeEnvironment-test.js renamed to packages/jest-environment-node/src/__tests__/NodeEnvironment-test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
// Copyright 2004-present Facebook. All Rights Reserved.
2-
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+
*/
38
'use strict';
49

510
jest.disableAutomock();
611

7-
const NodeEnvironment = require.requireActual('../NodeEnvironment');
12+
const NodeEnvironment = require.requireActual('../');
813

914
describe('NodeEnvironment', () => {
1015

src/environments/NodeEnvironment.js renamed to packages/jest-environment-node/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
'use strict';
99

10-
const FakeTimers = require('../lib/FakeTimers');
11-
const installCommonGlobals = require('./installCommonGlobals');
10+
const FakeTimers = require('jest-util/lib/FakeTimers');
11+
const installCommonGlobals = require('jest-util/lib/installCommonGlobals');
1212
const vm = require('vm');
1313

1414
class NodeEnvironment {

packages/jest-jasmine1/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-jasmine1",
3-
"version": "10.0.1",
3+
"version": "10.0.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/facebook/jest.git"
@@ -9,12 +9,12 @@
99
"main": "src/index.js",
1010
"dependencies": {
1111
"graceful-fs": "^4.1.3",
12-
"jest-util": "^10.0.1"
12+
"jest-util": "^10.0.2"
1313
},
1414
"scripts": {
1515
"test": "../../bin/jest.js"
1616
},
1717
"jest": {
18-
"testEnvironment": "<rootDir>/../../src/environments/NodeEnvironment"
18+
"testEnvironment": "node"
1919
}
2020
}

packages/jest-jasmine2/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-jasmine2",
3-
"version": "10.0.1",
3+
"version": "10.0.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/facebook/jest.git"
@@ -9,12 +9,12 @@
99
"main": "src/index.js",
1010
"dependencies": {
1111
"graceful-fs": "^4.1.3",
12-
"jest-util": "^10.0.1"
12+
"jest-util": "^10.0.2"
1313
},
1414
"scripts": {
1515
"test": "../../bin/jest.js"
1616
},
1717
"jest": {
18-
"testEnvironment": "<rootDir>/../../src/environments/NodeEnvironment"
18+
"testEnvironment": "node"
1919
}
2020
}

packages/jest-mock/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-mock",
3-
"version": "10.0.1",
3+
"version": "10.0.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/facebook/jest.git"
@@ -9,5 +9,8 @@
99
"main": "src/index.js",
1010
"scripts": {
1111
"test": "../../bin/jest.js"
12+
},
13+
"jest": {
14+
"testEnvironment": "node"
1215
}
1316
}

packages/jest-util/index.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,7 @@ function replacePathSepForRegex(str) {
2222
return str;
2323
}
2424

25-
function deepCopy(obj) {
26-
const newObj = {};
27-
let value;
28-
for (const key in obj) {
29-
value = obj[key];
30-
if (typeof value === 'object' && value !== null) {
31-
value = deepCopy(value);
32-
}
33-
newObj[key] = value;
34-
}
35-
return newObj;
36-
}
37-
3825
exports.cleanStackTrace = formatMessages.cleanStackTrace;
39-
exports.deepCopy = deepCopy;
4026
exports.escapeStrForRegex = escapeStrForRegex;
4127
exports.formatFailureMessage = formatMessages.formatFailureMessage;
4228
exports.replacePathSepForRegex = replacePathSepForRegex;

src/lib/FakeTimers.js renamed to packages/jest-util/lib/FakeTimers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class FakeTimers {
4646
// If there's a process.nextTick on the global, mock it out
4747
// (only applicable to node/node-emulating environments)
4848
if (typeof global.process === 'object'
49-
&& typeof global.process.nextTick === 'function') {
49+
&& typeof global.process.nextTick === 'function') {
5050
this._originalTimerAPIs.nextTick = global.process.nextTick;
5151
this._fakeTimerAPIs.nextTick = mocks.getMockFn().mockImpl(
5252
this._fakeNextTick.bind(this)
@@ -57,7 +57,7 @@ class FakeTimers {
5757
if (typeof global.setImmediate === 'function') {
5858
this._originalTimerAPIs.setImmediate = global.setImmediate;
5959
this._fakeTimerAPIs.setImmediate = mocks.getMockFn().mockImpl(
60-
this._fakeSetImmediate.bind(this)
60+
this._fakeSetImmediate.bind(this)
6161
);
6262
this._originalTimerAPIs.clearImmediate = global.clearImmediate;
6363
this._fakeTimerAPIs.clearImmediate = mocks.getMockFn().mockImpl(

packages/jest-util/lib/JasmineFormatter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
77
*/
8-
98
'use strict';
109

1110
const diff = require('diff');

packages/jest-util/lib/formatMessages.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
77
*/
8-
98
'use strict';
109

1110
const chalk = require('chalk');
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
'use strict';
9+
10+
function deepCopy(obj) {
11+
const newObj = {};
12+
let value;
13+
for (const key in obj) {
14+
value = obj[key];
15+
if (typeof value === 'object' && value !== null) {
16+
value = deepCopy(value);
17+
}
18+
newObj[key] = value;
19+
}
20+
return newObj;
21+
}
22+
23+
module.exports = (global, globals) => {
24+
// Forward some APIs
25+
global.Buffer = Buffer;
26+
27+
// `global.process` is mutated by FakeTimers. Make a copy of the
28+
// object for the jsdom environment to prevent memory leaks.
29+
global.process = Object.assign({}, process);
30+
global.process.setMaxListeners = process.setMaxListeners.bind(process);
31+
global.process.getMaxListeners = process.getMaxListeners.bind(process);
32+
global.process.emit = process.emit.bind(process);
33+
global.process.addListener = process.addListener.bind(process);
34+
global.process.on = process.on.bind(process);
35+
global.process.once = process.once.bind(process);
36+
global.process.removeListener = process.removeListener.bind(process);
37+
global.process.removeAllListeners = process.removeAllListeners.bind(process);
38+
global.process.listeners = process.listeners.bind(process);
39+
global.process.listenerCount = process.listenerCount.bind(process);
40+
41+
global.setImmediate = setImmediate;
42+
global.clearImmediate = clearImmediate;
43+
44+
Object.assign(global, deepCopy(globals));
45+
};

0 commit comments

Comments
 (0)