From ec382f527e75e0d6932e3e3954187d8a4d6c6240 Mon Sep 17 00:00:00 2001 From: Eddie Monge Date: Tue, 2 Oct 2018 14:40:53 -0700 Subject: [PATCH] fix: coverage summary reporting (#7058) --- CHANGELOG.md | 1 + .../coverage_report.test.js.snap | 53 +++++++++++++++++++ e2e/__tests__/coverage_report.test.js | 47 ++++++++++++++++ .../src/reporters/coverage_reporter.js | 11 ++-- 4 files changed, 105 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1b6ebd926e3..469d7114960e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixes +- `[jest-cli]` Fix coverage summary reporting ([#7058](https://github.com/facebook/jest/pull/7058)) - `[jest-each]` Add each array validation check ([#7033](https://github.com/facebook/jest/pull/7033)) - `[jest-haste-map]` [**BREAKING**] Replace internal data structures to improve performance ([#6960](https://github.com/facebook/jest/pull/6960)) - `[jest-haste-map]` Use relative paths to allow remote caching ([#7020](https://github.com/facebook/jest/pull/7020)) diff --git a/e2e/__tests__/__snapshots__/coverage_report.test.js.snap b/e2e/__tests__/__snapshots__/coverage_report.test.js.snap index ca5eb9a00684..0e2fae8499c8 100644 --- a/e2e/__tests__/__snapshots__/coverage_report.test.js.snap +++ b/e2e/__tests__/__snapshots__/coverage_report.test.js.snap @@ -40,6 +40,8 @@ All files | 85.71 | 100 | 50 | 100 | | ----------|----------|----------|----------|----------|-------------------|" `; +exports[`does not output coverage report when html is requested 1`] = `""`; + exports[`json reporter printing with --coverage 1`] = ` "Test Suites: 1 failed, 1 total Tests: 1 failed, 2 passed, 3 total @@ -65,3 +67,54 @@ All files | 56.52 | 0 | 50 | 55.56 Identical.js | 100 | 100 | 100 | 100 | | -------------------------------------|----------|----------|----------|----------|-------------------|" `; + +exports[`outputs coverage report when text and text-summary is requested 1`] = ` +" +=============================== Coverage summary =============================== +Statements : 56.52% ( 13/23 ) +Branches : 0% ( 0/4 ) +Functions : 50% ( 3/6 ) +Lines : 55.56% ( 10/18 ) +================================================================================ +-------------------------------------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-------------------------------------|----------|----------|----------|----------|-------------------| +All files | 56.52 | 0 | 50 | 55.56 | | + coverage-report | 41.18 | 0 | 25 | 42.86 | | + OtherFile.js | 100 | 100 | 100 | 100 | | + Sum.js | 85.71 | 100 | 50 | 100 | | + SumDependency.js | 0 | 0 | 0 | 0 | 8,10,12 | + notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8,15,16,17,19 | + coverage-report/cached-duplicates/a | 100 | 100 | 100 | 100 | | + Identical.js | 100 | 100 | 100 | 100 | | + coverage-report/cached-duplicates/b | 100 | 100 | 100 | 100 | | + Identical.js | 100 | 100 | 100 | 100 | | +-------------------------------------|----------|----------|----------|----------|-------------------|" +`; + +exports[`outputs coverage report when text is requested 1`] = ` +"-------------------------------------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-------------------------------------|----------|----------|----------|----------|-------------------| +All files | 56.52 | 0 | 50 | 55.56 | | + coverage-report | 41.18 | 0 | 25 | 42.86 | | + OtherFile.js | 100 | 100 | 100 | 100 | | + Sum.js | 85.71 | 100 | 50 | 100 | | + SumDependency.js | 0 | 0 | 0 | 0 | 8,10,12 | + notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8,15,16,17,19 | + coverage-report/cached-duplicates/a | 100 | 100 | 100 | 100 | | + Identical.js | 100 | 100 | 100 | 100 | | + coverage-report/cached-duplicates/b | 100 | 100 | 100 | 100 | | + Identical.js | 100 | 100 | 100 | 100 | | +-------------------------------------|----------|----------|----------|----------|-------------------|" +`; + +exports[`outputs coverage report when text-summary is requested 1`] = ` +" +=============================== Coverage summary =============================== +Statements : 56.52% ( 13/23 ) +Branches : 0% ( 0/4 ) +Functions : 50% ( 3/6 ) +Lines : 55.56% ( 10/18 ) +================================================================================" +`; diff --git a/e2e/__tests__/coverage_report.test.js b/e2e/__tests__/coverage_report.test.js index caa1644a5028..2c29d268e945 100644 --- a/e2e/__tests__/coverage_report.test.js +++ b/e2e/__tests__/coverage_report.test.js @@ -89,6 +89,53 @@ test('outputs coverage report as json', () => { } }); +test('outputs coverage report when text is requested', () => { + const {stdout, status} = runJest(DIR, [ + '--no-cache', + '--coverage', + '--coverageReporters=text', + '--coverageReporters=html', + ]); + expect(status).toBe(0); + expect(stdout).toMatch(/Stmts | . Branch/); + expect(stdout).toMatchSnapshot(); +}); + +test('outputs coverage report when text-summary is requested', () => { + const {stdout, status} = runJest(DIR, [ + '--no-cache', + '--coverage', + '--coverageReporters=text-summary', + ]); + expect(status).toBe(0); + expect(stdout).toMatch(/Coverage summary/); + expect(stdout).toMatchSnapshot(); +}); + +test('outputs coverage report when text and text-summary is requested', () => { + const {stdout, status} = runJest(DIR, [ + '--no-cache', + '--coverage', + '--coverageReporters=text-summary', + '--coverageReporters=text', + ]); + expect(status).toBe(0); + expect(stdout).toMatch(/Stmts | . Branch/); + expect(stdout).toMatch(/Coverage summary/); + expect(stdout).toMatchSnapshot(); +}); + +test('does not output coverage report when html is requested', () => { + const {stdout, status} = runJest(DIR, [ + '--no-cache', + '--coverage', + '--coverageReporters=html', + ]); + expect(status).toBe(0); + expect(stdout).toMatch(/^$/); + expect(stdout).toMatchSnapshot(); +}); + test('collects coverage from duplicate files avoiding shared cache', () => { const args = [ '--coverage', diff --git a/packages/jest-cli/src/reporters/coverage_reporter.js b/packages/jest-cli/src/reporters/coverage_reporter.js index f515d2294edf..7d3fffe2ae5c 100644 --- a/packages/jest-cli/src/reporters/coverage_reporter.js +++ b/packages/jest-cli/src/reporters/coverage_reporter.js @@ -93,13 +93,10 @@ export default class CoverageReporter extends BaseReporter { reporter.dir = this._globalConfig.coverageDirectory; } - let coverageReporters = this._globalConfig.coverageReporters || []; - if ( - !this._globalConfig.useStderr && - coverageReporters.length && - coverageReporters.indexOf('text') === -1 - ) { - coverageReporters = coverageReporters.concat(['text-summary']); + const coverageReporters = this._globalConfig.coverageReporters || []; + + if (!this._globalConfig.useStderr && coverageReporters.length < 1) { + coverageReporters.push('text-summary'); } reporter.addAll(coverageReporters);