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 21, 2022
1 parent f9d4fc4 commit 29f0422
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
10 changes: 8 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,19 +456,23 @@ _Note: Any global variables that are defined through `globalSetup` can only be r

_Note: While code transformation is applied to the linked setup-file, Jest will **not** transform any code in `node_modules`. This is due to the need to load the actual transformers (e.g. `babel` or `typescript`) to perform transformation._

\_Note: User can access Jest [global config](https://github.com/facebook/jest/blob/main/packages/jest-types/src/Config.ts#L282) and [project config](https://github.com/facebook/jest/blob/main/packages/jest-types/src/Config.ts#L347) as arguments for the function.

Example:

```js title="setup.js"
// can be synchronous
module.exports = async () => {
module.exports = async (globalConfig, projectConfig) => {
// ...
// Can access and use Jest global config and project config
// Set reference to mongod in order to close the server during teardown.
globalThis.__MONGOD__ = mongod;
};
```

```js title="teardown.js"
module.exports = async function () {
module.exports = async function (globalConfig, projectConfig) {
// Can access and use Jest global config and project config
await globalThis.__MONGOD__.stop();
};
```
Expand All @@ -483,6 +487,8 @@ _Note: A global teardown module configured in a project (using multi-project run

_Note: The same caveat concerning transformation of `node_modules` as for `globalSetup` applies to `globalTeardown`._

\_Note: User can access Jest [global config](https://github.com/facebook/jest/blob/main/packages/jest-types/src/Config.ts#L282) and [project config](https://github.com/facebook/jest/blob/main/packages/jest-types/src/Config.ts#L347) as arguments for the function.

### `haste` \[object]

Default: `undefined`
Expand Down
8 changes: 5 additions & 3 deletions e2e/__tests__/globalSetup.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 29f0422

Please sign in to comment.