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 Sep 19, 2017
1 parent 95278b2 commit c9989c9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 21 additions & 18 deletions packages/jest-runner/src/run_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export default function runTest(
mapCoverage: globalConfig.mapCoverage,
});
const start = Date.now();
return testFramework(globalConfig, config, environment, runtime, path)
return (environment.setup ? environment.setup() : Promise.resolve({}))
.then(testSetup => {
setGlobal(environment.global, 'setup', testSetup);
return testFramework(globalConfig, config, environment, runtime, path);
})
.then((result: TestResult) => {
const testCount =
result.numPassingTests +
Expand All @@ -116,23 +120,22 @@ export default function runTest(
return result;
})
.then(
result =>
Promise.resolve().then(() => {
environment.dispose();
if (globalConfig.logHeapUsage) {
if (global.gc) {
global.gc();
}
result.memoryUsage = process.memoryUsage().heapUsed;
async result => {
if (environment.teardown) await environment.teardown();
environment.dispose();
if (globalConfig.logHeapUsage) {
if (global.gc) {
global.gc();
}

// 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;
}),
result.memoryUsage = process.memoryUsage().heapUsed;
}
// Delay the resolution to allow log messages to be output.
return new Promise(resolve => setImmediate(() => resolve(result)));
},
async 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<Global>,
teardown(): Promise<void>,
}

export type Environment = $JestEnvironment;
Expand Down

0 comments on commit c9989c9

Please sign in to comment.