Skip to content

Commit

Permalink
feat(jest-core): Pass project config to globalSetup/`globalTeardown…
Browse files Browse the repository at this point in the history
…` function as 2nd argument

Closes #12436
  • Loading branch information
ahnpnl committed Feb 20, 2022
1 parent 7d4595e commit f9eec12
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
- `[jest-config]` [**BREAKING**] Stop shipping `jest-environment-jsdom` by default ([#12354](https://github.com/facebook/jest/pull/12354))
- `[jest-config]` [**BREAKING**] Stop shipping `jest-jasmine2` by default ([#12355](https://github.com/facebook/jest/pull/12355))
- `[jest-config, @jest/types]` Add `ci` to `GlobalConfig` ([#12378](https://github.com/facebook/jest/pull/12378))
- `[jest-core]` Pass project config to `globalSetup`/`globalTeardown` function as second argument ([#12440](https://github.com/facebook/jest/pull/12440))
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290))
- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924))
- `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924))
Expand Down
8 changes: 5 additions & 3 deletions e2e/__tests__/globalSetup.test.ts
Expand Up @@ -81,17 +81,18 @@ test('jest throws an error when globalSetup does not export a function', () => {
);
});

test('globalSetup function gets jest config object as a parameter', () => {
test('globalSetup function gets global config object and project config as parameters', () => {
const setupPath = path.resolve(e2eDir, 'setupWithConfig.js');

const testPathPattern = 'pass';

const result = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
`--testPathPattern=${testPathPattern}`,
'--cache=true',
]);

expect(result.stdout).toBe(testPathPattern);
expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
});

test('should call globalSetup function of multiple projects', () => {
Expand Down Expand Up @@ -145,9 +146,10 @@ test('globalSetup works with default export', () => {
const result = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
`--testPathPattern=${testPathPattern}`,
'--cache=true',
]);

expect(result.stdout).toBe(testPathPattern);
expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
});

test('globalSetup throws with named export', () => {
Expand Down
8 changes: 5 additions & 3 deletions e2e/__tests__/globalTeardown.test.ts
Expand Up @@ -65,17 +65,18 @@ test('jest throws an error when globalTeardown does not export a function', () =
);
});

test('globalTeardown function gets jest config object as a parameter', () => {
test('globalSetup function gets global config object and project config as parameters', () => {
const teardownPath = path.resolve(e2eDir, 'teardownWithConfig.js');

const testPathPattern = 'pass';

const result = runJest(e2eDir, [
`--globalTeardown=${teardownPath}`,
`--testPathPattern=${testPathPattern}`,
'--cache=true',
]);

expect(result.stdout).toBe(testPathPattern);
expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
});

test('should call globalTeardown function of multiple projects', () => {
Expand Down Expand Up @@ -113,9 +114,10 @@ test('globalTeardown works with default export', () => {
const result = runJest(e2eDir, [
`--globalTeardown=${teardownPath}`,
`--testPathPattern=${testPathPattern}`,
'--cache=true',
]);

expect(result.stdout).toBe(testPathPattern);
expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
});

test('globalTeardown throws with named export', () => {
Expand Down
5 changes: 3 additions & 2 deletions e2e/global-setup/setupWithConfig.js
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

module.exports = function (jestConfig) {
console.log(jestConfig.testPathPattern);
module.exports = function (globalConfig, projectConfig) {
console.log(globalConfig.testPathPattern);
console.log(projectConfig.cache);
};
5 changes: 3 additions & 2 deletions e2e/global-setup/setupWithDefaultExport.js
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

export default function (jestConfig): void {
console.log(jestConfig.testPathPattern);
export default function (globalConfig, projectConfig): void {
console.log(globalConfig.testPathPattern);
console.log(projectConfig.cache);
}
5 changes: 3 additions & 2 deletions e2e/global-teardown/teardownWithConfig.js
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

module.exports = function (jestConfig) {
console.log(jestConfig.testPathPattern);
module.exports = function (globalConfig, projectConfig) {
console.log(globalConfig.testPathPattern);
console.log(projectConfig.cache);
};
5 changes: 3 additions & 2 deletions e2e/global-teardown/teardownWithDefaultExport.js
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

export default function (jestConfig): void {
console.log(jestConfig.testPathPattern);
export default function (globalConfig, projectConfig): void {
console.log(globalConfig.testPathPattern);
console.log(projectConfig.cache);
}
2 changes: 1 addition & 1 deletion packages/jest-core/src/runGlobalHook.ts
Expand Up @@ -55,7 +55,7 @@ export default async function runGlobalHook({
);
}

await globalModule(globalConfig);
await globalModule(globalConfig, projectConfig);
},
);
} catch (error) {
Expand Down

0 comments on commit f9eec12

Please sign in to comment.