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 Nov 2, 2020
1 parent 6bd96a0 commit af4cec4
Show file tree
Hide file tree
Showing 19 changed files with 33 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
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
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
@@ -1,3 +1,5 @@
{
"jest": {}
"jest": {
"testEnvironment": "jsdom"
}
}
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: {
'\\.[tj]s$': ['babel-jest', {configFile: require.resolve('./.babelrc')}],
},
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 @@ -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
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
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
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
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
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
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
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
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

0 comments on commit af4cec4

Please sign in to comment.