From d0b2c24537b70b5323fc53cb595adbfb36b1a910 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Tue, 29 Jan 2019 15:42:24 -0800 Subject: [PATCH] globalSetup and globalTeardown use default export with es modules (#7750) --- CHANGELOG.md | 1 + e2e/__tests__/globalSetup.test.js | 33 +++++++++++++++++++ e2e/__tests__/globalTeardown.test.js | 33 +++++++++++++++++++ e2e/global-setup/babel.config.js | 2 +- .../invalidSetupWithNamedExport.js | 12 +++++++ e2e/global-setup/setupWithDefaultExport.js | 10 ++++++ e2e/global-teardown/babel.config.js | 5 +++ .../invalidTeardownWithNamedExport.js | 12 +++++++ .../teardownWithDefaultExport.js | 10 ++++++ packages/jest-cli/src/runGlobalHook.js | 7 +++- 10 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 e2e/global-setup/invalidSetupWithNamedExport.js create mode 100644 e2e/global-setup/setupWithDefaultExport.js create mode 100644 e2e/global-teardown/babel.config.js create mode 100644 e2e/global-teardown/invalidTeardownWithNamedExport.js create mode 100644 e2e/global-teardown/teardownWithDefaultExport.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f78d6c8630..99d277321399 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-resolve]`: Pass default resolver into custom resolvers ([#7714](https://github.com/facebook/jest/pull/7714)) +- `[jest-cli]`: `global{Setup,Teardown}` use default export with es modules ([#7750](https://github.com/facebook/jest/pull/7750)) ### Fixes diff --git a/e2e/__tests__/globalSetup.test.js b/e2e/__tests__/globalSetup.test.js index 55ccdad7c410..69ccad35636b 100644 --- a/e2e/__tests__/globalSetup.test.js +++ b/e2e/__tests__/globalSetup.test.js @@ -122,3 +122,36 @@ test('should not call any globalSetup if there are no tests to run', () => { expect(fs.existsSync(project1DIR)).toBe(false); expect(fs.existsSync(project2DIR)).toBe(false); }); + +test('globalSetup works with default export', () => { + const setupPath = path.resolve( + __dirname, + '../global-setup/setupWithDefaultExport.js', + ); + + const testPathPattern = 'pass'; + + const result = runJest('global-setup', [ + `--globalSetup=${setupPath}`, + `--testPathPattern=${testPathPattern}`, + ]); + + expect(result.stdout).toBe(testPathPattern); +}); + +test('globalSetup throws with named export', () => { + const setupPath = path.resolve( + __dirname, + '../global-setup/invalidSetupWithNamedExport.js', + ); + + const {status, stderr} = runJest('global-setup', [ + `--globalSetup=${setupPath}`, + `--testPathPattern=__tests__`, + ]); + + expect(status).toBe(1); + expect(stderr).toMatch( + `TypeError: globalSetup file must export a function at ${setupPath}`, + ); +}); diff --git a/e2e/__tests__/globalTeardown.test.js b/e2e/__tests__/globalTeardown.test.js index 0f83b55c8e65..841cef11f179 100644 --- a/e2e/__tests__/globalTeardown.test.js +++ b/e2e/__tests__/globalTeardown.test.js @@ -111,3 +111,36 @@ test('should not call a globalTeardown of a project if there are no tests to run expect(fs.existsSync(project1DIR)).toBe(true); expect(fs.existsSync(project2DIR)).toBe(false); }); + +test('globalTeardown works with default export', () => { + const teardownPath = path.resolve( + __dirname, + '../global-teardown/teardownWithDefaultExport.js', + ); + + const testPathPattern = 'pass'; + + const result = runJest('global-teardown', [ + `--globalTeardown=${teardownPath}`, + `--testPathPattern=${testPathPattern}`, + ]); + + expect(result.stdout).toBe(testPathPattern); +}); + +test('globalTeardown throws with named export', () => { + const teardownPath = path.resolve( + __dirname, + '../global-teardown/invalidTeardownWithNamedExport.js', + ); + + const {status, stderr} = runJest('global-teardown', [ + `--globalTeardown=${teardownPath}`, + `--testPathPattern=__tests__`, + ]); + + expect(status).toBe(1); + expect(stderr).toMatch( + `TypeError: globalTeardown file must export a function at ${teardownPath}`, + ); +}); diff --git a/e2e/global-setup/babel.config.js b/e2e/global-setup/babel.config.js index 245aa3fd79db..443b08450adb 100644 --- a/e2e/global-setup/babel.config.js +++ b/e2e/global-setup/babel.config.js @@ -1,5 +1,5 @@ // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. module.exports = { - presets: ['@babel/preset-flow'], + presets: ['@babel/preset-env', '@babel/preset-flow'], }; diff --git a/e2e/global-setup/invalidSetupWithNamedExport.js b/e2e/global-setup/invalidSetupWithNamedExport.js new file mode 100644 index 000000000000..df3142d35e04 --- /dev/null +++ b/e2e/global-setup/invalidSetupWithNamedExport.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +function invalidSetupWithNamedExport(jestConfig): void { + console.log(jestConfig.testPathPattern); +} + +export {invalidSetupWithNamedExport}; diff --git a/e2e/global-setup/setupWithDefaultExport.js b/e2e/global-setup/setupWithDefaultExport.js new file mode 100644 index 000000000000..f60cfc524bf4 --- /dev/null +++ b/e2e/global-setup/setupWithDefaultExport.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export default function(jestConfig): void { + console.log(jestConfig.testPathPattern); +} diff --git a/e2e/global-teardown/babel.config.js b/e2e/global-teardown/babel.config.js new file mode 100644 index 000000000000..443b08450adb --- /dev/null +++ b/e2e/global-teardown/babel.config.js @@ -0,0 +1,5 @@ +// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + +module.exports = { + presets: ['@babel/preset-env', '@babel/preset-flow'], +}; diff --git a/e2e/global-teardown/invalidTeardownWithNamedExport.js b/e2e/global-teardown/invalidTeardownWithNamedExport.js new file mode 100644 index 000000000000..284253b9c79e --- /dev/null +++ b/e2e/global-teardown/invalidTeardownWithNamedExport.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +function invalidTeardownWithNamedExport(jestConfig): void { + console.log(jestConfig.testPathPattern); +} + +export {invalidTeardownWithNamedExport}; diff --git a/e2e/global-teardown/teardownWithDefaultExport.js b/e2e/global-teardown/teardownWithDefaultExport.js new file mode 100644 index 000000000000..f60cfc524bf4 --- /dev/null +++ b/e2e/global-teardown/teardownWithDefaultExport.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export default function(jestConfig): void { + console.log(jestConfig.testPathPattern); +} diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-cli/src/runGlobalHook.js index cd5a4ff06bad..ca4448cfd623 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -15,6 +15,11 @@ import pEachSeries from 'p-each-series'; import {addHook} from 'pirates'; import Runtime from 'jest-runtime'; +// copied from https://github.com/babel/babel/blob/56044c7851d583d498f919e9546caddf8f80a72f/packages/babel-helpers/src/helpers.js#L558-L562 +function _interopRequireDefault(obj) { + return obj && obj.__esModule ? obj : {default: obj}; +} + export default ({ allTests, globalConfig, @@ -59,7 +64,7 @@ export default ({ ); // $FlowFixMe - const globalModule = require(modulePath); + const globalModule = _interopRequireDefault(require(modulePath)).default; if (typeof globalModule !== 'function') { throw new TypeError(