Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-runner): allow setupFiles module to export an async function #12042

Merged
merged 4 commits into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions docs/Configuration.md
Expand Up @@ -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.

:::note
SimenB marked this conversation as resolved.
Show resolved Hide resolved

If your setup script is a CJS module, it may export an async function. Jest will call the function and will await for the result. This might be useful to fetch some data asynchronously. In case of ESM module, simply use top-level await to achieve the same result.

SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

### `setupFilesAfterEnv` \[array]

Default: `[]`
Expand Down
13 changes: 13 additions & 0 deletions 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);
});
12 changes: 12 additions & 0 deletions 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');
});
10 changes: 10 additions & 0 deletions 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',
};
7 changes: 7 additions & 0 deletions e2e/setup-files/package.json
@@ -0,0 +1,7 @@
{
"jest": {
"setupFiles": [
"./setup-fetchdata.js"
]
}
}
21 changes: 21 additions & 0 deletions 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;
};
5 changes: 5 additions & 0 deletions packages/jest-repl/src/cli/runtime-cli.ts
Expand Up @@ -116,8 +116,13 @@ export async function run(
await runtime.unstable_importModule(path);
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved
} else {
runtime.requireModule(path);
SimenB marked this conversation as resolved.
Show resolved Hide resolved
const setupFile = runtime.requireModule(path);
if (typeof setupFile === 'function') {
await setupFile();
}
}
}

const esm = runtime.unstable_shouldLoadAsEsm(filePath);

if (esm) {
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-runner/src/runTest.ts
Expand Up @@ -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();
}
}
}

Expand Down