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

Add testEnvironmentOptions to apply to jsdom options or node context. #5003

Merged
merged 10 commits into from
Dec 6, 2017
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

### Features

* `[jest-config]` Add `testEnvironmentOptions` to apply to jsdom options or node context.
([#5003](https://github.com/facebook/jest/pull/5003))
* `[jest-jasmine2]` Update Timeout error message to `jest.timeout` and display
current timeout value ([#4990](https://github.com/facebook/jest/pull/4990))
* `[jest-runner]` Enable experimental detection of leaked contexts
Expand Down
11 changes: 11 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,17 @@ class CustomEnvironment extends NodeEnvironment {
}
```

### `testEnvironmentOptions` [Object]

##### available in Jest **22.0.0+**

Default: `{}`

Test environment options that will be passed to the `testEnvironment`. The
relevant options depend on the environment. For example you can override
options given to [jsdom](https://github.com/tmpvar/jsdom) such as
`{userAgent: "Agent/007"}`.

### `testMatch` [array<string>]

##### available in Jest **19.0.0+**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"setupFiles\\": [],
\\"snapshotSerializers\\": [],
\\"testEnvironment\\": \\"jest-environment-jsdom\\",
\\"testEnvironmentOptions\\": {},
\\"testLocationInResults\\": false,
\\"testMatch\\": [
\\"**/__tests__/**/*.js?(x)\\",
Expand Down
15 changes: 15 additions & 0 deletions integration_tests/__tests__/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,18 @@ test('config from argv is respected with sane config JSON', () => {

expect(stdout).toMatch('"watchman": false');
});

test('works with jsdom testEnvironmentOptions config JSON', () => {
const result = runJest('environmentOptions', [
'--config=' +
JSON.stringify({
testEnvironmentOptions: {
userAgent: 'Agent/007',
},
}),
]);
const stderr = result.stderr.toString();

expect(result.status).toBe(0);
expect(stderr).toMatch('found userAgent Agent/007');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/*global window */

test('found userAgent Agent/007', () => {
expect(window.navigator.userAgent).toBe('Agent/007');
});
5 changes: 5 additions & 0 deletions integration_tests/environmentOptions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "jsdom"
}
}
1 change: 1 addition & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default ({
runner: 'jest-runner',
snapshotSerializers: [],
testEnvironment: 'jest-environment-jsdom',
testEnvironmentOptions: {},
testFailureExitCode: 1,
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const getConfigs = (
skipNodeResolution: options.skipNodeResolution,
snapshotSerializers: options.snapshotSerializers,
testEnvironment: options.testEnvironment,
testEnvironmentOptions: options.testEnvironmentOptions,
testLocationInResults: options.testLocationInResults,
testMatch: options.testMatch,
testPathIgnorePatterns: options.testPathIgnorePatterns,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
case 'silent':
case 'skipNodeResolution':
case 'testEnvironment':
case 'testEnvironmentOptions':
case 'testFailureExitCode':
case 'testLocationInResults':
case 'testNamePattern':
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/valid_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default ({
skipNodeResolution: false,
snapshotSerializers: ['my-serializer-module'],
testEnvironment: 'jest-environment-jsdom',
testEnvironmentOptions: {},
testFailureExitCode: 1,
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'],
Expand Down
16 changes: 11 additions & 5 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ class JSDOMEnvironment {
moduleMocker: ?ModuleMocker;

constructor(config: ProjectConfig) {
this.dom = new JSDOM('<!DOCTYPE html>', {
pretendToBeVisual: true,
runScripts: 'dangerously',
url: config.testURL,
});
this.dom = new JSDOM(
'<!DOCTYPE html>',
Object.assign(
{
pretendToBeVisual: true,
runScripts: 'dangerously',
url: config.testURL,
},
config.testEnvironmentOptions,
),
);
const global = (this.global = this.dom.window.document.defaultView);
// Node's error-message stack size is limited at 10, but it's pretty useful
// to see more than that when a test fails.
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class NodeEnvironment {

constructor(config: ProjectConfig) {
this.context = vm.createContext();
const global = (this.global = vm.runInContext('this', this.context));
const global = (this.global = vm.runInContext(
'this',
Object.assign(this.context, config.testEnvironmentOptions),
));
global.global = global;
global.clearInterval = clearInterval;
global.clearTimeout = clearTimeout;
Expand Down
1 change: 1 addition & 0 deletions test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = {
skipNodeResolution: false,
snapshotSerializers: [],
testEnvironment: 'node',
testEnvironmentOptions: {},
testLocationInResults: false,
testMatch: [],
testPathIgnorePatterns: [],
Expand Down
3 changes: 3 additions & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type DefaultOptions = {|
runTestsByPath: boolean,
snapshotSerializers: Array<Path>,
testEnvironment: string,
testEnvironmentOptions: Object,
testFailureExitCode: string | number,
testLocationInResults: boolean,
testMatch: Array<Glob>,
Expand Down Expand Up @@ -121,6 +122,7 @@ export type InitialOptions = {
skipNodeResolution?: boolean,
snapshotSerializers?: Array<Path>,
testEnvironment?: string,
testEnvironmentOptions?: Object,
testFailureExitCode?: string | number,
testLocationInResults?: boolean,
testMatch?: Array<Glob>,
Expand Down Expand Up @@ -222,6 +224,7 @@ export type ProjectConfig = {|
skipNodeResolution: boolean,
snapshotSerializers: Array<Path>,
testEnvironment: string,
testEnvironmentOptions: Object,
testMatch: Array<Glob>,
testLocationInResults: boolean,
testPathIgnorePatterns: Array<string>,
Expand Down