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

Add Async Test Environment APIs #4506

Merged
merged 1 commit into from
Oct 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* `[jest-util]` Fix `runOnlyPendingTimers` for `setTimeout` inside `setImmediate` ([#4608](https://github.com/facebook/jest/pull/4608))

### Features
* `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))
* `[jest-cli]` Add an option to clear the cache ([#4430](https://github.com/facebook/jest/pull/4430))
* `[babel-plugin-jest-hoist]` Improve error message, that the second argument of `jest.mock` must be an inline function ([#4593](https://github.com/facebook/jest/pull/4593))
* `[jest-snapshot]` [**BREAKING**] Concatenate name of test and snapshot ([#4460](https://github.com/facebook/jest/pull/4460))
Expand Down
26 changes: 26 additions & 0 deletions integration_tests/__tests__/test_environment_async.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 MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';

const fs = require('fs');
const os = require('os');
const runJest = require('../runJest');
const {cleanup} = require('../utils');

const DIR = os.tmpdir() + '/jest';

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

it('triggers setup/teardown hooks', () => {
const result = runJest('test-environment-async');
expect(result.status).toBe(0);
const teardown = fs.readFileSync(DIR + '/teardown', 'utf8');
expect(teardown).toBe('teardown');
});
13 changes: 13 additions & 0 deletions integration_tests/test-environment-async/__tests__/custom.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
/* eslint-env browser*/

test('setup', () => {
expect(global.setup).toBe('setup');
});
5 changes: 5 additions & 0 deletions integration_tests/test-environment-async/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "./test-environment.js"
}
}
33 changes: 33 additions & 0 deletions integration_tests/test-environment-async/test-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const fs = require('fs');
const os = require('os');
const mkdirp = require('mkdirp');
const JSDOMEnvironment = require('jest-environment-jsdom');

const DIR = os.tmpdir() + '/jest';

class TestEnvironment extends JSDOMEnvironment {
constructor(config) {
super(config);
}

setup() {
return super.setup().then(() => {
this.global.setup = 'setup';
});
}

teardown() {
return super.teardown().then(() => {
mkdirp.sync(DIR);
fs.writeFileSync(DIR + '/teardown', 'teardown');
});
}

runScript(script) {
return super.runScript(script);
}
}

module.exports = TestEnvironment;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"react-test-renderer": "15.4.2",
"regenerator-runtime": "^0.11.0",
"resolve": "^1.4.0",
"rimraf": "^2.5.4",
"rimraf": "^2.6.2",
"rollup": "^0.50.0",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-commonjs": "^8.2.1",
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ class JSDOMEnvironment {
});
}

dispose(): void {
setup(): Promise<void> {
return Promise.resolve();
}

teardown(): Promise<void> {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
Expand All @@ -68,6 +72,7 @@ class JSDOMEnvironment {
this.global = null;
this.document = null;
this.fakeTimers = null;
return Promise.resolve();
}

runScript(script: Script): ?any {
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ class NodeEnvironment {
});
}

dispose() {
setup(): Promise<void> {
return Promise.resolve();
}

teardown(): Promise<void> {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
this.context = null;
this.fakeTimers = null;
return Promise.resolve();
}

// Disabling rule as return type depends on script's return type.
Expand Down
66 changes: 30 additions & 36 deletions packages/jest-runner/src/run_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import * as docblock from 'jest-docblock';
// and required implicitly through the `testRunner` ProjectConfig option.
jasmine2;

export default function runTest(
export default (async function runTest(
path: Path,
globalConfig: GlobalConfig,
config: ProjectConfig,
Expand Down Expand Up @@ -95,39 +95,33 @@ export default function runTest(
mapCoverage: globalConfig.mapCoverage,
});
const start = Date.now();
return testFramework(globalConfig, config, environment, runtime, path)
.then((result: TestResult) => {
const testCount =
result.numPassingTests +
result.numFailingTests +
result.numPendingTests;
result.perfStats = {end: Date.now(), start};
result.testFilePath = path;
result.coverage = runtime.getAllCoverageInfo();
result.sourceMaps = runtime.getSourceMapInfo();
result.console = testConsole.getBuffer();
result.skipped = testCount === result.numPendingTests;
result.displayName = config.displayName;
return result;
})
.then(
result =>
Promise.resolve().then(() => {
environment.dispose();
if (globalConfig.logHeapUsage) {
if (global.gc) {
global.gc();
}
result.memoryUsage = process.memoryUsage().heapUsed;
}

// Delay the resolution to allow log messages to be output.
return new Promise(resolve => setImmediate(() => resolve(result)));
}),
err =>
Promise.resolve().then(() => {
environment.dispose();
throw err;
}),
await environment.setup();
try {
const result: TestResult = await testFramework(
globalConfig,
config,
environment,
runtime,
path,
);
}
const testCount =
result.numPassingTests + result.numFailingTests + result.numPendingTests;
result.perfStats = {end: Date.now(), start};
result.testFilePath = path;
result.coverage = runtime.getAllCoverageInfo();
result.sourceMaps = runtime.getSourceMapInfo();
result.console = testConsole.getBuffer();
result.skipped = testCount === result.numPendingTests;
result.displayName = config.displayName;
if (globalConfig.logHeapUsage) {
if (global.gc) {
global.gc();
}
result.memoryUsage = process.memoryUsage().heapUsed;
}
// Delay the resolution to allow log messages to be output.
return new Promise(resolve => setImmediate(() => resolve(result)));
Copy link
Member

Choose a reason for hiding this comment

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

this can probably be

await new Promise(resolve => setImmediate(resolve));
return result;

No idea if it affects the stacktraces of matchers, though.

Feel free to ignore!

} finally {
await environment.teardown();
}
});
3 changes: 2 additions & 1 deletion types/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {ModuleMocker} from 'jest-mock';

declare class $JestEnvironment {
constructor(config: ProjectConfig): void,
dispose(): void,
runScript(script: Script): any,
global: Global,
fakeTimers: {
Expand All @@ -30,6 +29,8 @@ declare class $JestEnvironment {
},
testFilePath: string,
moduleMocker: ModuleMocker,
setup(): Promise<void>,
teardown(): Promise<void>,
}

export type Environment = $JestEnvironment;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ debug@2.6.8, debug@^2.2.0, debug@^2.6.3, debug@^2.6.8:
dependencies:
ms "2.0.0"

debug@^3.0.1, debug@3.1.0:
debug@3.1.0, debug@^3.0.1:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
dependencies:
Expand Down Expand Up @@ -5103,9 +5103,9 @@ right-align@^0.1.1:
dependencies:
align-text "^0.1.1"

rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.0, rimraf@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.0, rimraf@^2.6.1, rimraf@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
dependencies:
glob "^7.0.5"

Expand Down