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 @@ -51,6 +51,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 **2?.0.0+**
SimenB marked this conversation as resolved.
Show resolved Hide resolved

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
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 @@ -147,6 +147,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 @@ -481,6 +481,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 @@ -89,6 +89,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 @@ -122,6 +123,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 @@ -224,6 +226,7 @@ export type ProjectConfig = {|
skipNodeResolution: boolean,
snapshotSerializers: Array<Path>,
testEnvironment: string,
testEnvironmentOptions: Object,
testMatch: Array<Glob>,
testLocationInResults: boolean,
testPathIgnorePatterns: Array<string>,
Expand Down