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

2

4 Answers 4

11
+25

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

1

I use this:

// package.json

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

In a console, it looks like...

enter image description here

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.