From c9a3d370f117a51e29df8f5e4b540e9172a7125a Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 25 Feb 2022 11:18:52 +0200 Subject: [PATCH] feat(jest-runner): allow `setupFiles` module to export an async function (#12042) --- CHANGELOG.md | 1 + docs/Configuration.md | 6 ++++++ e2e/__tests__/setupFiles.test.ts | 13 +++++++++++++ e2e/setup-files/__tests__/setup.test.js | 12 ++++++++++++ e2e/setup-files/fetched-data.js | 10 ++++++++++ e2e/setup-files/package.json | 7 +++++++ e2e/setup-files/setup-fetchdata.js | 21 +++++++++++++++++++++ packages/jest-repl/src/cli/runtime-cli.ts | 6 +++++- packages/jest-runner/src/runTest.ts | 5 ++++- 9 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 e2e/__tests__/setupFiles.test.ts create mode 100644 e2e/setup-files/__tests__/setup.test.js create mode 100644 e2e/setup-files/fetched-data.js create mode 100644 e2e/setup-files/package.json create mode 100644 e2e/setup-files/setup-fetchdata.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae06c8a4e65..4575a85de209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - `[jest-resolve]` [**BREAKING**] Add support for `package.json` `exports` ([#11961](https://github.com/facebook/jest/pull/11961), [#12373](https://github.com/facebook/jest/pull/12373)) - `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392)) - `[jest-resolve, jest-runtime]` Add support for async resolver ([#11540](https://github.com/facebook/jest/pull/11540)) +- `[jest-runner]` Allow `setupFiles` module to export an async function ([#12042](https://github.com/facebook/jest/pull/12042)) - `[jest-runner]` Allow passing `testEnvironmentOptions` via docblocks ([#12470](https://github.com/facebook/jest/pull/12470)) - `[jest-runtime]` [**BREAKING**] `Runtime.createHasteMap` now returns a promise ([#12008](https://github.com/facebook/jest/pull/12008)) - `[@jest/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384)) diff --git a/docs/Configuration.md b/docs/Configuration.md index 9552ca526793..ecba5dca19c0 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -925,6 +925,12 @@ Default: `[]` A list of paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment before executing [`setupFilesAfterEnv`](#setupfilesafterenv-array) and before the test code itself. +:::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. + +::: + ### `setupFilesAfterEnv` \[array] Default: `[]` diff --git a/e2e/__tests__/setupFiles.test.ts b/e2e/__tests__/setupFiles.test.ts new file mode 100644 index 000000000000..9773178553f0 --- /dev/null +++ b/e2e/__tests__/setupFiles.test.ts @@ -0,0 +1,13 @@ +/** + * 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. + */ + +import runJest from '../runJest'; + +test('invokes async function exported from `setupFiles` module', () => { + const result = runJest('setup-files'); + expect(result.exitCode).toBe(0); +}); diff --git a/e2e/setup-files/__tests__/setup.test.js b/e2e/setup-files/__tests__/setup.test.js new file mode 100644 index 000000000000..02ec10bb2f28 --- /dev/null +++ b/e2e/setup-files/__tests__/setup.test.js @@ -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. + */ + +const fetchedData = require('../fetched-data'); + +test('fetches mock data', () => { + expect(fetchedData.RESPONSE).toBe('mock-fetched-data'); +}); diff --git a/e2e/setup-files/fetched-data.js b/e2e/setup-files/fetched-data.js new file mode 100644 index 000000000000..0be269354795 --- /dev/null +++ b/e2e/setup-files/fetched-data.js @@ -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. + */ + +module.exports = { + RESPONSE: 'not-yet-received', +}; diff --git a/e2e/setup-files/package.json b/e2e/setup-files/package.json new file mode 100644 index 000000000000..d52876239b6a --- /dev/null +++ b/e2e/setup-files/package.json @@ -0,0 +1,7 @@ +{ + "jest": { + "setupFiles": [ + "./setup-fetchdata.js" + ] + } +} diff --git a/e2e/setup-files/setup-fetchdata.js b/e2e/setup-files/setup-fetchdata.js new file mode 100644 index 000000000000..1e917a8ca24d --- /dev/null +++ b/e2e/setup-files/setup-fetchdata.js @@ -0,0 +1,21 @@ +/** + * 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. + */ + +const fetchedData = require('./fetched-data'); + +function mockFetchData(mockData) { + return new Promise(resolve => { + setTimeout(() => { + resolve(mockData); + }, 2000); + }); +} + +module.exports = async () => { + const response = await mockFetchData('mock-fetched-data'); + fetchedData.RESPONSE = response; +}; diff --git a/packages/jest-repl/src/cli/runtime-cli.ts b/packages/jest-repl/src/cli/runtime-cli.ts index c5c444e3de5b..f814911582a7 100644 --- a/packages/jest-repl/src/cli/runtime-cli.ts +++ b/packages/jest-repl/src/cli/runtime-cli.ts @@ -115,9 +115,13 @@ export async function run( if (esm) { await runtime.unstable_importModule(path); } else { - runtime.requireModule(path); + const setupFile = runtime.requireModule(path); + if (typeof setupFile === 'function') { + await setupFile(); + } } } + const esm = runtime.unstable_shouldLoadAsEsm(filePath); if (esm) { diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index c0ad57bbc93c..dd6247751661 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -207,7 +207,10 @@ async function runTestInternal( if (esm) { await runtime.unstable_importModule(path); } else { - runtime.requireModule(path); + const setupFile = runtime.requireModule(path); + if (typeof setupFile === 'function') { + await setupFile(); + } } }