From 9088f6517813f6c089cf52e980d6579511dcde88 Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Tue, 30 Aug 2016 21:59:12 +0100 Subject: [PATCH] Add short coverage summary. (#1518) --- .../coverage_report-test.js.snap | 9 +++++++ .../__tests__/coverage_report-test.js | 17 +++++++++++-- .../console/__tests__/console2-test.js | 2 +- .../json_reporter/__tests__/sum-test.js | 2 -- integration_tests/json_reporter/package.json | 3 ++- .../src/reporters/CoverageReporter.js | 24 ++++++++++++++++--- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/integration_tests/__tests__/__snapshots__/coverage_report-test.js.snap b/integration_tests/__tests__/__snapshots__/coverage_report-test.js.snap index 3e1bae1c55dd..17805ccae0ea 100644 --- a/integration_tests/__tests__/__snapshots__/coverage_report-test.js.snap +++ b/integration_tests/__tests__/__snapshots__/coverage_report-test.js.snap @@ -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 | diff --git a/integration_tests/__tests__/coverage_report-test.js b/integration_tests/__tests__/coverage_report-test.js index 5df4c1c18190..ea414f67dfee 100644 --- a/integration_tests/__tests__/coverage_report-test.js +++ b/integration_tests/__tests__/coverage_report-test.js @@ -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'); @@ -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', @@ -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(); +}); diff --git a/integration_tests/console/__tests__/console2-test.js b/integration_tests/console/__tests__/console2-test.js index 051ea06ff930..94406e6a151e 100644 --- a/integration_tests/console/__tests__/console2-test.js +++ b/integration_tests/console/__tests__/console2-test.js @@ -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.'); }); diff --git a/integration_tests/json_reporter/__tests__/sum-test.js b/integration_tests/json_reporter/__tests__/sum-test.js index a1ad51bbb19e..bdd9fd02556c 100644 --- a/integration_tests/json_reporter/__tests__/sum-test.js +++ b/integration_tests/json_reporter/__tests__/sum-test.js @@ -7,8 +7,6 @@ */ 'use strict'; -jest.unmock('../sum'); - const sum = require('../sum'); describe('sum', () => { diff --git a/integration_tests/json_reporter/package.json b/integration_tests/json_reporter/package.json index 148788b25446..d59562604cad 100644 --- a/integration_tests/json_reporter/package.json +++ b/integration_tests/json_reporter/package.json @@ -1,5 +1,6 @@ { "jest": { - "testEnvironment": "node" + "testEnvironment": "node", + "coverageReporters": ["json"] } } diff --git a/packages/jest-cli/src/reporters/CoverageReporter.js b/packages/jest-cli/src/reporters/CoverageReporter.js index cd8afe260b06..02ba0bfee23e 100644 --- a/packages/jest-cli/src/reporters/CoverageReporter.js +++ b/packages/jest-cli/src/reporters/CoverageReporter.js @@ -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; @@ -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: @@ -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, @@ -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 [ @@ -140,7 +158,7 @@ class CoverageReporter extends BaseReporter { const errors = check( 'global', config.coverageThreshold.global, - globalResults, + results, ); if (errors.length > 0) {