Skip to content

Commit

Permalink
chore: default to node test env rather than browser
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 23, 2020
1 parent 1c2011d commit b7df539
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser ([#9874](https://github.com/facebook/jest/pull/9874))
- `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801))
- `[jest-runtime]` Populate `require.cache` ([#9841](https://github.com/facebook/jest/pull/9841))

Expand Down
6 changes: 3 additions & 3 deletions docs/Configuration.md
Expand Up @@ -881,9 +881,9 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap

### `testEnvironment` [string]

Default: `"jsdom"`
Default: `"node"`

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.
The test environment that will be used for testing. The default environment in Jest is a Node.js environment. If you are building a web app, you can use a browser-like environment through [`jsdom`](https://github.com/jsdom/jsdom) instead.

By adding a `@jest-environment` docblock at the top of the file, you can specify another environment to be used for all tests in that file:

Expand Down Expand Up @@ -963,7 +963,7 @@ beforeAll(() => {

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"}`.
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/jsdom/jsdom) such as `{userAgent: "Agent/007"}`.

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

Expand Down
Expand Up @@ -20,6 +20,6 @@ exports[`prints console.logs when run with forceExit 3`] = `
console.log
Hey
at Object.<anonymous> (__tests__/a-banana.js:1:1)
at Object.log (__tests__/a-banana.js:1:30)
`;
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -51,7 +51,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"setupFilesAfterEnv": [],
"skipFilter": false,
"snapshotSerializers": [],
"testEnvironment": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-environment-jsdom/build/index.js",
"testEnvironment": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-environment-node/build/index.js",
"testEnvironmentOptions": {},
"testLocationInResults": false,
"testMatch": [
Expand Down
1 change: 1 addition & 0 deletions e2e/__tests__/config.test.ts
Expand Up @@ -59,6 +59,7 @@ test('works with jsdom testEnvironmentOptions config JSON', () => {
const result = runJest('environmentOptions', [
'--config=' +
JSON.stringify({
testEnvironment: 'jsdom',
testEnvironmentOptions: {
url: 'https://jestjs.io',
},
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/consoleLogOutputWhenRunInBand.test.ts
Expand Up @@ -34,14 +34,14 @@ test('prints console.logs when run with forceExit', () => {

const {rest, summary} = extractSummary(stderr);

if (nodeMajorVersion < 12) {
if (nodeMajorVersion < 10) {
expect(stdout).toContain(
'at Object.<anonymous>.test (__tests__/a-banana.js:1:1)',
);

stdout = stdout.replace(
'at Object.<anonymous>.test (__tests__/a-banana.js:1:1)',
'at Object.<anonymous> (__tests__/a-banana.js:1:1)',
'at Object.log (__tests__/a-banana.js:1:30)',
);
}

Expand Down
1 change: 1 addition & 0 deletions e2e/__tests__/resolveBrowserField.test.ts
Expand Up @@ -35,6 +35,7 @@ test('preserves module identity for symlinks when using browser field resolution
`,
'package.json': JSON.stringify({
jest: {
testEnvironment: 'jsdom',
testMatch: ['<rootDir>/test-files/test.js'],
browser: true,
},
Expand Down
1 change: 1 addition & 0 deletions examples/angular/jest.config.js
@@ -1,6 +1,7 @@
module.exports = {
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
setupFilesAfterEnv: ['<rootDir>/setupJest.js'],
testEnvironment: 'jsdom',
transform: {
'^.+\\.[t|j]s$': [
'babel-jest',
Expand Down
3 changes: 3 additions & 0 deletions examples/jquery/package.json
Expand Up @@ -13,5 +13,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
3 changes: 3 additions & 0 deletions examples/react-testing-library/package.json
Expand Up @@ -17,5 +17,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
3 changes: 3 additions & 0 deletions examples/react/package.json
Expand Up @@ -16,5 +16,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
3 changes: 3 additions & 0 deletions examples/typescript/package.json
Expand Up @@ -18,5 +18,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
1 change: 0 additions & 1 deletion jest.config.js
Expand Up @@ -34,7 +34,6 @@ module.exports = {
'<rootDir>/packages/pretty-format/build/plugins/ConvertAnsi.js',
require.resolve('jest-snapshot-serializer-raw'),
],
testEnvironment: './packages/jest-environment-node',
testPathIgnorePatterns: [
'/__arbitraries__/',
'/node_modules/',
Expand Down
Expand Up @@ -180,7 +180,7 @@ module.exports = {
// snapshotSerializers: [],
// The test environment that will be used for testing
// testEnvironment: \\"jest-environment-jsdom\\",
// testEnvironment: \\"jest-environment-node\\",
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
Expand Down
6 changes: 2 additions & 4 deletions packages/jest-cli/src/init/__tests__/init.test.js
Expand Up @@ -100,8 +100,7 @@ describe('init', () => {

const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
// should modify when the default environment will be changed to "node"
expect(evaluatedConfig).toEqual({});
expect(evaluatedConfig).toEqual({testEnvironment: 'jsdom'});
});

it('should create configuration for {environment: "node"}', async () => {
Expand All @@ -111,8 +110,7 @@ describe('init', () => {

const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
// should modify when the default environment will be changed to "node"
expect(evaluatedConfig).toEqual({testEnvironment: 'node'});
expect(evaluatedConfig).toEqual({});
});

it('should create package.json with configured test command when {scripts: true}', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/src/init/generate_config_file.ts
Expand Up @@ -45,9 +45,9 @@ const generateConfigFile = (
});
}

if (environment === 'node') {
if (environment === 'jsdom') {
Object.assign(overrides, {
testEnvironment: 'node',
testEnvironment: 'jsdom',
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/Defaults.ts
Expand Up @@ -53,7 +53,7 @@ const defaultOptions: Config.DefaultOptions = {
setupFilesAfterEnv: [],
skipFilter: false,
snapshotSerializers: [],
testEnvironment: 'jest-environment-jsdom',
testEnvironment: 'jest-environment-node',
testEnvironmentOptions: {},
testFailureExitCode: 1,
testLocationInResults: false,
Expand Down
2 changes: 1 addition & 1 deletion website/blog/2017-12-18-jest-22.md
Expand Up @@ -11,7 +11,7 @@ Today we are announcing a new major version of Jest which refines almost all par

## Good bye Node 4 & welcome JSDOM 11

With this release we are dropping support for Node 4, mainly because one of our main dependencies, JSDOM, ended their support. Jest now comes out of the box with JSDOM 11 which features better support for SVGs, `requestAnimationFrame`, `URL` and `URLSearchParams` built in, and [much more](https://github.com/tmpvar/jsdom/blob/master/Changelog.md).
With this release we are dropping support for Node 4, mainly because one of our main dependencies, JSDOM, ended their support. Jest now comes out of the box with JSDOM 11 which features better support for SVGs, `requestAnimationFrame`, `URL` and `URLSearchParams` built in, and [much more](https://github.com/jsdom/jsdom/blob/master/Changelog.md).

## Custom Runners + Easy parallelization with `jest-worker`

Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-22.x/Configuration.md
Expand Up @@ -651,7 +651,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap

Default: `"jsdom"`

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.
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/jsdom/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead.

If some tests require another environment, you can add a `@jest-environment` docblock.

Expand Down Expand Up @@ -714,7 +714,7 @@ _Note: Jest comes with JSDOM@11 by default. Due to JSDOM 12 and newer dropping s

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"}`.
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/jsdom/jsdom) such as `{userAgent: "Agent/007"}`.

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

Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-23.x/Configuration.md
Expand Up @@ -715,7 +715,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap

Default: `"jsdom"`

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.
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/jsdom/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead.

By adding a `@jest-environment` docblock at the top of the file, you can specify another environment to be used for all tests in that file:

Expand Down Expand Up @@ -780,7 +780,7 @@ _Note: Jest comes with JSDOM@11 by default. Due to JSDOM 12 and newer dropping s

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"}`.
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/jsdom/jsdom) such as `{userAgent: "Agent/007"}`.

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

Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-24.x/Configuration.md
Expand Up @@ -860,7 +860,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap

Default: `"jsdom"`

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.
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/jsdom/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead.

By adding a `@jest-environment` docblock at the top of the file, you can specify another environment to be used for all tests in that file:

Expand Down Expand Up @@ -942,7 +942,7 @@ _Note: Jest comes with JSDOM@11 by default. Due to JSDOM 12 and newer dropping s

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"}`.
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/jsdom/jsdom) such as `{userAgent: "Agent/007"}`.

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

Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-25.1/Configuration.md
Expand Up @@ -871,7 +871,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap

Default: `"jsdom"`

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.
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/jsdom/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead.

By adding a `@jest-environment` docblock at the top of the file, you can specify another environment to be used for all tests in that file:

Expand Down Expand Up @@ -951,7 +951,7 @@ beforeAll(() => {

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"}`.
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/jsdom/jsdom) such as `{userAgent: "Agent/007"}`.

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

Expand Down
4 changes: 2 additions & 2 deletions website/versioned_docs/version-25.3/Configuration.md
Expand Up @@ -884,7 +884,7 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap

Default: `"jsdom"`

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.
The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/jsdom/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead.

By adding a `@jest-environment` docblock at the top of the file, you can specify another environment to be used for all tests in that file:

Expand Down Expand Up @@ -964,7 +964,7 @@ beforeAll(() => {

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"}`.
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/jsdom/jsdom) such as `{userAgent: "Agent/007"}`.

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

Expand Down

0 comments on commit b7df539

Please sign in to comment.