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

Support different coverage options in projects #9633

Closed
wants to merge 3 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ junit.xml
.yarn/unplugged/
.yarn/build-state.yml
e2e/async-regenerator/.yarn/

/.history
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-config]` [**BREAKING**] Move coverage options to project config ([#9633](https://github.com/facebook/jest/pull/9633))
- `[jest-console]` Add code frame to `console.error` and `console.warn` ([#9741](https://github.com/facebook/jest/pull/9741))
- `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801))

Expand Down
6 changes: 3 additions & 3 deletions TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
changedFilesWithAncestor: false,
changedSince: '',
collectCoverage: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not all the coverage* options?

collectCoverageFrom: [],
collectCoverageOnlyFrom: null,
coverageDirectory: 'coverage',
coverageProvider: 'babel',
coverageReporters: [],
coverageThreshold: {global: {}},
detectLeaks: false,
detectOpenHandles: false,
enabledTestsMap: null,
Expand Down Expand Up @@ -71,7 +68,10 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
cache: false,
cacheDirectory: '/test_cache_dir/',
clearMocks: false,
collectCoverageFrom: [],
collectCoverageOnlyFrom: null,
coveragePathIgnorePatterns: [],
coverageThreshold: {global: {}},
cwd: '/test_root_dir/',
detectLeaks: false,
detectOpenHandles: false,
Expand Down
2 changes: 0 additions & 2 deletions docs/WatchPlugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ For stability and safety reasons, only part of the global configuration keys can
- [`bail`](configuration.html#bail-number--boolean)
- [`changedSince`](cli.html#--changedsince)
- [`collectCoverage`](configuration.html#collectcoverage-boolean)
- [`collectCoverageFrom`](configuration.html#collectcoveragefrom-array)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think watch plugins should still be able to set these, somehow... Might have to solve watchplugins setting project options before we can land this

- [`collectCoverageOnlyFrom`](configuration.html#collectcoverageonlyfrom-array)
- [`coverageDirectory`](configuration.html#coveragedirectory-string)
- [`coverageReporters`](configuration.html#coveragereporters-arraystring)
- [`notify`](configuration.html#notify-boolean)
Expand Down
13 changes: 13 additions & 0 deletions e2e/__tests__/__snapshots__/coverageProjects.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`outputs coverage report 1`] = `
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
add | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
mul | 100 | 100 | 100 | 100 |
other_file.js | 100 | 100 | 100 | 100 |
----------------|---------|----------|---------|---------|-------------------
`;
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"cache": false,
"cacheDirectory": "/tmp/jest",
"clearMocks": false,
"collectCoverageFrom": [],
"coveragePathIgnorePatterns": [
"/node_modules/"
],
Expand Down Expand Up @@ -82,7 +83,6 @@ exports[`--showConfig outputs config info and exits 1`] = `
"bail": 0,
"changedFilesWithAncestor": false,
"collectCoverage": false,
"collectCoverageFrom": [],
"coverageDirectory": "<<REPLACED_ROOT_DIR>>/coverage",
"coverageProvider": "babel",
"coverageReporters": [
Expand Down
35 changes: 35 additions & 0 deletions e2e/__tests__/coverageProjects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as fs from 'fs';
import * as path from 'path';
import {wrap} from 'jest-snapshot-serializer-raw';
import {run} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../coverage-projects');

beforeAll(() => {
run('yarn', DIR);
});

test('outputs coverage report', () => {
const {stdout, exitCode} = runJest(DIR, ['--no-cache', '--coverage'], {
stripAnsi: true,
});
const coverageDir = path.join(DIR, 'coverage');

// - the `index.js` file in package `mul` is ignored and should not be in the
// coverage report.
expect(wrap(stdout)).toMatchSnapshot();

// `index.js` should only appear once (in package `add`)
expect(stdout.match(/index\.js/g)).toHaveLength(1);

expect(() => fs.accessSync(coverageDir, fs.F_OK)).not.toThrow();
expect(exitCode).toBe(0);
});
2 changes: 2 additions & 0 deletions e2e/coverage-projects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
yarn-error.log
12 changes: 12 additions & 0 deletions e2e/coverage-projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"private": true,
"workspaces": {
"packages": [
"packages/*"
]
},
"jest": {
"collectCoverage": true,
"projects": ["packages/*"]
}
}
8 changes: 8 additions & 0 deletions e2e/coverage-projects/packages/add/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = (x, y) => x + y;
14 changes: 14 additions & 0 deletions e2e/coverage-projects/packages/add/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const add = require('.');

describe('add', () => {
it('should add correctly', () => {
expect(add(2, 3)).toBe(5);
});
});
11 changes: 11 additions & 0 deletions e2e/coverage-projects/packages/add/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "add",
"private": true,
"jest": {
"collectCoverage": true,
"collectCoverageFrom": [
"!**.json",
"!<rootDir>/coverage/**"
]
}
}
8 changes: 8 additions & 0 deletions e2e/coverage-projects/packages/mul/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = (x, y) => x * y;
14 changes: 14 additions & 0 deletions e2e/coverage-projects/packages/mul/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const mul = require('.');

describe('mul', () => {
it('should multiply correctly', () => {
expect(mul(2, 3)).toBe(6);
});
});
8 changes: 8 additions & 0 deletions e2e/coverage-projects/packages/mul/other_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = () => 'hello';
14 changes: 14 additions & 0 deletions e2e/coverage-projects/packages/mul/other_file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const hello = require('./other_file');

describe('other_file', () => {
it('should output hello', () => {
expect(hello()).toBe('hello');
});
});
12 changes: 12 additions & 0 deletions e2e/coverage-projects/packages/mul/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "mul",
"private": true,
"jest": {
"collectCoverage": true,
"collectCoverageFrom": [
"!**.json",
"!<rootDir>/coverage/**",
"!<rootDir>/index.js"
]
}
}
4 changes: 4 additions & 0 deletions e2e/coverage-projects/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = {
'/packages/jest-worker/src/__performance_tests__',
'/packages/pretty-format/perf/test.js',
'/e2e/__tests__/iterator-to-null-test.ts',
'/.history',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this directory?

],
transform: {
'^.+\\.[jt]sx?$': '<rootDir>/packages/babel-jest',
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ const groupOptions = (
changedFilesWithAncestor: options.changedFilesWithAncestor,
changedSince: options.changedSince,
collectCoverage: options.collectCoverage,
collectCoverageFrom: options.collectCoverageFrom,
collectCoverageOnlyFrom: options.collectCoverageOnlyFrom,
coverageDirectory: options.coverageDirectory,
coverageProvider: options.coverageProvider,
coverageReporters: options.coverageReporters,
coverageThreshold: options.coverageThreshold,
detectLeaks: options.detectLeaks,
detectOpenHandles: options.detectOpenHandles,
enabledTestsMap: options.enabledTestsMap,
Expand Down Expand Up @@ -167,7 +164,10 @@ const groupOptions = (
cache: options.cache,
cacheDirectory: options.cacheDirectory,
clearMocks: options.clearMocks,
collectCoverageFrom: options.collectCoverageFrom,
collectCoverageOnlyFrom: options.collectCoverageOnlyFrom,
coveragePathIgnorePatterns: options.coveragePathIgnorePatterns,
coverageThreshold: options.coverageThreshold,
cwd: options.cwd,
dependencyExtractor: options.dependencyExtractor,
detectLeaks: options.detectLeaks,
Expand Down
3 changes: 0 additions & 3 deletions packages/jest-core/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,8 @@ describe('Watch mode flows', () => {
${'✖︎'} | ${'changedFilesWithAncestor'}
${'✔︎'} | ${'changedSince'}
${'✔︎'} | ${'collectCoverage'}
${'✔︎'} | ${'collectCoverageFrom'}
${'✔︎'} | ${'collectCoverageOnlyFrom'}
${'✔︎'} | ${'coverageDirectory'}
${'✔︎'} | ${'coverageReporters'}
${'✖︎'} | ${'coverageThreshold'}
${'✖︎'} | ${'detectLeaks'}
${'✖︎'} | ${'detectOpenHandles'}
${'✖︎'} | ${'enabledTestsMap'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ exports[`prints the config object 1`] = `
"cache": false,
"cacheDirectory": "/test_cache_dir/",
"clearMocks": false,
"collectCoverageFrom": [],
"collectCoverageOnlyFrom": null,
"coveragePathIgnorePatterns": [],
"coverageThreshold": {
"global": {}
},
"cwd": "/test_root_dir/",
"detectLeaks": false,
"detectOpenHandles": false,
Expand Down Expand Up @@ -68,14 +73,9 @@ exports[`prints the config object 1`] = `
"changedFilesWithAncestor": false,
"changedSince": "",
"collectCoverage": false,
"collectCoverageFrom": [],
"collectCoverageOnlyFrom": null,
"coverageDirectory": "coverage",
"coverageProvider": "babel",
"coverageReporters": [],
"coverageThreshold": {
"global": {}
},
"detectLeaks": false,
"detectOpenHandles": false,
"enabledTestsMap": null,
Expand Down
8 changes: 0 additions & 8 deletions packages/jest-core/src/lib/update_global_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ export default (
newConfig.collectCoverage = options.collectCoverage || false;
}

if (options.collectCoverageFrom !== undefined) {
newConfig.collectCoverageFrom = options.collectCoverageFrom;
}

if (options.collectCoverageOnlyFrom !== undefined) {
newConfig.collectCoverageOnlyFrom = options.collectCoverageOnlyFrom;
}

if (options.coverageDirectory !== undefined) {
newConfig.coverageDirectory = options.coverageDirectory;
}
Expand Down
4 changes: 0 additions & 4 deletions packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ export default function watch(
bail,
changedSince,
collectCoverage,
collectCoverageFrom,
collectCoverageOnlyFrom,
coverageDirectory,
coverageReporters,
mode,
Expand All @@ -122,8 +120,6 @@ export default function watch(
bail,
changedSince,
collectCoverage,
collectCoverageFrom,
collectCoverageOnlyFrom,
coverageDirectory,
coverageReporters,
mode,
Expand Down
Loading