jest icon indicating copy to clipboard operation
jest copied to clipboard

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

Open wilhen01 opened this issue 2 years ago • 15 comments

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

> [email protected] 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

wilhen01 avatar Feb 18 '22 15:02 wilhen01

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.

github-actions[bot] avatar Mar 20 '22 15:03 github-actions[bot]

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.

ibratoev avatar Mar 21 '22 19:03 ibratoev

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.

github-actions[bot] avatar Apr 20 '22 19:04 github-actions[bot]

Bump.

wilhen01 avatar Apr 21 '22 07:04 wilhen01

+1 for this question, Similar problem: https://github.com/facebook/jest/issues/10914

calpa avatar May 12 '22 17:05 calpa

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.

github-actions[bot] avatar Jun 11 '22 17:06 github-actions[bot]

bump

sghsri avatar Jun 13 '22 16:06 sghsri

+1 for this question

sghsri avatar Jun 13 '22 16:06 sghsri

Outstanding since 2017, closed by bot

https://github.com/facebook/jest/issues/2927

StoneCypher avatar Jun 25 '22 17:06 StoneCypher

Not a fix, but a practical workaround: instead of using --json --outputFile, install a json-outputting reporter. The summary box is not supplanted by other reporters.

I'm using jest-json-reporter2.

StoneCypher avatar Jun 25 '22 17:06 StoneCypher

Thanks for the tip @StoneCypher. Does jest-json-reporter2 use the same output format as --json --outputFile?

We're using the output file with DangerJS to show unit test results in our PRs, so it's only a good workaround for us if the format is the same.

wilhen01 avatar Jun 28 '22 11:06 wilhen01

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.

StoneCypher avatar Jun 28 '22 13:06 StoneCypher

Cool, thanks, I'll have a play when I get a chance 👍

wilhen01 avatar Jun 28 '22 14:06 wilhen01

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.

github-actions[bot] avatar Jul 28 '22 14:07 github-actions[bot]

Bump. I'd still like this fixed...

wilhen01 avatar Jul 28 '22 15:07 wilhen01

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.

github-actions[bot] avatar Aug 27 '22 15:08 github-actions[bot]

Not stale. Again.

StoneCypher avatar Aug 27 '22 18:08 StoneCypher

There is #12536 fwiw

SimenB avatar Aug 28 '22 10:08 SimenB

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
}

andersondanilo avatar Dec 20 '22 22:12 andersondanilo