Skip to content

Commit 544fac7

Browse files
authored
feat(jest-circus, jest-jasmine2): Add support for async function in setupFilesAfterEnv (#14749)
1 parent fb2bac0 commit 544fac7

File tree

7 files changed

+89
-2
lines changed

7 files changed

+89
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `[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))
66
- `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738))
7+
- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962))
78
- `[jest-config]` [**BREAKING**] Add `mts` and `cts` to default `moduleFileExtensions` config ([#14369](https://github.com/facebook/jest/pull/14369))
89
- `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584))
910
- `[jest-config]` Loads config file from provided path in `package.json` ([#14044](https://github.com/facebook/jest/pull/14044))

docs/Configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,12 @@ const config: Config = {
16651665
export default config;
16661666
```
16671667

1668+
:::tip
1669+
1670+
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.
1671+
1672+
:::
1673+
16681674
### `showSeed` \[boolean]
16691675

16701676
Default: `false`

e2e/__tests__/setupFilesAfterEnvConfig.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,48 @@ describe('setupFilesAfterEnv', () => {
6464
expect(result.json.testResults).toHaveLength(1);
6565
expect(result.exitCode).toBe(0);
6666
});
67+
68+
it('awaits async function returned from the setup file (jest-circus)', () => {
69+
const pkgJson = {
70+
jest: {
71+
setupFilesAfterEnv: ['./setupAsyncFunction.js'],
72+
testRunner: 'jest-circus',
73+
},
74+
};
75+
76+
writeFiles(DIR, {
77+
'package.json': JSON.stringify(pkgJson, null, 2),
78+
});
79+
80+
const result = runWithJson('setup-files-after-env-config', [
81+
'setupAsyncFunction.test.js',
82+
]);
83+
84+
expect(result.json.numTotalTests).toBe(1);
85+
expect(result.json.numPassedTests).toBe(1);
86+
expect(result.json.testResults).toHaveLength(1);
87+
expect(result.exitCode).toBe(0);
88+
});
89+
90+
it('awaits async function returned from the setup file (jest-jasmine2)', () => {
91+
const pkgJson = {
92+
jest: {
93+
setupFilesAfterEnv: ['./setupAsyncFunction.js'],
94+
testRunner: 'jest-jasmine2',
95+
},
96+
};
97+
98+
writeFiles(DIR, {
99+
'package.json': JSON.stringify(pkgJson, null, 2),
100+
});
101+
102+
const result = runWithJson('setup-files-after-env-config', [
103+
'setupAsyncFunction.test.js',
104+
]);
105+
106+
expect(result.json.numTotalTests).toBe(1);
107+
expect(result.json.numPassedTests).toBe(1);
108+
expect(result.json.testResults).toHaveLength(1);
109+
expect(result.exitCode).toBe(0);
110+
});
67111
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
describe('setupFilesAfterEnv', () => {
10+
it('has waited for async function', () => {
11+
expect(globalThis.afterEnvAsyncFunctionFinished).toBe(true);
12+
});
13+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
globalThis.afterEnvAsyncFunctionFinished = false;
9+
10+
module.exports = async () => {
11+
await new Promise(resolve =>
12+
setTimeout(() => {
13+
globalThis.afterEnvAsyncFunctionFinished = true;
14+
resolve();
15+
}, 2000),
16+
);
17+
};

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ const jestAdapter = async (
8181
if (esm) {
8282
await runtime.unstable_importModule(path);
8383
} else {
84-
runtime.requireModule(path);
84+
const setupFile = runtime.requireModule(path);
85+
if (typeof setupFile === 'function') {
86+
await setupFile();
87+
}
8588
}
8689
}
8790
const setupAfterEnvEnd = Date.now();

packages/jest-jasmine2/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ export default async function jasmine2(
180180
if (esm) {
181181
await runtime.unstable_importModule(path);
182182
} else {
183-
runtime.requireModule(path);
183+
const setupFile = runtime.requireModule(path);
184+
if (typeof setupFile === 'function') {
185+
await setupFile();
186+
}
184187
}
185188
}
186189

0 commit comments

Comments
 (0)