can jest output console log within test block output
Asked Answered
C

4

20

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

Camel answered 29/10, 2018 at 3:17 Comment(2)
Does this answer your question? Console.log statements output nothing at all in JestChari
Possibly related: #58937150 (One can remove the check for a failed test and instead show console outputs for all tests instead)Measures
I
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

Ivanovo answered 2/11, 2018 at 18:41 Comment(1)
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.Conspiracy
C
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

Conspiracy answered 6/11, 2018 at 20:36 Comment(0)
J
1

I use this:

// package.json

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

In a console, it looks like...

enter image description here

Jerboa answered 9/2, 2021 at 16:37 Comment(0)
L
0

Use the verbose Flag:

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "test-only": "bash test-only.sh",
    "test": "npm run test:unit && npm run test:integration",
    "test:watch": "jest --watch",
    "test:integration": "jest test/integration",
    "test:unit": "jest test/unit"
  },

test-only.sh:

filtered_test=$(grep -rnwl ./test -e "test.only\|it.only\|describe.only" --include \*.js | tr '\n' ' ')
jest --verbose --coverage --runInBand --detectOpenHandles $filtered_test
Leguminous answered 1/6 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.