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

chore: default to node test env rather than browser #9874

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))

### Fixes

### Chore & Maintenance
Expand Down
6 changes: 3 additions & 3 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,9 +989,9 @@ More about serializers API can be found [here](https://github.com/facebook/jest/

### `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/jsdom/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 @@ -1071,7 +1071,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"skipFilter": false,
"slowTestThreshold": 5,
"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
Original file line number Diff line number Diff line change
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: 3 additions & 1 deletion e2e/dom-diffing/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"jest": {}
"jest": {
"testEnvironment": "jsdom"
}
}
1 change: 1 addition & 0 deletions examples/angular/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
setupFilesAfterEnv: ['<rootDir>/setupJest.js'],
testEnvironment: 'jsdom',
transform: {
'\\.[tj]s$': ['babel-jest', {configFile: require.resolve('./.babelrc')}],
},
Expand Down
3 changes: 3 additions & 0 deletions examples/jquery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
3 changes: 3 additions & 0 deletions examples/react-testing-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
3 changes: 3 additions & 0 deletions examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
3 changes: 3 additions & 0 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
},
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "jsdom"
}
}
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ module.exports = {
'<rootDir>/packages/pretty-format/build/plugins/ConvertAnsi.js',
require.resolve('jest-snapshot-serializer-raw'),
],
testEnvironment: './packages/jest-environment-node',
testPathIgnorePatterns: [
'/test-types/',
'/__arbitraries__/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,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
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,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 @@ -135,8 +134,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/generateConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const defaultOptions: Config.DefaultOptions = {
skipFilter: false,
slowTestThreshold: 5,
snapshotSerializers: [],
testEnvironment: 'jest-environment-jsdom',
testEnvironment: 'jest-environment-node',
testEnvironmentOptions: {},
testFailureExitCode: 1,
testLocationInResults: false,
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-22.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-23.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-24.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-25.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-26.0/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-26.2/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-26.4/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-26.5/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-26.6/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,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/jsdom/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"}`.

### `testFailureExitCode` [number]

Expand Down