Skip to content

Commit

Permalink
Add short coverage summary. (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer authored Aug 30, 2016
1 parent d7316a8 commit 9088f65
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ All files | 100 | 100 | 100 | 100 | |
"
`;

exports[`test json reporter printing with --coverage 1`] = `
"Coverage Summary (add \"text\" to the \"coverageReporter\" setting to receive a full report)
› Statements: 100%
› Branches: 100%
› Lines: 100%
› Functions: 100%
"
`;

exports[`test outputs coverage report 1`] = `
"-------------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
Expand Down
17 changes: 15 additions & 2 deletions integration_tests/__tests__/coverage_report-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const DIR = path.resolve(__dirname, '../coverage_report');

beforeEach(() => linkJestPackage('babel-jest', DIR));

it('outputs coverage report', () => {
test('outputs coverage report', () => {
const {stdout, status} = runJest(DIR, ['--no-cache', '--coverage']);
const coverageDir = path.resolve(__dirname, '../coverage_report/coverage');

Expand All @@ -33,7 +33,7 @@ it('outputs coverage report', () => {
expect(status).toBe(0);
});

it('collects coverage only from specified files', () => {
test('collects coverage only from specified files', () => {
const {stdout} = runJest(DIR, [
'--no-cache',
'--coverage',
Expand All @@ -44,3 +44,16 @@ it('collects coverage only from specified files', () => {
// Coverage report should only have `setup.js` coverage info
expect(stdout).toMatchSnapshot();
});

test('json reporter printing with --coverage', () => {
const result = runJest('json_reporter', ['--coverage']);
let stderr =
('Coverage' + result.stderr.toString().split('Coverage')[1]).split('\n');
stderr.pop();
stderr.pop();
stderr = stderr.join('\n');

expect(result.status).toBe(1);

expect(stderr).toMatchSnapshot();
});
2 changes: 1 addition & 1 deletion integration_tests/console/__tests__/console2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('works just fine', () => {
return new Promise(resolve => {
// Make the second test finish last to get consistent console
// output
setTimeout(resolve, 3000);
setTimeout(resolve, 2000);
}).then(() => {
console.error('This is an error from another test file.');
});
Expand Down
2 changes: 0 additions & 2 deletions integration_tests/json_reporter/__tests__/sum-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
'use strict';

jest.unmock('../sum');

const sum = require('../sum');

describe('sum', () => {
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/json_reporter/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"coverageReporters": ["json"]
}
}
24 changes: 21 additions & 3 deletions packages/jest-cli/src/reporters/CoverageReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const fs = require('fs');
const generateEmptyCoverage = require('../generateEmptyCoverage');
const istanbulCoverage = require('istanbul-lib-coverage');

const COVERAGE_SUMMARY = chalk.bold;
const FAIL_COLOR = chalk.bold.red;
const RUNNING_TEST_COLOR = chalk.bold.gray;

Expand Down Expand Up @@ -62,6 +63,23 @@ class CoverageReporter extends BaseReporter {
}
reporter.addAll(config.coverageReporters || []);
reporter.write(this._coverageMap);

if (
config.coverageReporters &&
config.coverageReporters.length &&
!config.coverageReporters.includes('text')
) {
const results = this._coverageMap.getCoverageSummary().toJSON();
const format = percent => percent + (percent === 'Unknown' ? '' : '%');
this.log(
'\n' + COVERAGE_SUMMARY('Coverage Summary') + ' (add "text" to the ' +
'"coverageReporter" setting to receive a full report)\n' +
' \u203A Statements: ' + format(results.statements.pct) + '\n' +
' \u203A Branches: ' + format(results.branches.pct) + '\n' +
' \u203A Lines: ' + format(results.lines.pct) + '\n' +
' \u203A Functions: ' + format(results.functions.pct) + '\n',
);
}
} catch (e) {
console.error(chalk.red(`
Failed to write coverage reports:
Expand All @@ -76,7 +94,7 @@ class CoverageReporter extends BaseReporter {
_addUntestedFiles(config: Config, runnerContext: RunnerContext) {
if (config.collectCoverageFrom && config.collectCoverageFrom.length) {
process.stderr.write(RUNNING_TEST_COLOR(
'Running coverage for untested files...',
'Running coverage on untested files...',
));
const files = runnerContext.hasteFS.matchFilesWithGlob(
config.collectCoverageFrom,
Expand Down Expand Up @@ -106,7 +124,7 @@ class CoverageReporter extends BaseReporter {

_checkThreshold(config: Config) {
if (config.coverageThreshold) {
const globalResults = this._coverageMap.getCoverageSummary().toJSON();
const results = this._coverageMap.getCoverageSummary().toJSON();

function check(name, thresholds, actuals) {
return [
Expand Down Expand Up @@ -140,7 +158,7 @@ class CoverageReporter extends BaseReporter {
const errors = check(
'global',
config.coverageThreshold.global,
globalResults,
results,
);

if (errors.length > 0) {
Expand Down

0 comments on commit 9088f65

Please sign in to comment.