Skip to content

Commit

Permalink
Add Async Test Environment APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
xfumihiro committed Oct 3, 2017
1 parent 95278b2 commit 8a50998
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 31 deletions.
7 changes: 3 additions & 4 deletions integration_tests/__tests__/test_environment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ const runJest = require('../runJest');
const testFixturePackage = require('../test-environment/package.json');

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

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

expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(1);
expect(result.stdout).toBe('teardown\n');
});
6 changes: 5 additions & 1 deletion integration_tests/test-environment/__tests__/env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* @jest-environment ./test-environment
*/
'use strict';
/* eslint-env browser*/
Expand All @@ -14,3 +14,7 @@ test('stub', () => {
const element = document.createElement('div');
expect(element).not.toBeNull();
});

test('stub2', () => {
expect(global.setup).toBe('setup');
});
2 changes: 1 addition & 1 deletion integration_tests/test-environment/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"jest": {
"testEnvironment": "node"
"testEnvironment": "./test-environment"
}
}
25 changes: 25 additions & 0 deletions integration_tests/test-environment/test-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const JSDOMEnvironment = require('../../packages/jest-environment-jsdom');

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

async setup() {
this.global.setup = 'setup';
}

async teardown() {
console.log('teardown');
}

dispose() {
super.dispose();
}

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

module.exports = TestEnvironment;
8 changes: 8 additions & 0 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class JSDOMEnvironment {
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
}

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

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

dispose(): void {
if (this.fakeTimers) {
this.fakeTimers.dispose();
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class NodeEnvironment {
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
}

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

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

dispose() {
if (this.fakeTimers) {
this.fakeTimers.dispose();
Expand Down
54 changes: 29 additions & 25 deletions packages/jest-runner/src/run_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {EnvironmentClass} from 'types/Environment';
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
import type {Resolver} from 'types/Resolve';
import type {TestFramework} from 'types/TestRunner';
import type {TestResult} from 'types/TestResult';
import type RuntimeClass from 'jest-runtime';

import fs from 'fs';
Expand Down Expand Up @@ -100,8 +99,15 @@ export default function runTest(
mapCoverage: globalConfig.mapCoverage,
});
const start = Date.now();
return testFramework(globalConfig, config, environment, runtime, path)
.then((result: TestResult) => {
return environment.setup().then(async () => {
try {
const result = await testFramework(
globalConfig,
config,
environment,
runtime,
path,
);
const testCount =
result.numPassingTests +
result.numFailingTests +
Expand All @@ -113,26 +119,24 @@ export default function runTest(
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;
}),
);
if (environment.teardown) {
await environment.teardown();
}
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)));
} catch (err) {
if (environment.teardown) {
await environment.teardown();
}
environment.dispose();
throw err;
}
});
}
2 changes: 2 additions & 0 deletions types/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ declare class $JestEnvironment {
},
testFilePath: string,
moduleMocker: ModuleMocker,
setup(): Promise<void>,
teardown(): Promise<void>,
}

export type Environment = $JestEnvironment;
Expand Down

0 comments on commit 8a50998

Please sign in to comment.