jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.2k stars 6.46k forks source link

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

Open wilhen01 opened 2 years ago

wilhen01 commented 2 years ago

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
github-actions[bot] commented 2 years ago

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.

ibratoev commented 2 years ago

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.

github-actions[bot] commented 2 years ago

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.

wilhen01 commented 2 years ago

Bump.

calpa commented 2 years ago

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

github-actions[bot] commented 2 years ago

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.

sghsri commented 2 years ago

bump

sghsri commented 2 years ago

+1 for this question

StoneCypher commented 2 years ago

Outstanding since 2017, closed by bot

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

StoneCypher commented 2 years ago

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.

wilhen01 commented 2 years ago

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.

StoneCypher commented 2 years ago

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.

wilhen01 commented 2 years ago

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

github-actions[bot] commented 2 years ago

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.

wilhen01 commented 2 years ago

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

github-actions[bot] commented 2 years ago

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.

StoneCypher commented 2 years ago

Not stale. Again.

SimenB commented 2 years ago

There is #12536 fwiw

andersondanilo commented 1 year ago

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
}