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 a descriptive error for invalid globalSetup or globalTeardown #5835

Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

### Fixes

* `[jest-cli]` Add descriptive error message when trying to use
`globalSetup`/`globalTeardown` file that doesn't export a function.
([#5835](https://github.com/facebook/jest/pull/5835))
* `[expect]` Do not rely on `instanceof RegExp`, since it will not work for
RegExps created inside of a different VM
([#5729](https://github.com/facebook/jest/pull/5729))
Expand Down
12 changes: 12 additions & 0 deletions integration-tests/__tests__/global_setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ test('globalSetup is triggered once before all test suites', () => {
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
expect(setup).toBe('setup');
});

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

expect(status).toBe(1);
expect(stderr).toMatch(
`TypeError: globalSetup file must export a function at ${setupPath}`,
);
});
15 changes: 15 additions & 0 deletions integration-tests/__tests__/global_teardown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ test('globalTeardown is triggered once after all test suites', () => {
const teardown = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
expect(teardown).toBe('teardown');
});

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

expect(status).toBe(1);
expect(stderr).toMatch(
`TypeError: globalTeardown file must export a function at ${teardownPath}`,
);
});
1 change: 1 addition & 0 deletions integration-tests/global-setup/invalid_setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('there is no exported function');
1 change: 1 addition & 0 deletions integration-tests/global-teardown/invalid_teardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('there is no exported function');
22 changes: 20 additions & 2 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,16 @@ export default (async function runJest({
setConfig(contexts, {cwd: process.cwd()});
if (globalConfig.globalSetup) {
// $FlowFixMe
await require(globalConfig.globalSetup)();
const globalSetup = require(globalConfig.globalSetup);
if (typeof globalSetup !== 'function') {
throw new TypeError(
`globalSetup file must export a function at ${
globalConfig.globalSetup
}`,
);
}

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

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

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