Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6ce37e

Browse files
fix: Keep CLI coverage output when using --json with --outputFile (#15918)
1 parent 0b51155 commit a6ce37e

5 files changed

Lines changed: 51 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `[jest-runtime]` Fix issue where user cannot utilize dynamic import despite specifying `--experimental-vm-modules` Node option ([#15842](https://github.com/jestjs/jest/pull/15842))
1313
- `[jest-test-sequencer]` Fix issue where failed tests due to compilation errors not getting re-executed even with `--onlyFailures` CLI option ([#15851](https://github.com/jestjs/jest/pull/15851))
1414
- `[jest-util]` Make sure `process.features.require_module` is `false` ([#15867](https://github.com/jestjs/jest/pull/15867))
15+
- `[jest-config]` Keep CLI coverage output when using `--json` with `--outputFile` ([#15918](https://github.com/jestjs/jest/pull/15918))
1516

1617
### Chore & Maintenance
1718

e2e/__tests__/__snapshots__/coverageReport.test.ts.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,21 @@ Functions : 50% ( 3/6 )
138138
Lines : 60% ( 12/20 )
139139
================================================================================"
140140
`;
141+
142+
exports[`prints coverage when using --outputFile with --json 1`] = `
143+
"-------------------------------------|---------|----------|---------|---------|-------------------
144+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
145+
-------------------------------------|---------|----------|---------|---------|-------------------
146+
All files | 60 | 0 | 50 | 60 |
147+
coverage-report | 47.36 | 0 | 25 | 50 |
148+
file.js | 100 | 100 | 100 | 100 |
149+
notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8-19
150+
otherFile.js | 100 | 100 | 100 | 100 |
151+
sum.js | 87.5 | 100 | 50 | 100 |
152+
sumDependency.js | 0 | 0 | 0 | 0 | 8-13
153+
coverage-report/cached-duplicates/a | 100 | 100 | 100 | 100 |
154+
identical.js | 100 | 100 | 100 | 100 |
155+
coverage-report/cached-duplicates/b | 100 | 100 | 100 | 100 |
156+
identical.js | 100 | 100 | 100 | 100 |
157+
-------------------------------------|---------|----------|---------|---------|-------------------"
158+
`;

e2e/__tests__/coverageReport.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,18 @@ test('generates coverage when using the testRegex config param ', () => {
189189
expect(stdout).toMatchSnapshot();
190190
expect(exitCode).toBe(0);
191191
});
192+
193+
test('prints coverage when using --outputFile with --json', () => {
194+
const outputFileName = 'sum.result.json';
195+
const outputFilePath = path.join(DIR, outputFileName);
196+
197+
const {stdout, exitCode} = runJest(DIR, [
198+
'--json',
199+
`--outputFile=${outputFileName}`,
200+
'--coverage',
201+
]);
202+
203+
expect(stdout).toMatchSnapshot();
204+
expect(exitCode).toBe(0);
205+
fs.unlinkSync(outputFilePath);
206+
});

packages/jest-config/src/__tests__/normalize.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ it('sets coverageReporters correctly when argv.json is set', async () => {
174174
expect(options.coverageReporters).toEqual(['json', 'lcov', 'clover']);
175175
});
176176

177+
it('keeps text coverage reporter when argv.json and outputFile are set', async () => {
178+
const {options} = await normalize(
179+
{
180+
rootDir: '/root/path/foo',
181+
},
182+
{
183+
json: true,
184+
outputFile: 'results.json',
185+
} as Config.Argv,
186+
);
187+
188+
expect(options.coverageReporters).toEqual(['json', 'text', 'lcov', 'clover']);
189+
});
190+
177191
describe('rootDir', () => {
178192
it('throws if the options is missing a rootDir property', async () => {
179193
expect.assertions(1);

packages/jest-config/src/normalize.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,8 +1110,9 @@ export default async function normalize(
11101110
newOptions.testMatch = [];
11111111
}
11121112

1113-
// If argv.json is set, coverageReporters shouldn't print a text report.
1114-
if (argv.json) {
1113+
// If argv.json is set without an outputFile, coverageReporters shouldn't print
1114+
// a text report to avoid polluting the JSON written to stdout.
1115+
if (argv.json && !argv.outputFile) {
11151116
newOptions.coverageReporters = (newOptions.coverageReporters || []).filter(
11161117
reporter => reporter !== 'text',
11171118
);

0 commit comments

Comments
 (0)