Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the message when run coverage while there are no tests #6334

Merged
merged 13 commits into from Aug 9, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
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
@@ -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
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');
});