From 2f85fd6786233b69915ace42828e3f63780db7bc Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Tue, 29 Jan 2019 13:37:57 -0800 Subject: [PATCH] globalSetup and globalTeardown use default export with es modules --- 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 | 15 ++++++++- 9 files changed, 130 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/e2e/__tests__/globalSetup.test.js b/e2e/__tests__/globalSetup.test.js index 55ccdad7c410..bc8ca3f76327 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 use a default export with ES Modules at ${setupPath}`, + ); +}); diff --git a/e2e/__tests__/globalTeardown.test.js b/e2e/__tests__/globalTeardown.test.js index 0f83b55c8e65..f9d1d09c06a9 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 use a default export with ES Modules 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..685659b4f326 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -59,7 +59,20 @@ export default ({ ); // $FlowFixMe - const globalModule = require(modulePath); + let globalModule = require(modulePath); + + if ( + typeof globalModule === 'object' && + globalModule.__esModule === true + ) { + if (globalModule.default) { + globalModule = globalModule.default; + } else { + throw new TypeError( + `${moduleName} file must use a default export with ES Modules at ${modulePath}`, + ); + } + } if (typeof globalModule !== 'function') { throw new TypeError(