Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can jest output console log within test block output

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

like image 561
A. L Avatar asked Oct 29 '18 03:10

A. L


People also ask

Does console log work in jest?

Jest by default prints all console. log (warnings, errors, etc) messages to the console. That's great - it helps you understand what's going on in your code when tests run.

How do I display the output of a console log?

Steps to Open the Console Log in Google Chrome By default, the Inspect will open the "Elements" tab in the Developer Tools. Click on the "Console" tab which is to the right of "Elements". Now you can see the Console and any output that has been written to the Console log.

How do I hide console error in jest?

If using Jest test framework, you can turn off all console messages by one command: jest --silent .

Does console log impact performance?

console. log by itself doesn't really impact performance in a way that you'll notice unless you bind it to a scroll / resize handler. These get called alot and if your browser has to send text to the console like 30/60x a second it can get ugly.


2 Answers

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

like image 117
Georgy Avatar answered Sep 19 '22 04:09

Georgy


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

like image 20
nircraft Avatar answered Sep 19 '22 04:09

nircraft