Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console support #45

Closed
mfulton26 opened this issue Mar 30, 2018 · 19 comments · Fixed by #96
Closed

console support #45

mfulton26 opened this issue Mar 30, 2018 · 19 comments · Fixed by #96

Comments

@mfulton26
Copy link

I'm not sure if this is possible but it would be a huge benefit if the results of any console.log statements, etc. were made available inside of <testcase> elements using <system-out> and <system-err>.

@palmerj3
Copy link
Collaborator

If I get time this weekend I can revisit this.

But the last time I looked into it I was worried that it would contain too much irrelevant data. My main worry was if people used the --useStderr cli flag.

@mfulton26
Copy link
Author

Yeah, I know that Jest buffers console output when running tests concurrently. I wonder if something similar could be done where console.log, console.warn, et. al. are wrapped to intercept what is being logged and then buffered along with the test name and then associated with each <testcase>.

@palmerj3
Copy link
Collaborator

palmerj3 commented Apr 3, 2018

@mfulton26 I THINK that jest already handles this and by the time this information is sent to a testResultsProcessor that it's already in a non-buffered organized format.

If I get some time this week I can confirm that. But feel free to send a PR if you have time and I'll review!

:)

@SimenB
Copy link
Member

SimenB commented Sep 17, 2018

Now that this module is a reporter, you can implement this

@palmerj3
Copy link
Collaborator

I'm looking at this now and it appears to be doable however the spec we abide by appears that system-out is associated with a test suite (which for jest would be an individual test file with many test cases). Would that be useful?

I am going to make a PR for the fun of it which adds this and get your feedback.

@SimenB
Copy link
Member

SimenB commented Mar 11, 2019

Note that it doesn't buffer when --silent is passed. Should be fixed, but worth to note

@palmerj3
Copy link
Collaborator

I've submitted a PR which adds this functionality. Please give me your feedback when you can. I'll leave it open for a few days before merging if there is no feedback.

#90

@TonyG623
Copy link

TonyG623 commented May 2, 2019

@palmerj3 I see the PR was merged. Just added the variable to my jest config as shown:

  reporters: [
    'default',
    [
      'jest-junit',
      {
        suiteName: 'Unit Tests',
        outputDirectory: '.',
        outputName: 'coverage/test-results.xml',
        classNameTemplate: '{classname}-{title}',
        titleTemplate: '{classname}-{title}',
        ancestorSeparator: ' › ',
        usePathForSuiteName: 'true',
        includeConsoleOutput: 'true',
      },
    ],
  ],

Using 6.4.0 and im not seeing any of my console logs in the my xml report tho :/

I think I have that set up correctly based on the readme.

@ghost
Copy link

ghost commented May 3, 2019

I also tried it, same as @TonyG623, and saw no output in my XML report.

package.json

"dependencies": {
    "jest": "^24.7.1",
    "jest-junit": "^6.4.0"
  }

jest.config.js

reporters: [
    "default",
    [
      "jest-junit",
      {
        ancestorSeparator: " › ",
        classNameTemplate: "{classname}-{title}",
        includeConsoleOutput: "true",
        outputDirectory: ".",
        outputName: "coverage/test-results.xml",
        suiteName: "Unit Tests",
        titleTemplate: "{classname}-{title}",
        usePathForSuiteName: "true"
      }
    ]
  ],

tests/dummy.test.js

describe("console log", () => {
  test("outputs to XML report", () => {
    console.log("nope")
    throw "yes"
  })
})

XML report:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Unit Tests" tests="1" failures="1" time="1.988">
  <testsuite name="tests\dummy.test.js" errors="0" failures="1" skipped="0" timestamp="2019-05-03T08:00:06" time="1.905" tests="1">
    <testcase classname="console log-outputs to XML report" name="console log-outputs to XML report" time="0.01">
      <failure>yes</failure>
    </testcase>
  </testsuite>
</testsuites>

@palmerj3
Copy link
Collaborator

palmerj3 commented May 3, 2019

Hmm thanks for reporting. Can you both tell me the version(s) of jest you're running this against? (nevermind I see the versions now!)

Will take a look

@ghost
Copy link

ghost commented May 6, 2019

Not sure if this is related, but could it be a Windows vs. Linux path thing somewhere in your code?

Tried cloning the repo and running the tests. Got a bunch of these errors:


 ● buildJsonResults › should return the proper name from testFilePath when usePathForSuiteName is "true"; no appDirectory set

   expect(received).toBe(expected) // Object.is equality

   Expected: "path/to/test/__tests__/foo.test.js"
   Received: "path\\to\\test\\__tests__\\foo.test.js"

     101 |         usePathForSuiteName: "true"
     102 |       }));
   > 103 |     expect(jsonResults.testsuites[1].testsuite[0]._attr.name).toBe('path/to/test/__tests__/foo.test.js');
         |                                                               ^
     104 |   });
     105 |
     106 |   it('should return the proper name from testFilePath when usePathForSuiteName is "true"; with appDirectory set', () => {

     at Object.toBe (__tests__/buildJsonResults.test.js:103:63)

@jkblume
Copy link

jkblume commented May 21, 2019

Hi there, join the discussion again and hope i can help a little bit more this time.
Actually we want to test a react project but for testing purpose i created a minimal project setup here:
https://github.com/jkblume/simple-app

Steps to reproduce the error:

  • Clone the project and step into it
  • npm install
  • npm run test
  • Check junit.xml does not contain the expected stdout that is logged in test and implementation code

I added some debug output to the jest junit project and found, that the console attribute of the suite object is undefined. Maybe this is already the error?

{
    console: undefined,
    ... 
}

Hope that a concrete example helps to solve the problem or approach the origin of the error.

@jkblume
Copy link

jkblume commented May 28, 2019

I dived a little bit into jest core code and found that ReportDispatcher overrides .console property with undefined. I opened an issue to discuss this: jestjs/jest#8499

@brownbl1
Copy link

Is there an update on this? I'm seeing the same behavior? Did any of you find a workaround?

@palmerj3
Copy link
Collaborator

Hey sorry for the radio silence on this one. It's somewhat complex.

I believe I have a fix for jest to re-enable this functionality. Will link the PR here shortly and hopefully it's approved.

@palmerj3
Copy link
Collaborator

I just published jest-junit 7.0.0 with a fix that should now allow for console output to be written to junit.xml.

A few caveats (due to breaking changes in jest):

  1. You must have at least 2 test files in order for console output to be sent to jest-junit
  2. You cannot set verbose to true in jests config

@SimenB
Copy link
Member

SimenB commented Jul 23, 2019

Both of those caveats are bugs in jest... (jestjs/jest#6441 sorta relates, but reporters should always get console output regardless of silent/verbose - it should decide itself based on those options whether to print it or not)

@CMCDragonkai
Copy link

@palmerj3 are those 2 caveats still true?

@CMCDragonkai
Copy link

What's the reason to do this:

You cannot set verbose to true in jests config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants