From 66ef648bca69737260e75eaf7dcabd7e45487905 Mon Sep 17 00:00:00 2001 From: Uladzislau Kamliou Date: Mon, 11 Dec 2023 19:52:16 +0700 Subject: [PATCH 1/4] Add support for async function in setupFilesAfterEnv --- CHANGELOG.md | 1 + .../setupFilesAfterEnvConfig.test.ts | 44 +++++++++++++++++++ .../__tests__/setupAsyncFunction.test.js | 13 ++++++ .../setupAsyncFunction.js | 10 +++++ .../legacy-code-todo-rewrite/jestAdapter.ts | 5 ++- packages/jest-jasmine2/src/index.ts | 5 ++- 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 e2e/setup-files-after-env-config/__tests__/setupAsyncFunction.test.js create mode 100644 e2e/setup-files-after-env-config/setupAsyncFunction.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 023069f47135..a79d79067293 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -648,6 +648,7 @@ - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343)) - `[jest-worker]` [**BREAKING**] Default to advanced serialization when using child process workers ([#10983](https://github.com/facebook/jest/pull/10983)) - `[pretty-format]` New `maxWidth` parameter ([#12402](https://github.com/facebook/jest/pull/12402)) +- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962)) ### Fixes diff --git a/e2e/__tests__/setupFilesAfterEnvConfig.test.ts b/e2e/__tests__/setupFilesAfterEnvConfig.test.ts index 031b98b2ce54..953f86a874c4 100644 --- a/e2e/__tests__/setupFilesAfterEnvConfig.test.ts +++ b/e2e/__tests__/setupFilesAfterEnvConfig.test.ts @@ -64,4 +64,48 @@ describe('setupFilesAfterEnv', () => { expect(result.json.testResults).toHaveLength(1); expect(result.exitCode).toBe(0); }); + + it('awaits async function returned from the setup file (jest-circus)', () => { + const pkgJson = { + jest: { + setupFilesAfterEnv: ['./setupAsyncFunction.js'], + testRunner: 'jest-circus', + }, + }; + + writeFiles(DIR, { + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + + const result = runWithJson('setup-files-after-env-config', [ + 'setupAsyncFunction.test.js', + ]); + + expect(result.json.numTotalTests).toBe(1); + expect(result.json.numPassedTests).toBe(1); + expect(result.json.testResults).toHaveLength(1); + expect(result.exitCode).toBe(0); + }); + + it('awaits async function returned from the setup file (jest-jasmine2)', () => { + const pkgJson = { + jest: { + setupFilesAfterEnv: ['./setupAsyncFunction.js'], + testRunner: 'jest-jasmine2', + }, + }; + + writeFiles(DIR, { + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + + const result = runWithJson('setup-files-after-env-config', [ + 'setupAsyncFunction.test.js', + ]); + + expect(result.json.numTotalTests).toBe(1); + expect(result.json.numPassedTests).toBe(1); + expect(result.json.testResults).toHaveLength(1); + expect(result.exitCode).toBe(0); + }); }); diff --git a/e2e/setup-files-after-env-config/__tests__/setupAsyncFunction.test.js b/e2e/setup-files-after-env-config/__tests__/setupAsyncFunction.test.js new file mode 100644 index 000000000000..0fa60b5f5424 --- /dev/null +++ b/e2e/setup-files-after-env-config/__tests__/setupAsyncFunction.test.js @@ -0,0 +1,13 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +'use strict'; + +describe('setupFilesAfterEnv', () => { + it('has waited for async function', () => { + expect(globalThis.afterEnvAsyncFunctionFinished).toBe(true); + }); +}); diff --git a/e2e/setup-files-after-env-config/setupAsyncFunction.js b/e2e/setup-files-after-env-config/setupAsyncFunction.js new file mode 100644 index 000000000000..0450cff298c8 --- /dev/null +++ b/e2e/setup-files-after-env-config/setupAsyncFunction.js @@ -0,0 +1,10 @@ +globalThis.afterEnvAsyncFunctionFinished = false; + +module.exports = async () => { + await new Promise(resolve => + setTimeout(() => { + globalThis.afterEnvAsyncFunctionFinished = true; + resolve(); + }, 2000), + ); +}; diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts index 2a16a8635a66..b665185ed200 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -81,7 +81,10 @@ const jestAdapter = async ( if (esm) { await runtime.unstable_importModule(path); } else { - runtime.requireModule(path); + const setupFile = runtime.requireModule(path); + if (typeof setupFile === 'function') { + await setupFile(); + } } } const setupAfterEnvEnd = Date.now(); diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index 09f575240981..65ba78819ddb 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -180,7 +180,10 @@ export default async function jasmine2( if (esm) { await runtime.unstable_importModule(path); } else { - runtime.requireModule(path); + const setupFile = runtime.requireModule(path); + if (typeof setupFile === 'function') { + await setupFile(); + } } } From e350794716eda94808e4c1748677eddb99a0587b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 25 Dec 2023 12:21:23 +0100 Subject: [PATCH 2/4] move changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a79d79067293..a44aba119d49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-circus, jest-cli, jest-config]` Add `waitNextEventLoopTurnForUnhandledRejectionEvents` flag to minimise performance impact of correct detection of unhandled promise rejections introduced in [#14315](https://github.com/jestjs/jest/pull/14315) ([#14681](https://github.com/jestjs/jest/pull/14681)) +- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962)) - `[jest-config]` [**BREAKING**] Add `mts` and `cts` to default `moduleFileExtensions` config ([#14369](https://github.com/facebook/jest/pull/14369)) - `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584)) - `[jest-config]` Loads config file from provided path in `package.json` ([#14044](https://github.com/facebook/jest/pull/14044)) @@ -648,7 +649,6 @@ - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343)) - `[jest-worker]` [**BREAKING**] Default to advanced serialization when using child process workers ([#10983](https://github.com/facebook/jest/pull/10983)) - `[pretty-format]` New `maxWidth` parameter ([#12402](https://github.com/facebook/jest/pull/12402)) -- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962)) ### Fixes From 7212d60f212c41c2dd48b529506663b6c575e77c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 25 Dec 2023 12:22:23 +0100 Subject: [PATCH 3/4] add tip to docs --- docs/Configuration.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/Configuration.md b/docs/Configuration.md index 208f188d22d0..336c1677f9fe 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1665,6 +1665,12 @@ const config: Config = { export default config; ``` +:::tip + +If your setup script is a CJS module, it may export an async function. Jest will call the function and await its result. This might be useful to fetch some data asynchronously. If the file is an ESM module, simply use top-level await to achieve the same result. + +::: + ### `showSeed` \[boolean] Default: `false` From 187db7ce3dc1141f75a6bfb1b94bcecb428ec446 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 1 Jan 2024 23:00:21 +0100 Subject: [PATCH 4/4] copyright headers --- e2e/setup-files-after-env-config/setupAsyncFunction.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/e2e/setup-files-after-env-config/setupAsyncFunction.js b/e2e/setup-files-after-env-config/setupAsyncFunction.js index 0450cff298c8..ec17aaa05065 100644 --- a/e2e/setup-files-after-env-config/setupAsyncFunction.js +++ b/e2e/setup-files-after-env-config/setupAsyncFunction.js @@ -1,3 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + globalThis.afterEnvAsyncFunctionFinished = false; module.exports = async () => {