Skip to content

Commit

Permalink
fix(global_setup|global_teardown): fixed await call, change to TypeEr…
Browse files Browse the repository at this point in the history
…ror, add path to error
  • Loading branch information
ranyitz committed Mar 20, 2018
1 parent a8d0a9f commit 87a432c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
5 changes: 4 additions & 1 deletion integration-tests/__tests__/global_setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ test('jest throws an error when globalSetup does not export a function', () => {
]);

expect(status).toBe(1);
expect(stderr).toMatch('Error: globalSetup file must export a function');
expect(stderr).toMatch(
'TypeError: globalSetup file must export a function at',
);
expect(stderr).toMatch(setupPath);
});
9 changes: 6 additions & 3 deletions integration-tests/__tests__/global_teardown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ test('globalTeardown is triggered once after all test suites', () => {
});

test('jest throws an error when globalTeardown does not export a function', () => {
const setupPath = path.resolve(
const teardownPath = path.resolve(
__dirname,
'../global-teardown/invalid_teardown.js',
);
const {status, stderr} = runJest('global-teardown', [
`--globalTeardown=${setupPath}`,
`--globalTeardown=${teardownPath}`,
]);

expect(status).toBe(1);
expect(stderr).toMatch('Error: globalTeardown file must export a function');
expect(stderr).toMatch(
'TypeError: globalTeardown file must export a function at',
);
expect(stderr).toMatch(teardownPath);
});
20 changes: 14 additions & 6 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,16 @@ export default (async function runJest({
setConfig(contexts, {cwd: process.cwd()});
if (globalConfig.globalSetup) {
// $FlowFixMe
const globalSetup = await require(globalConfig.globalSetup);
const globalSetup = require(globalConfig.globalSetup);
if (typeof globalSetup !== 'function') {
throw new Error('globalSetup file must export a function');
throw new TypeError(
`globalSetup file must export a function at ${
globalConfig.globalSetup
}`,
);
}

globalSetup();
await globalSetup();
}
const results = await new TestScheduler(
globalConfig,
Expand All @@ -221,12 +225,16 @@ export default (async function runJest({

if (globalConfig.globalTeardown) {
// $FlowFixMe
const globalTeardown = await require(globalConfig.globalTeardown);
const globalTeardown = require(globalConfig.globalTeardown);
if (typeof globalTeardown !== 'function') {
throw new Error('globalTeardown file must export a function');
throw new TypeError(
`globalTeardown file must export a function at ${
globalConfig.globalTeardown
}`,
);
}

globalTeardown();
await globalTeardown();
}
return processResults(results, {
isJSON: globalConfig.json,
Expand Down

0 comments on commit 87a432c

Please sign in to comment.