From d32c45e269a3dab26c84a0d4ef26ea9c1ed46da5 Mon Sep 17 00:00:00 2001 From: Seong Min Park <32555977+notoriousmango@users.noreply.github.com> Date: Sun, 14 Dec 2025 15:21:18 +0900 Subject: [PATCH 1/2] prints coverage when using --outputFile with --json --- .../__snapshots__/coverageReport.test.ts.snap | 18 ++++++++++++++++++ e2e/__tests__/coverageReport.test.ts | 15 +++++++++++++++ .../src/__tests__/normalize.test.ts | 14 ++++++++++++++ packages/jest-config/src/normalize.ts | 5 +++-- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap b/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap index c37043ff20e4..3f32ad42246b 100644 --- a/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap +++ b/e2e/__tests__/__snapshots__/coverageReport.test.ts.snap @@ -138,3 +138,21 @@ Functions : 50% ( 3/6 ) Lines : 60% ( 12/20 ) ================================================================================" `; + +exports[`prints coverage when using --outputFile with --json 1`] = ` +"-------------------------------------|---------|----------|---------|---------|------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +-------------------------------------|---------|----------|---------|---------|------------------- +All files | 60 | 0 | 50 | 60 | + coverage-report | 47.36 | 0 | 25 | 50 | + file.js | 100 | 100 | 100 | 100 | + notRequiredInTestSuite.js | 0 | 0 | 0 | 0 | 8-19 + otherFile.js | 100 | 100 | 100 | 100 | + sum.js | 87.5 | 100 | 50 | 100 | + sumDependency.js | 0 | 0 | 0 | 0 | 8-13 + 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 | +-------------------------------------|---------|----------|---------|---------|-------------------" +`; diff --git a/e2e/__tests__/coverageReport.test.ts b/e2e/__tests__/coverageReport.test.ts index e3bffe332dac..e8b0d0b8409f 100644 --- a/e2e/__tests__/coverageReport.test.ts +++ b/e2e/__tests__/coverageReport.test.ts @@ -189,3 +189,18 @@ test('generates coverage when using the testRegex config param ', () => { expect(stdout).toMatchSnapshot(); expect(exitCode).toBe(0); }); + +test('prints coverage when using --outputFile with --json', () => { + const outputFileName = 'sum.result.json'; + const outputFilePath = path.join(DIR, outputFileName); + + const {stdout, exitCode} = runJest(DIR, [ + '--json', + `--outputFile=${outputFileName}`, + '--coverage', + ]); + + expect(stdout).toMatchSnapshot(); + expect(exitCode).toBe(0); + fs.unlinkSync(outputFilePath); +}); diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index 3939302c890a..f140d1205337 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -174,6 +174,20 @@ it('sets coverageReporters correctly when argv.json is set', async () => { expect(options.coverageReporters).toEqual(['json', 'lcov', 'clover']); }); +it('keeps text coverage reporter when argv.json and outputFile are set', async () => { + const {options} = await normalize( + { + rootDir: '/root/path/foo', + }, + { + json: true, + outputFile: 'results.json', + } as Config.Argv, + ); + + expect(options.coverageReporters).toEqual(['json', 'text', 'lcov', 'clover']); +}); + describe('rootDir', () => { it('throws if the options is missing a rootDir property', async () => { expect.assertions(1); diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index e2ed840cf020..3688b840ea59 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -1110,8 +1110,9 @@ export default async function normalize( newOptions.testMatch = []; } - // If argv.json is set, coverageReporters shouldn't print a text report. - if (argv.json) { + // If argv.json is set without an outputFile, coverageReporters shouldn't print + // a text report to avoid polluting the JSON written to stdout. + if (argv.json && !argv.outputFile) { newOptions.coverageReporters = (newOptions.coverageReporters || []).filter( reporter => reporter !== 'text', ); From c76bf2367451d4bb02e9f0d1e498bd163a687d52 Mon Sep 17 00:00:00 2001 From: Seong Min Park <32555977+notoriousmango@users.noreply.github.com> Date: Sun, 14 Dec 2025 15:27:07 +0900 Subject: [PATCH 2/2] add Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91113593e25d..ffdc43a1f027 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - `[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)) - `[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)) - `[jest-util]` Make sure `process.features.require_module` is `false` ([#15867](https://github.com/jestjs/jest/pull/15867)) +- `[jest-config]` Keep CLI coverage output when using `--json` with `--outputFile` ([#15918](https://github.com/jestjs/jest/pull/15918)) ### Chore & Maintenance