Skip to content

Commit

Permalink
globalSetup and globalTeardown use default export with es modules (#7750
Browse files Browse the repository at this point in the history
)
  • Loading branch information
chrisblossom authored and SimenB committed Jan 29, 2019
1 parent 09c5003 commit d0b2c24
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 2 deletions.
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 export a function 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;

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

0 comments on commit d0b2c24

Please sign in to comment.