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

globalSetup and globalTeardown use default export with es modules #7750

Merged
merged 3 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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 export a function 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);
}
7 changes: 6 additions & 1 deletion packages/jest-cli/src/runGlobalHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -59,7 +64,7 @@ export default ({
);

// $FlowFixMe
const globalModule = require(modulePath);
const globalModule = _interopRequireDefault(require(modulePath)).default;
chrisblossom marked this conversation as resolved.
Show resolved Hide resolved

if (typeof globalModule !== 'function') {
throw new TypeError(
Expand Down