jest: [Bug]: Using --outputFile --json suppresses CLI coverage output

Version

27.5.1

Steps to reproduce

  1. Clone https://github.com/wilhen01/jest-outputfile-bug-repro
  2. npm install
  3. npm run test-outputfile to see output using --outputFile --json
  4. npm run test-no-outputfile to see output without those flags

Expected behavior

With code coverage configured. via jest.config.js I would expect to see CLI coverage output, regardless of the use of --outputFile --json

e.g.

 PASS  test/hello.test.ts
  hello world function
    ✓ returns the correct string (1 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 index.ts |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.83 s, estimated 2 s
Ran all test suites.

Actual behavior

When using --outputFile --json the CLI coverage output is suppressed. Regular test output on the command line remains.

e.g.

❯ npm run test-outputfile

> jest-outputfile-bug-repro@1.0.0 test-outputfile
> jest --outputFile test-results.json --json

 PASS  test/hello.test.ts
  hello world function
    ✓ returns the correct string (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.935 s, estimated 3 s
Ran all test suites.
Test results written to: test-results.json

Additional context

No response

Environment

System:
    OS: macOS 12.2
    CPU: (10) x64 Apple M1 Pro
  Binaries:
    Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node
    npm: 8.5.0 - ~/.nvm/versions/node/v14.19.0/bin/npm
  npmPackages:
    jest: latest => 27.5.1

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 19 (1 by maintainers)

Most upvoted comments

While the PR is not merged, an workarround to get the same file can be:

jest --coverage --testResultsProcessor=my-results-processor.js

my-results-processor.js

const { writeFileSync } = require('fs')
const { join } = require('path')
const { formatTestResults } = require('@jest/test-result')

module.exports = (testResults) => 
  writeFileSync(
    join(process.cwd(), 'coverage', 'my-result.json'),
    JSON.stringify(formatTestResults(testResults))
  )

  return testResults
}

There is #12536 fwiw

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

Reporter2 has a short output and a long output. The short output is some simple summary statistics. For my project, the long output was 1.5 meg, and I didn’t feel like digging through it. I honestly have no idea.

This is a really small but annoying issue for us as well. There should be an easy way to get the test json output while having the nice CLI coverage during CI.

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.