简体   繁体   中英

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 eg

  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

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

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

I use this:

// package.json

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

In a console, it looks like...

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM