Skip to content

Commit

Permalink
Add coverageProvider to jest --init prompt (#10109)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsgap committed May 31, 2020
1 parent beba320 commit 5809534
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ module.exports = {
// \\"/node_modules/\\"
// ],
// Indicates which provider should be used to instrument code for coverage
// coverageProvider: \\"babel\\",
// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// \\"json\\",
Expand Down
22 changes: 22 additions & 0 deletions packages/jest-cli/src/init/__tests__/init.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const defaultQuestions: Array<PromptObject> = [
name: 'coverage',
type: 'confirm',
},
{
choices: [
{title: 'v8', value: 'v8'},
{title: 'babel', value: 'babel'},
],
initial: 0,
message: 'Which provider should be used to instrument code for coverage?',
name: 'coverageProvider',
type: 'select',
},
{
initial: false,
message: 'Automatically clear mock calls and instances between every test?',
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/Descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const descriptions: {[key in keyof Config.InitialOptions]: string} = {
'The directory where Jest should output its coverage files',
coveragePathIgnorePatterns:
'An array of regexp pattern strings used to skip coverage collection',
coverageProvider:
'Indicates which provider should be used to instrument code for coverage',
coverageReporters:
'A list of reporter names that Jest uses when writing coverage reports',
coverageThreshold:
Expand Down

0 comments on commit 5809534

Please sign in to comment.