Skip to content

Commit 7109b8c

Browse files
authored
fix: call exported config function (#11475)
1 parent 0927834 commit 7109b8c

File tree

4 files changed

+6
-36
lines changed

4 files changed

+6
-36
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `[jest-circus]` Add missing `slash` dependency ([#11465](https://github.com/facebook/jest/pull/11465))
1212
- `[jest-circus, @jest/test-sequencer]` Remove dependency on `jest-runner` ([#11466](https://github.com/facebook/jest/pull/11466))
1313
- `[jest-config]` Resolve `config.runner` to absolute path ([#11465](https://github.com/facebook/jest/pull/11465))
14+
- `[jest-config]` Make sure to support functions as config ([#11475](https://github.com/facebook/jest/pull/11475))
1415
- `[jest-core]` Do not warn about `DNSCHANNEL` handles when using the `--detectOpenHandles` option ([#11470](https://github.com/facebook/jest/pull/11470))
1516
- `[jest-runner]` Remove dependency on `jest-config` ([#11466](https://github.com/facebook/jest/pull/11466))
1617
- `[jest-worker]` Loosen engine requirement to `>= 10.13.0` ([#11451](https://github.com/facebook/jest/pull/11451))

packages/jest-config/src/__tests__/readConfigs.test.ts

-29
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,19 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
import {Config} from '@jest/types';
87
import {readConfigs} from '../index';
98

10-
let mockResult;
119
jest.mock('graceful-fs', () => ({
1210
...jest.requireActual('fs'),
1311
existsSync: jest.fn(() => true),
1412
lstatSync: jest.fn(() => ({
1513
isDirectory: () => false,
1614
})),
1715
}));
18-
jest.mock('../readConfigFileAndSetRootDir', () => jest.fn(() => mockResult));
1916

2017
test('readConfigs() throws when called without project paths', async () => {
2118
await expect(
2219
// @ts-expect-error
2320
readConfigs(null /* argv */, [] /* projectPaths */),
2421
).rejects.toThrowError('jest: No configuration found for any project.');
2522
});
26-
27-
test('readConfigs() loads async config file', async () => {
28-
mockResult = jest.fn(async () => ({
29-
rootDir: './',
30-
}));
31-
await expect(
32-
readConfigs(
33-
<Config.Argv>{} /* argv */,
34-
['./some-jest-config-file.js'] /* projectPaths */,
35-
),
36-
).resolves.toHaveProperty('configs');
37-
expect(mockResult).toHaveBeenCalled();
38-
});
39-
40-
test('readConfigs() reject if async was rejected', async () => {
41-
mockResult = jest.fn(async () => {
42-
throw new Error('Some error');
43-
});
44-
await expect(
45-
readConfigs(
46-
<Config.Argv>{} /* argv */,
47-
['./some-jest-config-file.js'] /* projectPaths */,
48-
),
49-
).rejects.toBeTruthy();
50-
expect(mockResult).toHaveBeenCalled();
51-
});

packages/jest-config/src/index.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ export async function readConfig(
4545
parentConfigDirname?: Config.Path | null,
4646
projectIndex: number = Infinity,
4747
): Promise<ReadConfig> {
48-
let rawOptions:
49-
| Config.InitialOptions
50-
| (() => Config.InitialOptions | Promise<Config.InitialOptions>);
48+
let rawOptions: Config.InitialOptions;
5149
let configPath = null;
5250

5351
if (typeof packageRootOrConfig !== 'string') {
@@ -87,10 +85,6 @@ export async function readConfig(
8785
rawOptions = await readConfigFileAndSetRootDir(configPath);
8886
}
8987

90-
if (typeof rawOptions === 'function') {
91-
rawOptions = await rawOptions();
92-
}
93-
9488
const {options, hasDeprecationWarnings} = await normalize(
9589
rawOptions,
9690
argv,

packages/jest-config/src/readConfigFileAndSetRootDir.ts

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export default async function readConfigFileAndSetRootDir(
5858
configObject = configObject.jest || {};
5959
}
6060

61+
if (typeof configObject === 'function') {
62+
configObject = await configObject();
63+
}
64+
6165
if (configObject.rootDir) {
6266
// We don't touch it if it has an absolute path specified
6367
if (!path.isAbsolute(configObject.rootDir)) {

0 commit comments

Comments
 (0)