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 a49cb57
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
48 changes: 33 additions & 15 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() : (async () => ({}))())
.then(testSetup => {
setGlobal(environment.global, 'setup', testSetup);
return testFramework(globalConfig, config, environment, runtime, path);
})
.then((result: TestResult) => {
const testCount =
result.numPassingTests +
Expand All @@ -117,22 +121,36 @@ export default function runTest(
})
.then(
result =>
Promise.resolve().then(() => {
environment.dispose();
if (globalConfig.logHeapUsage) {
if (global.gc) {
global.gc();
Promise.resolve()
.then(
() =>
environment.teardown
? environment.teardown()
: (async () => ({}))(),
)
.then(() => {
environment.dispose();
if (globalConfig.logHeapUsage) {
if (global.gc) {
global.gc();
}
result.memoryUsage = process.memoryUsage().heapUsed;
}
result.memoryUsage = process.memoryUsage().heapUsed;
}

// Delay the resolution to allow log messages to be output.
return new Promise(resolve => setImmediate(() => resolve(result)));
}),
// 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;
}),
Promise.resolve()
.then(
() =>
environment.teardown
? environment.teardown()
: (async () => ({}))(),
)
.then(() => {
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 a49cb57

Please sign in to comment.