Skip to content

Commit

Permalink
fix: Improve the message when run coverage while there are no tests (#…
Browse files Browse the repository at this point in the history
…6334)

* 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.

* use Object.assign

* 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.

* 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.

* revert unnecessary test changes

* linting

* Replace process.cwd() in stderr snapshot test.

* 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)

* move changelog entry
  • Loading branch information
rhysawilliams2010 authored and thymikee committed Aug 9, 2018
1 parent a5fa590 commit 08cb885
Show file tree
Hide file tree
Showing 5 changed files with 490 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `[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-each]` Add timeout support to parameterised tests ([#6660](https://github.com/facebook/jest/pull/6660))
- `[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

Expand Down
110 changes: 110 additions & 0 deletions e2e/__tests__/__snapshots__/coverage_threshold.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`excludes tests matched by path threshold groups from global group 1`] = `
"PASS __tests__/banana.test.js
✓ banana
Jest: \\"global\\" coverage threshold for lines (100%) not met: 0%
"
`;

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: <<REPLACED>>
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 1`] = `
"PASS __tests__/a-banana.js
✓ banana
Jest: \\"global\\" coverage threshold for lines (90%) not met: 50%
"
`;
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: <<REPLACED>>
Ran all test suites.
"
`;
exports[`exits with 1 if coverage threshold is not met: stdout 1`] = `
"----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
Expand All @@ -9,3 +63,59 @@ All files | 50 | 100 | 0 | 50 |
----------------|----------|----------|----------|----------|-------------------|
"
`;
exports[`exits with 1 if path threshold group is not found in coverage data 1`] = `
"PASS __tests__/banana.test.js
✓ banana
Jest: Coverage data for apple.js was not found.
"
`;
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: <<REPLACED>>
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 1`] = `
"PASS __tests__/banana.test.js
✓ banana
Jest: \\"./\\" coverage threshold for lines (100%) not met: 50%
Jest: \\"<<FULL_PATH_TO_BANANA_JS>>\\" 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: <<REPLACED>>
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 |
-----------|----------|----------|----------|----------|-------------------|
"
`;
169 changes: 167 additions & 2 deletions e2e/__tests__/coverage_threshold.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -44,7 +44,172 @@ 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']);
const {rest, summary} = extractSummary(stderr);

expect(status).toBe(1);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
expect(stdout).toMatchSnapshot('stdout');
});

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']);
const {rest, summary} = extractSummary(stderr);

expect(status).toBe(1);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
expect(stdout).toMatchSnapshot('stdout');
});

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']);
const {rest, summary} = extractSummary(stderr);

expect(status).toBe(1);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
expect(stdout).toMatchSnapshot('stdout');
});

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']);
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'),
'<<FULL_PATH_TO_BANANA_JS>>',
),
);

expect(status).toBe(1);
expect(stdout).toMatchSnapshot();
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
expect(stdout).toMatchSnapshot('stdout');
});
Loading

0 comments on commit 08cb885

Please sign in to comment.