Skip to content

Commit

Permalink
globalSetup and globalTeardown use default export with es modules
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Jan 29, 2019
1 parent 832fe47 commit 2f85fd6
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 2 deletions.
33 changes: 33 additions & 0 deletions e2e/__tests__/globalSetup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
});
33 changes: 33 additions & 0 deletions e2e/__tests__/globalTeardown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
});
2 changes: 1 addition & 1 deletion e2e/global-setup/babel.config.js
Original file line number Diff line number Diff line change
@@ -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'],
};
12 changes: 12 additions & 0 deletions e2e/global-setup/invalidSetupWithNamedExport.js
Original file line number Diff line number Diff line change
@@ -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};
10 changes: 10 additions & 0 deletions e2e/global-setup/setupWithDefaultExport.js
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions e2e/global-teardown/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

module.exports = {
presets: ['@babel/preset-env', '@babel/preset-flow'],
};
12 changes: 12 additions & 0 deletions e2e/global-teardown/invalidTeardownWithNamedExport.js
Original file line number Diff line number Diff line change
@@ -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};
10 changes: 10 additions & 0 deletions e2e/global-teardown/teardownWithDefaultExport.js
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 14 additions & 1 deletion packages/jest-cli/src/runGlobalHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2f85fd6

Please sign in to comment.