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: support config files exporting (async) functions #10001

Merged
merged 4 commits into from May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
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
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