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

Add coverageProvider to jest --init #10044

Closed
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- `[jest-config]` Support config files exporting (`async`) `function`s ([#10001](https://github.com/facebook/jest/pull/10001))
- `[jest-cli, jest-core]` Add `--selectProjects` CLI argument to filter test suites by project name ([#8612](https://github.com/facebook/jest/pull/8612))
- `[jest-cli, jest-init]` Add `coverageProvider` to `jest --init` prompts ([#10044](https://github.com/facebook/jest/pull/10044))

### Fixes

Expand Down
22 changes: 22 additions & 0 deletions packages/jest-cli/src/init/__tests__/init.test.js
Expand Up @@ -93,6 +93,28 @@ describe('init', () => {
expect(evaluatedConfig).toEqual({coverageDirectory: 'coverage'});
});

it('should create configuration for {coverageProvider: "babel"}', async () => {
prompts.mockReturnValueOnce({coverageProvider: 'babel'});

await init(resolveFromFixture('only_package_json'));

const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
// should modify when the default coverageProvider will be changed to "v8"
expect(evaluatedConfig).toEqual({});
});

it('should create configuration for {coverageProvider: "v8"}', async () => {
prompts.mockReturnValueOnce({coverageProvider: 'v8'});

await init(resolveFromFixture('only_package_json'));

const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
const evaluatedConfig = eval(writtenJestConfig);
// should modify when the default coverageProvider will be changed to "v8"
expect(evaluatedConfig).toEqual({coverageProvider: 'v8'});
});

it('should create configuration for {environment: "jsdom"}', async () => {
prompts.mockReturnValueOnce({environment: 'jsdom'});

Expand Down
8 changes: 7 additions & 1 deletion packages/jest-cli/src/init/generate_config_file.ts
Expand Up @@ -35,7 +35,7 @@ const generateConfigFile = (
results: Record<string, unknown>,
generateEsm = false,
): string => {
const {coverage, clearMocks, environment} = results;
const {coverage, coverageProvider, clearMocks, environment} = results;

const overrides: Record<string, any> = {};

Expand All @@ -45,6 +45,12 @@ const generateConfigFile = (
});
}

if (coverageProvider === 'v8') {
Object.assign(overrides, {
coverageProvider: 'v8',
});
}

if (environment === 'node') {
Object.assign(overrides, {
testEnvironment: 'node',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/src/init/index.ts
Expand Up @@ -28,6 +28,7 @@ const {
type PromptsResults = {
clearMocks: boolean;
coverage: boolean;
coverageProvider: boolean;
environment: boolean;
scripts: boolean;
};
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-cli/src/init/questions.ts
Expand Up @@ -24,6 +24,16 @@ const defaultQuestions: Array<PromptObject> = [
name: 'coverage',
type: 'confirm',
},
{
choices: [
{title: 'babel', value: 'babel'},
{title: 'v8', value: 'v8'},
],
initial: 0,
message: 'Which coverageProvider should be used to collect coverage?',
name: 'coverageProvider',
type: 'select',
},
{
initial: false,
message: 'Automatically clear mock calls and instances between every test?',
Expand Down