From f238bec9aeed294dac783e2963fc5988b6fe9204 Mon Sep 17 00:00:00 2001 From: rhys williams Date: Wed, 30 May 2018 10:21:29 +1200 Subject: [PATCH 1/9] Improve the message when running coverage while there are no tests fixes #6141 Update the coverage reporting so that it still conforms to the documentation but doesn't throw an error when there are no files matching "global" threshold group (maybe because they are already matched with a path or glob). Also make sure that a file is matched against all matching path and glob threshold groups instead of just one. --- CHANGELOG.md | 1 + .../__tests__/coverage_reporter.test.js | 180 ++++++++++++++++-- .../src/reporters/coverage_reporter.js | 90 +++++---- 3 files changed, 215 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ed1c2d8212..8b6af8ab45b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313)) * `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310)) +* `[jest-cli]` Improve the message when running coverage while there are no files matching global threshold ([#6334](https://github.com/facebook/jest/pull/6334)) ### Chore & Maintenance diff --git a/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js b/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js index ca5bcb6be0f8..9cb7528aec3c 100644 --- a/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js +++ b/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js @@ -16,6 +16,7 @@ let libSourceMaps; let CoverageReporter; let istanbulApi; +import {CoverageSummary} from 'istanbul-lib-coverage/lib/file'; import path from 'path'; import mock from 'mock-fs'; @@ -32,6 +33,9 @@ beforeEach(() => { const fileTree = {}; fileTree[process.cwd() + '/path-test-files'] = { + '000pc_coverage_file.js': '', + '050pc_coverage_file.js': '', + '100pc_coverage_file.js': '', 'full_path_file.js': '', 'glob-path': { 'file1.js': '', @@ -68,29 +72,46 @@ describe('onRunComplete', () => { }; libCoverage.createCoverageMap = jest.fn(() => { - const files = [ - './path-test-files/covered_file_without_threshold.js', - './path-test-files/full_path_file.js', - './path-test-files/relative_path_file.js', - './path-test-files/glob-path/file1.js', - './path-test-files/glob-path/file2.js', - ].map(p => path.resolve(p)); + const covSummary = { + branches: {covered: 0, pct: 0, skipped: 0, total: 0}, + functions: {covered: 0, pct: 0, skipped: 0, total: 0}, + lines: {covered: 0, pct: 0, skipped: 0, total: 0}, + statements: {covered: 5, pct: 50, skipped: 0, total: 10}, + }; + const fileCoverage = [ + ['./path-test-files/covered_file_without_threshold.js'], + ['./path-test-files/full_path_file.js'], + ['./path-test-files/relative_path_file.js'], + ['./path-test-files/glob-path/file1.js'], + ['./path-test-files/glob-path/file2.js'], + [ + './path-test-files/000pc_coverage_file.js', + {statements: {covered: 0, pct: 0, total: 10}}, + ], + [ + './path-test-files/050pc_coverage_file.js', + {statements: {covered: 5, pct: 50, total: 10}}, + ], + [ + './path-test-files/100pc_coverage_file.js', + {statements: {covered: 10, pct: 100, total: 10}}, + ], + ].reduce((c, f) => { + const file = path.resolve(f[0]); + const override = f[1]; + c[file] = new CoverageSummary({ + ...covSummary, + ...override, + }); + return c; + }, {}); return { fileCoverageFor(path) { - if (files.indexOf(path) !== -1) { - const covSummary = { - branches: {covered: 0, pct: 0, skipped: 0, total: 0}, - functions: {covered: 0, pct: 0, skipped: 0, total: 0}, - lines: {covered: 0, pct: 0, skipped: 0, total: 0}, - merge(other) { - return covSummary; - }, - statements: {covered: 0, pct: 50, skipped: 0, total: 0}, - }; + if (fileCoverage[path]) { return { toSummary() { - return covSummary; + return fileCoverage[path]; }, }; } else { @@ -98,7 +119,7 @@ describe('onRunComplete', () => { } }, files() { - return files; + return Object.keys(fileCoverage); }, }; }); @@ -281,4 +302,125 @@ describe('onRunComplete', () => { expect(testReporter.getLastError().message.split('\n')).toHaveLength(1); }); }); + + test(`getLastError() returns 'undefined' when global threshold group + is empty because PATH and GLOB threshold groups have matched all the + files in the coverage data.`, () => { + const testReporter = new CoverageReporter( + { + collectCoverage: true, + coverageThreshold: { + './path-test-files/': { + statements: 50, + }, + global: { + statements: 100, + }, + }, + }, + { + maxWorkers: 2, + }, + ); + testReporter.log = jest.fn(); + return testReporter + .onRunComplete(new Set(), {}, mockAggResults) + .then(() => { + expect(testReporter.getLastError()).toBeUndefined(); + }); + }); + + test(`getLastError() returns 'undefined' when file and directory path + threshold groups overlap`, () => { + const covThreshold = {}; + [ + './path-test-files/', + './path-test-files/covered_file_without_threshold.js', + './path-test-files/full_path_file.js', + './path-test-files/relative_path_file.js', + './path-test-files/glob-path/file1.js', + './path-test-files/glob-path/file2.js', + './path-test-files/*.js', + ].forEach(path => { + covThreshold[path] = { + statements: 0, + }; + }); + + const testReporter = new CoverageReporter( + { + collectCoverage: true, + coverageThreshold: covThreshold, + }, + { + maxWorkers: 2, + }, + ); + testReporter.log = jest.fn(); + return testReporter + .onRunComplete(new Set(), {}, mockAggResults) + .then(() => { + expect(testReporter.getLastError()).toBeUndefined(); + }); + }); + + test(`that if globs or paths are specified alongside global, coverage + data for matching paths will be subtracted from overall coverage + and thresholds will be applied independently`, () => { + const testReporter = new CoverageReporter( + { + collectCoverage: true, + coverageThreshold: { + './path-test-files/100pc_coverage_file.js': { + statements: 100, + }, + global: { + statements: 50, + }, + }, + }, + { + maxWorkers: 2, + }, + ); + testReporter.log = jest.fn(); + // 100% coverage file is removed from overall coverage so + // coverage drops to < 50% + return testReporter + .onRunComplete(new Set(), {}, mockAggResults) + .then(() => { + expect(testReporter.getLastError().message.split('\n')).toHaveLength(1); + }); + }); + + test(`that files are matched by all matching threshold groups`, () => { + const testReporter = new CoverageReporter( + { + collectCoverage: true, + coverageThreshold: { + './path-test-files/': { + statements: 50, + }, + './path-test-files/050pc_coverage_file.js': { + statements: 50, + }, + './path-test-files/100pc_coverage_*.js': { + statements: 100, + }, + './path-test-files/100pc_coverage_file.js': { + statements: 100, + }, + }, + }, + { + maxWorkers: 2, + }, + ); + testReporter.log = jest.fn(); + return testReporter + .onRunComplete(new Set(), {}, mockAggResults) + .then(() => { + expect(testReporter.getLastError()).toBeUndefined(); + }); + }); }); diff --git a/packages/jest-cli/src/reporters/coverage_reporter.js b/packages/jest-cli/src/reporters/coverage_reporter.js index 4fa08a4f0a9d..f515d2294edf 100644 --- a/packages/jest-cli/src/reporters/coverage_reporter.js +++ b/packages/jest-cli/src/reporters/coverage_reporter.js @@ -248,51 +248,61 @@ export default class CoverageReporter extends BaseReporter { }; const coveredFiles = map.files(); const thresholdGroups = Object.keys(globalConfig.coverageThreshold); - const numThresholdGroups = thresholdGroups.length; const groupTypeByThresholdGroup = {}; const filesByGlob = {}; - const coveredFilesSortedIntoThresholdGroup = coveredFiles.map(file => { - for (let i = 0; i < numThresholdGroups; i++) { - const thresholdGroup = thresholdGroups[i]; - const absoluteThresholdGroup = path.resolve(thresholdGroup); + const coveredFilesSortedIntoThresholdGroup = coveredFiles.reduce( + (files, file) => { + const pathOrGlobMatches = thresholdGroups.reduce( + (agg, thresholdGroup) => { + const absoluteThresholdGroup = path.resolve(thresholdGroup); - // The threshold group might be a path: + // The threshold group might be a path: - if (file.indexOf(absoluteThresholdGroup) === 0) { - groupTypeByThresholdGroup[thresholdGroup] = - THRESHOLD_GROUP_TYPES.PATH; - return [file, thresholdGroup]; - } + if (file.indexOf(absoluteThresholdGroup) === 0) { + groupTypeByThresholdGroup[thresholdGroup] = + THRESHOLD_GROUP_TYPES.PATH; + return agg.concat([[file, thresholdGroup]]); + } - // If the threshold group is not a path it might be a glob: + // If the threshold group is not a path it might be a glob: - // Note: glob.sync is slow. By memoizing the files matching each glob - // (rather than recalculating it for each covered file) we save a tonne - // of execution time. - if (filesByGlob[absoluteThresholdGroup] === undefined) { - filesByGlob[absoluteThresholdGroup] = glob - .sync(absoluteThresholdGroup) - .map(filePath => path.resolve(filePath)); - } + // Note: glob.sync is slow. By memoizing the files matching each glob + // (rather than recalculating it for each covered file) we save a tonne + // of execution time. + if (filesByGlob[absoluteThresholdGroup] === undefined) { + filesByGlob[absoluteThresholdGroup] = glob + .sync(absoluteThresholdGroup) + .map(filePath => path.resolve(filePath)); + } + + if (filesByGlob[absoluteThresholdGroup].indexOf(file) > -1) { + groupTypeByThresholdGroup[thresholdGroup] = + THRESHOLD_GROUP_TYPES.GLOB; + return agg.concat([[file, thresholdGroup]]); + } + + return agg; + }, + [], + ); - if (filesByGlob[absoluteThresholdGroup].indexOf(file) > -1) { - groupTypeByThresholdGroup[thresholdGroup] = - THRESHOLD_GROUP_TYPES.GLOB; - return [file, thresholdGroup]; + if (pathOrGlobMatches.length > 0) { + return files.concat(pathOrGlobMatches); } - } - // Neither a glob or a path? Toss it in global if there's a global threshold: - if (thresholdGroups.indexOf(THRESHOLD_GROUP_TYPES.GLOBAL) > -1) { - groupTypeByThresholdGroup[THRESHOLD_GROUP_TYPES.GLOBAL] = - THRESHOLD_GROUP_TYPES.GLOBAL; - return [file, THRESHOLD_GROUP_TYPES.GLOBAL]; - } + // Neither a glob or a path? Toss it in global if there's a global threshold: + if (thresholdGroups.indexOf(THRESHOLD_GROUP_TYPES.GLOBAL) > -1) { + groupTypeByThresholdGroup[THRESHOLD_GROUP_TYPES.GLOBAL] = + THRESHOLD_GROUP_TYPES.GLOBAL; + return files.concat([[file, THRESHOLD_GROUP_TYPES.GLOBAL]]); + } - // A covered file that doesn't have a threshold: - return [file, undefined]; - }); + // A covered file that doesn't have a threshold: + return files.concat([[file, undefined]]); + }, + [], + ); const getFilesInThresholdGroup = thresholdGroup => coveredFilesSortedIntoThresholdGroup @@ -364,9 +374,15 @@ export default class CoverageReporter extends BaseReporter { ); break; default: - errors = errors.concat( - `Jest: Coverage data for ${thresholdGroup} was not found.`, - ); + // If the file specified by path is not found, error is returned. + if (thresholdGroup !== THRESHOLD_GROUP_TYPES.GLOBAL) { + errors = errors.concat( + `Jest: Coverage data for ${thresholdGroup} was not found.`, + ); + } + // Sometimes all files in the coverage data are matched by + // PATH and GLOB threshold groups in which case, don't error when + // the global threshold group doesn't match any files. } }); From 94f2331c6e8e8eef1e10376ed3f81935ea106fc1 Mon Sep 17 00:00:00 2001 From: rhys williams Date: Tue, 29 May 2018 20:29:43 +1200 Subject: [PATCH 2/9] use Object.assign --- .../src/reporters/__tests__/coverage_reporter.test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js b/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js index 9cb7528aec3c..27e30441fba6 100644 --- a/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js +++ b/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js @@ -99,10 +99,8 @@ describe('onRunComplete', () => { ].reduce((c, f) => { const file = path.resolve(f[0]); const override = f[1]; - c[file] = new CoverageSummary({ - ...covSummary, - ...override, - }); + const obj = Object.assign({}, covSummary, override); + c[file] = new CoverageSummary(obj); return c; }, {}); From 71b829e2d0fde7b500c7e9d8c870df769176da99 Mon Sep 17 00:00:00 2001 From: rhys williams Date: Wed, 30 May 2018 09:32:23 +1200 Subject: [PATCH 3/9] Only mock specific functions exported by istanbul-lib-coverage (instead of the whole module). So that I can use the original createCoverageSummary function to create test data. --- .../src/reporters/__tests__/coverage_reporter.test.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js b/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js index 27e30441fba6..9b56ad54e150 100644 --- a/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js +++ b/packages/jest-cli/src/reporters/__tests__/coverage_reporter.test.js @@ -6,17 +6,13 @@ */ 'use strict'; -jest - .mock('istanbul-lib-coverage') - .mock('istanbul-lib-source-maps') - .mock('istanbul-api'); +jest.mock('istanbul-lib-source-maps').mock('istanbul-api'); let libCoverage; let libSourceMaps; let CoverageReporter; let istanbulApi; -import {CoverageSummary} from 'istanbul-lib-coverage/lib/file'; import path from 'path'; import mock from 'mock-fs'; @@ -100,7 +96,7 @@ describe('onRunComplete', () => { const file = path.resolve(f[0]); const override = f[1]; const obj = Object.assign({}, covSummary, override); - c[file] = new CoverageSummary(obj); + c[file] = libCoverage.createCoverageSummary(obj); return c; }, {}); From 271b54234121cdc15a0e7e91522beadbf0de6ee4 Mon Sep 17 00:00:00 2001 From: rhys williams Date: Thu, 31 May 2018 10:56:38 +1200 Subject: [PATCH 4/9] Add e2e tests for coverage threshold. I added snapshots for stderr so that I could test the Jest errors raised for each failed threshold group. --- .../coverage_threshold.test.js.snap | 96 +++++++++- e2e/__tests__/coverage_threshold.test.js | 165 +++++++++++++++++- 2 files changed, 257 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap b/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap index a66d1328111a..594e9e1068af 100644 --- a/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap +++ b/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap @@ -1,6 +1,52 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`exits with 1 if coverage threshold is not met 1`] = ` +exports[`excludes tests matched by path threshold groups from global group: stderr 1`] = ` +"PASS __tests__/banana.test.js + ✓ banana (<>) + +Jest: \\"global\\" coverage threshold for lines (100%) not met: 0% +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +" +`; + +exports[`excludes tests matched by path threshold groups from global group: stdout 1`] = ` +"-----------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-----------|----------|----------|----------|----------|-------------------| +All files | 50 | 100 | 50 | 50 | | + apple.js | 0 | 100 | 0 | 0 | 2,3 | + banana.js | 100 | 100 | 100 | 100 | | +-----------|----------|----------|----------|----------|-------------------| +" +`; + +exports[`exits with 0 if global threshold group is not found in coverage data: stdout 1`] = ` +"-----------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-----------|----------|----------|----------|----------|-------------------| +All files | 100 | 100 | 100 | 100 | | + banana.js | 100 | 100 | 100 | 100 | | +-----------|----------|----------|----------|----------|-------------------|" +`; + +exports[`exits with 1 if coverage threshold is not met: stderr 1`] = ` +"PASS __tests__/a-banana.js + ✓ banana (<>) + +Jest: \\"global\\" coverage threshold for lines (90%) not met: 50% +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +" +`; + +exports[`exits with 1 if coverage threshold is not met: stdout 1`] = ` "----------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ----------------|----------|----------|----------|----------|-------------------| @@ -9,3 +55,51 @@ All files | 50 | 100 | 0 | 50 | ----------------|----------|----------|----------|----------|-------------------| " `; + +exports[`exits with 1 if path threshold group is not found in coverage data: stderr 1`] = ` +"PASS __tests__/banana.test.js + ✓ banana (<>) + +Jest: Coverage data for apple.js was not found. +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +" +`; + +exports[`exits with 1 if path threshold group is not found in coverage data: stdout 1`] = ` +"-----------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-----------|----------|----------|----------|----------|-------------------| +All files | 100 | 100 | 100 | 100 | | + banana.js | 100 | 100 | 100 | 100 | | +-----------|----------|----------|----------|----------|-------------------| +" +`; + +exports[`file is matched by all path and glob threshold groups: stderr 1`] = ` +"PASS __tests__/banana.test.js + ✓ banana (<>) + +Jest: \\"./\\" coverage threshold for lines (100%) not met: 50% +Jest: \\"/Users/rhyswilliams/facebook/jest/e2e/coverage-threshold/banana.js\\" coverage threshold for lines (100%) not met: 50% +Jest: \\"banana.js\\" coverage threshold for lines (100%) not met: 50% +Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +" +`; + +exports[`file is matched by all path and glob threshold groups: stdout 1`] = ` +"-----------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-----------|----------|----------|----------|----------|-------------------| +All files | 50 | 100 | 0 | 50 | | + banana.js | 50 | 100 | 0 | 50 | 3 | +-----------|----------|----------|----------|----------|-------------------| +" +`; diff --git a/e2e/__tests__/coverage_threshold.test.js b/e2e/__tests__/coverage_threshold.test.js index 3a53c5ef426a..797f3174956f 100644 --- a/e2e/__tests__/coverage_threshold.test.js +++ b/e2e/__tests__/coverage_threshold.test.js @@ -15,8 +15,18 @@ const runJest = require('../runJest'); const DIR = path.resolve(__dirname, '../coverage-threshold'); -beforeEach(() => cleanup(DIR)); -afterAll(() => cleanup(DIR)); +beforeEach(() => { + cleanup(DIR); + Date.now = jest.fn(() => 1482363367071); +}); +afterAll(() => { + cleanup(DIR); + Date.now.mockRestore(); +}); + +const replaceTime = str => { + return str.replace(/\d*\.?\d+m?s/g, '<>'); +}; test('exits with 1 if coverage threshold is not met', () => { const pkgJson = { @@ -44,7 +54,156 @@ test('exits with 1 if coverage threshold is not met', () => { 'package.json': JSON.stringify(pkgJson, null, 2), }); + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + expect(status).toBe(1); + expect(stdout).toMatchSnapshot('stdout'); + expect(replaceTime(stderr)).toMatchSnapshot('stderr'); +}); + +test('exits with 1 if path threshold group is not found in coverage data', () => { + const pkgJson = { + jest: { + collectCoverage: true, + collectCoverageFrom: ['**/*.js'], + coverageThreshold: { + 'apple.js': { + lines: 100, + }, + }, + }, + }; + + writeFiles(DIR, { + '__tests__/banana.test.js': ` + const banana = require('../banana.js'); + test('banana', () => expect(banana()).toBe(3)); + `, + 'banana.js': ` + module.exports = () => { + return 1 + 2; + }; + `, + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + + expect(status).toBe(1); + expect(stdout).toMatchSnapshot('stdout'); + expect(replaceTime(stderr)).toMatchSnapshot('stderr'); +}); + +test('exits with 0 if global threshold group is not found in coverage data', () => { + const pkgJson = { + jest: { + collectCoverage: true, + collectCoverageFrom: ['**/*.js'], + coverageThreshold: { + 'banana.js': { + lines: 100, + }, + global: { + lines: 100, + }, + }, + }, + }; + + writeFiles(DIR, { + '__tests__/banana.test.js': ` + const banana = require('../banana.js'); + test('banana', () => expect(banana()).toBe(3)); + `, + 'banana.js': ` + module.exports = () => { + return 1 + 2; + }; + `, + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + const {stdout, status} = runJest(DIR, ['--coverage', '--ci=false']); + + expect(status).toBe(0); + expect(stdout).toMatchSnapshot('stdout'); +}); + +test('excludes tests matched by path threshold groups from global group', () => { + const pkgJson = { + jest: { + collectCoverage: true, + collectCoverageFrom: ['**/*.js'], + coverageThreshold: { + 'banana.js': { + lines: 100, + }, + global: { + lines: 100, + }, + }, + }, + }; + + writeFiles(DIR, { + '__tests__/banana.test.js': ` + const banana = require('../banana.js'); + test('banana', () => expect(banana()).toBe(3)); + `, + 'apple.js': ` + module.exports = () => { + return 1 + 2; + }; + `, + 'banana.js': ` + module.exports = () => { + return 1 + 2; + }; + `, + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + + expect(status).toBe(1); + expect(stdout).toMatchSnapshot('stdout'); + expect(replaceTime(stderr)).toMatchSnapshot('stderr'); +}); + +test('file is matched by all path and glob threshold groups', () => { + const pkgJson = { + jest: { + collectCoverage: true, + collectCoverageFrom: ['**/*.js'], + coverageThreshold: { + './': { + lines: 100, + }, + 'ban*.js': { + lines: 100, + }, + 'banana.js': { + lines: 100, + }, + }, + }, + }; + + writeFiles(DIR, { + '__tests__/banana.test.js': ` + const banana = require('../banana.js'); + test('banana', () => expect(3).toBe(3)); + `, + 'banana.js': ` + module.exports = () => { + return 1 + 2; + }; + `, + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + + const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + expect(status).toBe(1); - expect(stdout).toMatchSnapshot(); + expect(stdout).toMatchSnapshot('stdout'); + expect(replaceTime(stderr)).toMatchSnapshot('stderr'); }); From f4b160bc6a626cb841da1eecc70ae1e3b2eb11bf Mon Sep 17 00:00:00 2001 From: rhys williams Date: Thu, 31 May 2018 11:09:39 +1200 Subject: [PATCH 5/9] revert unnecessary test changes --- e2e/__tests__/coverage_threshold.test.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/e2e/__tests__/coverage_threshold.test.js b/e2e/__tests__/coverage_threshold.test.js index 797f3174956f..d8c5717c08dd 100644 --- a/e2e/__tests__/coverage_threshold.test.js +++ b/e2e/__tests__/coverage_threshold.test.js @@ -15,14 +15,8 @@ const runJest = require('../runJest'); const DIR = path.resolve(__dirname, '../coverage-threshold'); -beforeEach(() => { - cleanup(DIR); - Date.now = jest.fn(() => 1482363367071); -}); -afterAll(() => { - cleanup(DIR); - Date.now.mockRestore(); -}); +beforeEach(() => cleanup(DIR)); +afterAll(() => cleanup(DIR)); const replaceTime = str => { return str.replace(/\d*\.?\d+m?s/g, '<>'); From bf81a293798a7ce6e8f2103b2ab39597776d8cbf Mon Sep 17 00:00:00 2001 From: rhys williams Date: Thu, 31 May 2018 11:25:03 +1200 Subject: [PATCH 6/9] linting --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ec4b068a489..a87c14fb8eed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310)) - `[jest-cli]` Fix run beforeAll in excluded suites tests" mode. ([#6234](https://github.com/facebook/jest/pull/6234)) - `[jest-haste-map`] Compute SHA-1s for non-tracked files when using Node crawler ([#6264](https://github.com/facebook/jest/pull/6264)) -* `[jest-cli]` Improve the message when running coverage while there are no files matching global threshold ([#6334](https://github.com/facebook/jest/pull/6334)) +- `[jest-cli]` Improve the message when running coverage while there are no files matching global threshold ([#6334](https://github.com/facebook/jest/pull/6334)) ### Chore & Maintenance From b62e3dfa847ffe03e5e98ee970e4d94a8a70568c Mon Sep 17 00:00:00 2001 From: rhys williams Date: Thu, 31 May 2018 12:03:50 +1200 Subject: [PATCH 7/9] Replace process.cwd() in stderr snapshot test. --- e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap | 2 +- e2e/__tests__/coverage_threshold.test.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap b/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap index 594e9e1068af..aa3b15bd81d2 100644 --- a/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap +++ b/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap @@ -84,7 +84,7 @@ exports[`file is matched by all path and glob threshold groups: stderr 1`] = ` ✓ banana (<>) Jest: \\"./\\" coverage threshold for lines (100%) not met: 50% -Jest: \\"/Users/rhyswilliams/facebook/jest/e2e/coverage-threshold/banana.js\\" coverage threshold for lines (100%) not met: 50% +Jest: \\"<>/e2e/coverage-threshold/banana.js\\" coverage threshold for lines (100%) not met: 50% Jest: \\"banana.js\\" coverage threshold for lines (100%) not met: 50% Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total diff --git a/e2e/__tests__/coverage_threshold.test.js b/e2e/__tests__/coverage_threshold.test.js index d8c5717c08dd..45579636b476 100644 --- a/e2e/__tests__/coverage_threshold.test.js +++ b/e2e/__tests__/coverage_threshold.test.js @@ -199,5 +199,7 @@ test('file is matched by all path and glob threshold groups', () => { expect(status).toBe(1); expect(stdout).toMatchSnapshot('stdout'); - expect(replaceTime(stderr)).toMatchSnapshot('stderr'); + expect( + replaceTime(stderr).replace(process.cwd(), '<>'), + ).toMatchSnapshot('stderr'); }); From 68a822b1f135a6e3bb6af912e6359fe30913c2ed Mon Sep 17 00:00:00 2001 From: rhys williams Date: Thu, 31 May 2018 17:16:52 +1200 Subject: [PATCH 8/9] Make the tests more robust by using extractSummary method to compare stderr snapshots. Do a replace on the stderr which has a full path to a failed glob threshold file which won't match when the test runs on windows (appveyor) --- .../coverage_threshold.test.js.snap | 44 +++++++++++++------ e2e/__tests__/coverage_threshold.test.js | 36 +++++++++------ 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap b/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap index aa3b15bd81d2..eb85e0a51a21 100644 --- a/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap +++ b/e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap @@ -1,11 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`excludes tests matched by path threshold groups from global group: stderr 1`] = ` +exports[`excludes tests matched by path threshold groups from global group 1`] = ` "PASS __tests__/banana.test.js - ✓ banana (<>) + ✓ banana Jest: \\"global\\" coverage threshold for lines (100%) not met: 0% -Test Suites: 1 passed, 1 total +" +`; + +exports[`excludes tests matched by path threshold groups from global group 2`] = ` +"Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: <> @@ -33,12 +37,16 @@ All files | 100 | 100 | 100 | 100 | | -----------|----------|----------|----------|----------|-------------------|" `; -exports[`exits with 1 if coverage threshold is not met: stderr 1`] = ` +exports[`exits with 1 if coverage threshold is not met 1`] = ` "PASS __tests__/a-banana.js - ✓ banana (<>) + ✓ banana Jest: \\"global\\" coverage threshold for lines (90%) not met: 50% -Test Suites: 1 passed, 1 total +" +`; + +exports[`exits with 1 if coverage threshold is not met 2`] = ` +"Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: <> @@ -56,12 +64,16 @@ All files | 50 | 100 | 0 | 50 | " `; -exports[`exits with 1 if path threshold group is not found in coverage data: stderr 1`] = ` +exports[`exits with 1 if path threshold group is not found in coverage data 1`] = ` "PASS __tests__/banana.test.js - ✓ banana (<>) + ✓ banana Jest: Coverage data for apple.js was not found. -Test Suites: 1 passed, 1 total +" +`; + +exports[`exits with 1 if path threshold group is not found in coverage data 2`] = ` +"Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: <> @@ -79,14 +91,18 @@ All files | 100 | 100 | 100 | 100 | | " `; -exports[`file is matched by all path and glob threshold groups: stderr 1`] = ` +exports[`file is matched by all path and glob threshold groups 1`] = ` "PASS __tests__/banana.test.js - ✓ banana (<>) + ✓ banana Jest: \\"./\\" coverage threshold for lines (100%) not met: 50% -Jest: \\"<>/e2e/coverage-threshold/banana.js\\" coverage threshold for lines (100%) not met: 50% -Jest: \\"banana.js\\" coverage threshold for lines (100%) not met: 50% -Test Suites: 1 passed, 1 total +Jest: \\"<>\\" coverage threshold for lines (100%) not met: 50% +Jest: \\"./banana.js\\" coverage threshold for lines (100%) not met: 50% +" +`; + +exports[`file is matched by all path and glob threshold groups 2`] = ` +"Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: <> diff --git a/e2e/__tests__/coverage_threshold.test.js b/e2e/__tests__/coverage_threshold.test.js index 45579636b476..d0c81cef0d98 100644 --- a/e2e/__tests__/coverage_threshold.test.js +++ b/e2e/__tests__/coverage_threshold.test.js @@ -10,7 +10,7 @@ 'use strict'; const path = require('path'); -const {cleanup, writeFiles} = require('../Utils'); +const {cleanup, writeFiles, extractSummary} = require('../Utils'); const runJest = require('../runJest'); const DIR = path.resolve(__dirname, '../coverage-threshold'); @@ -18,10 +18,6 @@ const DIR = path.resolve(__dirname, '../coverage-threshold'); beforeEach(() => cleanup(DIR)); afterAll(() => cleanup(DIR)); -const replaceTime = str => { - return str.replace(/\d*\.?\d+m?s/g, '<>'); -}; - test('exits with 1 if coverage threshold is not met', () => { const pkgJson = { jest: { @@ -49,9 +45,12 @@ test('exits with 1 if coverage threshold is not met', () => { }); const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {rest, summary} = extractSummary(stderr); + expect(status).toBe(1); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); expect(stdout).toMatchSnapshot('stdout'); - expect(replaceTime(stderr)).toMatchSnapshot('stderr'); }); test('exits with 1 if path threshold group is not found in coverage data', () => { @@ -81,10 +80,12 @@ test('exits with 1 if path threshold group is not found in coverage data', () => }); const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {rest, summary} = extractSummary(stderr); expect(status).toBe(1); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); expect(stdout).toMatchSnapshot('stdout'); - expect(replaceTime(stderr)).toMatchSnapshot('stderr'); }); test('exits with 0 if global threshold group is not found in coverage data', () => { @@ -157,10 +158,12 @@ test('excludes tests matched by path threshold groups from global group', () => }); const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {rest, summary} = extractSummary(stderr); expect(status).toBe(1); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); expect(stdout).toMatchSnapshot('stdout'); - expect(replaceTime(stderr)).toMatchSnapshot('stderr'); }); test('file is matched by all path and glob threshold groups', () => { @@ -172,10 +175,10 @@ test('file is matched by all path and glob threshold groups', () => { './': { lines: 100, }, - 'ban*.js': { + './ban*.js': { lines: 100, }, - 'banana.js': { + './banana.js': { lines: 100, }, }, @@ -196,10 +199,17 @@ test('file is matched by all path and glob threshold groups', () => { }); const {stdout, stderr, status} = runJest(DIR, ['--coverage', '--ci=false']); + const {rest, summary} = extractSummary( + /* This test also runs on windows and when the glob fails it outputs + the system specific absolute path to the test file. */ + stderr.replace( + path.resolve(DIR, './banana.js'), + '<>', + ), + ); expect(status).toBe(1); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); expect(stdout).toMatchSnapshot('stdout'); - expect( - replaceTime(stderr).replace(process.cwd(), '<>'), - ).toMatchSnapshot('stderr'); }); From 66798eb4e9f76a0046d9ccd34a46e2fbf0f9886e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 8 Aug 2018 22:26:52 +0200 Subject: [PATCH 9/9] move changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da48060032b7..1481414a373d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - `[jest-config]` Update default config for testURL from 'about:blank' to 'http://localhost' to address latest JSDOM security warning. ([#6792](https://github.com/facebook/jest/pull/6792)) - `[jest-cli]` Fix `testMatch` not working with negations ([#6648](https://github.com/facebook/jest/pull/6648)) - `[jest-cli]` Don't report promises as open handles ([#6716](https://github.com/facebook/jest/pull/6716)) +- `[jest-cli]` Improve the message when running coverage while there are no files matching global threshold ([#6334](https://github.com/facebook/jest/pull/6334)) ## 23.4.2 @@ -123,7 +124,6 @@ - `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310)) - `[jest-cli]` Fix run beforeAll in excluded suites tests" mode. ([#6234](https://github.com/facebook/jest/pull/6234)) - `[jest-haste-map`] Compute SHA-1s for non-tracked files when using Node crawler ([#6264](https://github.com/facebook/jest/pull/6264)) -- `[jest-cli]` Improve the message when running coverage while there are no files matching global threshold ([#6334](https://github.com/facebook/jest/pull/6334)) ### Chore & Maintenance