From fc820f7600c5e855a11c1880dbcadd9babffb006 Mon Sep 17 00:00:00 2001 From: ahnpnl <27772165+ahnpnl@users.noreply.github.com> Date: Sun, 20 Feb 2022 13:29:50 +0100 Subject: [PATCH] feat(jest-core): Pass project config to `globalSetup`/`globalTeardown` function as 2nd argument Closes #12436 --- CHANGELOG.md | 1 + docs/Configuration.md | 26 +++++++++++++++++++ e2e/__tests__/globalSetup.test.ts | 8 +++--- e2e/__tests__/globalTeardown.test.ts | 8 +++--- e2e/global-setup/setupWithConfig.js | 5 ++-- e2e/global-setup/setupWithDefaultExport.js | 5 ++-- e2e/global-teardown/teardownWithConfig.js | 5 ++-- .../teardownWithDefaultExport.js | 5 ++-- packages/jest-core/src/runGlobalHook.ts | 2 +- 9 files changed, 50 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f3284eb17e..8a3ab63b37e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/docs/Configuration.md b/docs/Configuration.md index ec927f3dbf9d..d7c47c15c636 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -456,8 +456,12 @@ _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: +- Without config + ```js title="setup.js" // can be synchronous module.exports = async () => { @@ -473,6 +477,26 @@ module.exports = async function () { }; ``` +- With config + +```js title="setup-with-config.js" +// can be synchronous +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. + global.__MONGOD__ = mongod; +}; +``` + +```js title="teardown-with-config.js" +module.exports = async function (globalConfig, projectConfig) { + // ... + // Can access and use Jest global config and project config + await global.__MONGOD__.stop(); +}; +``` + ### `globalTeardown` \[string] Default: `undefined` @@ -483,6 +507,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` diff --git a/e2e/__tests__/globalSetup.test.ts b/e2e/__tests__/globalSetup.test.ts index 07ceba95a03a..05dee3a4cdfa 100644 --- a/e2e/__tests__/globalSetup.test.ts +++ b/e2e/__tests__/globalSetup.test.ts @@ -81,7 +81,7 @@ 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'; @@ -89,9 +89,10 @@ test('globalSetup function gets jest config object as a parameter', () => { 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', () => { @@ -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', () => { diff --git a/e2e/__tests__/globalTeardown.test.ts b/e2e/__tests__/globalTeardown.test.ts index 8e0bf6e64b33..5f43bc4ecf84 100644 --- a/e2e/__tests__/globalTeardown.test.ts +++ b/e2e/__tests__/globalTeardown.test.ts @@ -65,7 +65,7 @@ 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'; @@ -73,9 +73,10 @@ test('globalTeardown function gets jest config object as a parameter', () => { 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', () => { @@ -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', () => { diff --git a/e2e/global-setup/setupWithConfig.js b/e2e/global-setup/setupWithConfig.js index 78e2b910c082..c97316ec0b53 100644 --- a/e2e/global-setup/setupWithConfig.js +++ b/e2e/global-setup/setupWithConfig.js @@ -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); }; diff --git a/e2e/global-setup/setupWithDefaultExport.js b/e2e/global-setup/setupWithDefaultExport.js index ea8cd34e38c1..6a075f9365d2 100644 --- a/e2e/global-setup/setupWithDefaultExport.js +++ b/e2e/global-setup/setupWithDefaultExport.js @@ -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); } diff --git a/e2e/global-teardown/teardownWithConfig.js b/e2e/global-teardown/teardownWithConfig.js index 78e2b910c082..c97316ec0b53 100644 --- a/e2e/global-teardown/teardownWithConfig.js +++ b/e2e/global-teardown/teardownWithConfig.js @@ -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); }; diff --git a/e2e/global-teardown/teardownWithDefaultExport.js b/e2e/global-teardown/teardownWithDefaultExport.js index ea8cd34e38c1..6a075f9365d2 100644 --- a/e2e/global-teardown/teardownWithDefaultExport.js +++ b/e2e/global-teardown/teardownWithDefaultExport.js @@ -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); } diff --git a/packages/jest-core/src/runGlobalHook.ts b/packages/jest-core/src/runGlobalHook.ts index 93cf857ac4cd..80440e59f9a5 100644 --- a/packages/jest-core/src/runGlobalHook.ts +++ b/packages/jest-core/src/runGlobalHook.ts @@ -55,7 +55,7 @@ export default async function runGlobalHook({ ); } - await globalModule(globalConfig); + await globalModule(globalConfig, projectConfig); }, ); } catch (error) {