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

Allow setting a test environment per file #2859

Merged
merged 2 commits into from
Mar 3, 2017
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
13 changes: 12 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,18 @@ To make a dependency explicit instead of implicit, you can call [`expect.addSnap
### `testEnvironment` [string]
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. Combining the test environments is currently not possible but the `jsdom` environment can be seen as a superset of the `node` one.
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. If some tests require another environment, you can add a `@jest-environment` docblock.

```js
/**
* @jest-environment jsdom
*/

test('use jsdom in this test file', () => {
const element = document.createElement('div');
expect(element).not.toBeNull();
});
```

You can create your own module that will be used for setting up the test environment. The module must export a class with `runScript` and `dispose` methods. See the [node](https://github.com/facebook/jest/blob/master/packages/jest-environment-node/src/index.js) or [jsdom](https://github.com/facebook/jest/blob/master/packages/jest-environment-jsdom/src/index.js) environments as examples.

Expand Down
26 changes: 26 additions & 0 deletions integration_tests/__tests__/test-environment-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
'use strict';

const runJest = require('../runJest');
const skipOnWindows = require('skipOnWindows');

skipOnWindows.suite();

const testFixturePackage = require('../test-environment/package.json');

it('respects testEnvironment docblock', () => {
expect(testFixturePackage.jest.testEnvironment).toEqual('node');

const result = runJest.json('test-environment').json;

expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(1);
});
16 changes: 16 additions & 0 deletions integration_tests/test-environment/__tests__/env-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @jest-environment jsdom
*/
'use strict';
/* eslint-env browser*/

test('stub', () => {
const element = document.createElement('div');
expect(element).not.toBeNull();
});
5 changes: 5 additions & 0 deletions integration_tests/test-environment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
1 change: 1 addition & 0 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"istanbul-lib-source-maps": "^1.1.0",
"jest-changed-files": "^19.0.2",
"jest-config": "^19.0.2",
"jest-docblock": "^19.0.2",
"jest-environment-jsdom": "^19.0.2",
"jest-haste-map": "^19.0.0",
"jest-jasmine2": "^19.0.2",
Expand Down
28 changes: 26 additions & 2 deletions packages/jest-cli/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,35 @@ const {
NullConsole,
setGlobal,
} = require('jest-util');

const {getTestEnvironment} = require('jest-config');
const fs = require('fs');
const docblock = require('jest-docblock');
const getConsoleOutput = require('./reporters/getConsoleOutput');

function runTest(path: Path, config: Config, resolver: Resolver) {
let testSource;

try {
testSource = fs.readFileSync(path, 'utf8');
} catch (e) {
return Promise.reject(e);
}

const parsedDocblock = docblock.parse(docblock.extract(testSource));
const customEnvironment = parsedDocblock['jest-environment'];
let testEnvironment = config.testEnvironment;

if (customEnvironment) {
testEnvironment = getTestEnvironment(
Object.assign({}, config, {
testEnvironment: customEnvironment,
})
);
}

/* $FlowFixMe */
const TestEnvironment = require(config.testEnvironment);
const TestEnvironment = require(testEnvironment);
/* $FlowFixMe */
const TestRunner = require(config.testRunner);
/* $FlowFixMe */
Expand All @@ -39,7 +63,7 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
(type, message) => getConsoleOutput(
config.rootDir,
!!config.verbose,
// 4 = the console call is burried 4 stack frames deep
// 4 = the console call is buried 4 stack frames deep
BufferedConsole.write([], type, message, 4),
),
);
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const loadFromFile = require('./loadFromFile');
const loadFromPackage = require('./loadFromPackage');
const normalize = require('./normalize');
const setFromArgv = require('./setFromArgv');
const {getTestEnvironment} = require('./utils');

const readConfig = (argv: Object, packageRoot: string) =>
readRawConfig(argv, packageRoot)
Expand Down Expand Up @@ -60,6 +61,7 @@ const readRawConfig = (argv, root) => {
};

module.exports = {
getTestEnvironment,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this OK?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

normalize,
readConfig,
};
3 changes: 3 additions & 0 deletions packages/jest-docblock/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
10 changes: 10 additions & 0 deletions packages/jest-docblock/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "jest-docblock",
"version": "19.0.2",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git"
},
"license": "BSD-3-Clause",
"main": "build/index.js"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'use strict';

const os = require('os');
const docblock = require('../docblock');
const docblock = require('../');

describe('docblock', () => {

Expand Down
1 change: 1 addition & 0 deletions packages/jest-haste-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"fb-watchman": "^2.0.0",
"graceful-fs": "^4.1.6",
"jest-docblock": "^19.0.2",
"micromatch": "^2.3.11",
"sane": "~1.5.0",
"worker-farm": "^1.3.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {HasteImpl, WorkerMessage, WorkerCallback} from './types';

const H = require('./constants');

const docblock = require('./lib/docblock');
const docblock = require('jest-docblock');
const extractRequires = require('./lib/extractRequires');
const fs = require('graceful-fs');
const path = require('path');
Expand Down