19

So if I put a console.log inside a test the console.log will appear after the tests e.g.

  authentication.spec.js
    register
      ✓ should be able to insert (128ms)
      ✓ should fail because of duplicate (100ms)
      ✓ should have user id assigned (1ms)
    login
      ✓ should be able to login for correct user (117ms)
      ✓ should fail for incorrect user (14ms)

  console.log tests/unit/authentication.spec.js:110
    we can see a message

What I would like to see instead is something like:

  authentication.spec.js
    register
      ✓ should be able to insert (128ms)
      ✓ should fail because of duplicate (100ms)
      ✓ should have user id assigned (1ms)
    login
      ✓ should be able to login for correct user (117ms)
        console.log tests/unit/authentication.spec.js:110
           we can see a message
      ✓ should fail for incorrect user (14ms)

So the console.log should be appearing with ✓ should be able to login for correct user in this case

When I was using Mocha I was using mocha-logger

A. L
  • 11,695
  • 23
  • 85
  • 163
  • Does this answer your question? [Console.log statements output nothing at all in Jest](https://stackoverflow.com/questions/48695717/console-log-statements-output-nothing-at-all-in-jest) – Michael Freidgeim Jul 03 '20 at 22:40
  • Possibly related: https://stackoverflow.com/questions/58936650/javascript-jest-how-to-show-logs-from-test-case-only-when-test-fails/68145722#68145722 (One can remove the check for a failed test and instead show console outputs for all tests instead) – sqwk Jun 26 '21 at 19:38

3 Answers3

11

As far as I know this is not easily possible, though a couple of places to look for more information (not out of the box):

Jest allows to use custom reporters: https://jestjs.io/docs/en/configuration.html#reporters-array-modulename-modulename-options , so you can write your own reporter and display output differently. At the moment though you don't get updates for separate tests, just test suits, here is an issue created by Dan Abramov: https://github.com/facebook/jest/issues/6616 .

From the github thread above - Reporter interface for now looks like:

export default class BaseReporter {
  onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {}

  // these are actually for the test suite, not individual test results
  onTestStart(test: Test) {}
  onTestResult(test: Test, testResult: TestResult, results: AggregatedResult) {}

  onRunComplete(contexts: Set<Context>, results: AggregatedResult ): ?Promise<void> {}
}

I didn't find a predefined way to pass parameters from test though into the testResult object. So you are mostly limited to logging information based on test names. Below is an example of testResult property inside testResult object:

testResults:
  [ { ancestorTitles: [Array],
       duration: 5,
       failureMessages: [],
       fullName: 'add should add two numbers',
       location: null,
       numPassingAsserts: 0,
       status: 'passed',
       title: 'should add two numbers' } ],

As you can see this is all the information standard reporter uses: test name, duration, status. For reference the default reporter is here: https://github.com/facebook/jest/blob/7b7fd01350/packages/jest-cli/src/reporters/default_reporter.js

Georgy
  • 2,410
  • 3
  • 21
  • 35
  • i don't think the question is about test coverage report. We can have the coverage report too but console logs in the test cases has to be handled differently. – nircraft Nov 06 '18 at 20:53
7

Yes, you can have it logged. You may need to Add --verbose false to package.json "test";

Ex: "scripts": { "test": "jest --watch --verbose false" }

See the details here at github for jest bugs

nircraft
  • 8,242
  • 5
  • 30
  • 46
1

I use this:

// package.json

...
    "scripts": {
        ...
        "test": "react-scripts test --verbose=true",
        ...
    }
...

In a console, it looks like...

enter image description here

Gennady Magomaev
  • 1,157
  • 6
  • 8