Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
Allow setting a test environment per file
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 2, 2017
1 parent 6b8e743 commit f4b2b7b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
13 changes: 12 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,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 pragma', () => {
expect(testFixturePackage.jest.testEnvironment).toEqual('node');

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

expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(1);
});
15 changes: 15 additions & 0 deletions integration_tests/test-environment/__tests__/env-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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
*/
/* 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 @@ -14,6 +14,7 @@
"istanbul-lib-instrument": "^1.1.1",
"jest-changed-files": "^19.0.2",
"jest-config": "^19.0.2",
"jest-docblock": "^19.0.0",
"jest-environment-jsdom": "^19.0.2",
"jest-haste-map": "^19.0.0",
"jest-jasmine2": "^19.0.2",
Expand Down
29 changes: 26 additions & 3 deletions packages/jest-cli/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,34 @@ const {
NullConsole,
setGlobal,
} = require('jest-util');

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

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

try {
data = fs.readFileSync(path, 'utf8');
} catch (e) {
return Promise.reject(e);
}
let testEnvironment = config.testEnvironment;

const testEnvironmentDocblock = docblock.parse(docblock.extract(data));

if (testEnvironmentDocblock['jest-environment']) {
testEnvironment = getTestEnvironment(
Object.assign({}, config, {
testEnvironment: testEnvironmentDocblock['jest-environment'],
})
);
}

/* $FlowFixMe */
const TestEnvironment = require(config.testEnvironment);
const TestEnvironment = require(testEnvironment);
/* $FlowFixMe */
const TestRunner = require(config.testRunner);
/* $FlowFixMe */
Expand All @@ -36,14 +59,14 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
: (config.silent
? NullConsole
: BufferedConsole
);
);
const testConsole = new TestConsole(
config.useStderr ? process.stderr : process.stdout,
process.stderr,
(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,
normalize,
readConfig,
};

0 comments on commit f4b2b7b

Please sign in to comment.