diff --git a/CHANGELOG.md b/CHANGELOG.md index a985276ab137..f228453a0d35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/integration-tests/__tests__/global_setup.test.js b/integration-tests/__tests__/global_setup.test.js index 1f957229e8e7..5c14ad22164d 100644 --- a/integration-tests/__tests__/global_setup.test.js +++ b/integration-tests/__tests__/global_setup.test.js @@ -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}`, + ); +}); diff --git a/integration-tests/__tests__/global_teardown.test.js b/integration-tests/__tests__/global_teardown.test.js index 3c9efae86086..92e2e28486a3 100644 --- a/integration-tests/__tests__/global_teardown.test.js +++ b/integration-tests/__tests__/global_teardown.test.js @@ -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}`, + ); +}); diff --git a/integration-tests/global-setup/invalid_setup.js b/integration-tests/global-setup/invalid_setup.js new file mode 100644 index 000000000000..de4e062fb3c2 --- /dev/null +++ b/integration-tests/global-setup/invalid_setup.js @@ -0,0 +1 @@ +console.log('there is no exported function'); diff --git a/integration-tests/global-teardown/invalid_teardown.js b/integration-tests/global-teardown/invalid_teardown.js new file mode 100644 index 000000000000..de4e062fb3c2 --- /dev/null +++ b/integration-tests/global-teardown/invalid_teardown.js @@ -0,0 +1 @@ +console.log('there is no exported function'); diff --git a/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index 8a3df9a0900d..f11bbf010ca3 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -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, @@ -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,