Skip to content

Commit

Permalink
feat: support config files exporting (async) functions (#10001)
Browse files Browse the repository at this point in the history
  • Loading branch information
hisco committed May 9, 2020
1 parent 41e2856 commit 8294bab
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

- `[jest-config]` Support config files exporting (`async`) `function`s ([#10001](https://github.com/facebook/jest/pull/10001))

### Fixes

- `[jest-jasmine2]` Stop adding `:` after an error that has no message ([#9990](https://github.com/facebook/jest/pull/9990))
Expand Down
8 changes: 8 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ Or through JavaScript:

```js
// jest.config.js
//Sync object
module.exports = {
verbose: true,
};

//Or async function
module.exports = async () => {
return {
verbose: true,
};
};
```

Please keep in mind that the resulting configuration must be JSON-serializable.
Expand Down
40 changes: 39 additions & 1 deletion packages/jest-config/src/__tests__/readConfigs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,50 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {Config} from '@jest/types';
import {readConfigs} from '../index';

let mockResult;
jest.mock('graceful-fs', () => ({
...jest.requireActual('fs'),
existsSync: jest.fn(() => true),
lstatSync: jest.fn(() => ({
isDirectory: () => false,
})),
}));
jest.mock('../readConfigFileAndSetRootDir', () => jest.fn(() => mockResult));

test('readConfigs() throws when called without project paths', async () => {
await expect(
// @ts-ignore
readConfigs(null /* argv */, [] /* projectPaths */),
).rejects.toThrowError('jest: No configuration found for any project.');
});

test('readConfigs() loads async config file', async () => {
mockResult = jest.fn(async () => ({
rootDir: './',
}));
await expect(
// @ts-ignore
readConfigs(
<Config.Argv>{} /* argv */,
['./some-jest-config-file.js'] /* projectPaths */,
),
).resolves.toHaveProperty('configs');
expect(mockResult).toHaveBeenCalled();
});

test('readConfigs() reject if async was rejected', async () => {
mockResult = jest.fn(async () => {
throw new Error('Some error');
});
await expect(
// @ts-ignore
readConfigs(
<Config.Argv>{} /* argv */,
['./some-jest-config-file.js'] /* projectPaths */,
),
).rejects.toBeTruthy();
expect(mockResult).toHaveBeenCalled();
});
8 changes: 7 additions & 1 deletion packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export async function readConfig(
parentConfigPath?: Config.Path | null,
projectIndex: number = Infinity,
): Promise<ReadConfig> {
let rawOptions;
let rawOptions:
| Config.InitialOptions
| (() => Config.InitialOptions | Promise<Config.InitialOptions>);
let configPath = null;

if (typeof packageRootOrConfig !== 'string') {
Expand Down Expand Up @@ -82,6 +84,10 @@ export async function readConfig(
rawOptions = await readConfigFileAndSetRootDir(configPath);
}

if (typeof rawOptions === 'function') {
rawOptions = await rawOptions();
}

const {options, hasDeprecationWarnings} = normalize(
rawOptions,
argv,
Expand Down

0 comments on commit 8294bab

Please sign in to comment.