Meteor-Community-Packages / meteor-mocha

A Mocha test driver package for Meteor 1.3+. This package reports server AND client test results in the server console and can be used for running tests on a CI server or locally.
https://packosphere.com/meteortesting/mocha
MIT License
67 stars 40 forks source link

Bugfix/xunit browser output #158

Open imajus opened 2 weeks ago

imajus commented 2 weeks ago

This is a fix for the XUNIT reporter output from the client. I was experiencing issues with this when running client tests in headless mode via Puppeteer.

Here's a reproduction repository: https://github.com/imajus/meteor-browser-tests-issue/

Expected to get XML in test-results/client.xml but getting the following contents:

In the test groupIn the test case<testsuite name="Mocha Tests" tests="1" failures="0" errors="1" skipped="0" timestamp="Fri, 20 Sep 2024 05:34:25 GMT" time="0.003">
<testcase classname="Example" name="test" time="0.001"><failure>undefined undefined undefined
AssertionError: undefined undefined undefined
    at Context.&#x3C;anonymous&#x3E; (app/app.js?hash=818e846856c24f4b042929071138d0f3cfb8a773:20:12)
    at callFn (packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:13223:23)
    at Test$4.Runnable$3.run (packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:13211:7)
    at Runner.runTest (packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:14756:12)
    at http://localhost:3000/packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:14879:14
    at next (packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:14671:16)
    at http://localhost:3000/packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:14681:9
    at next (packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:14564:16)
    at http://localhost:3000/packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:14649:7
    at timeslice (packages/meteortesting_mocha-core.js?hash=dd1ee602c0eabe4198d6106e7f5734b6963d105c:20603:29)</failure></testcase>
</testsuite>

As part of this PR there's also a refactoring which makes is easier to handle the MOCHA_REPORTER environment variable value in the source code.

imajus commented 2 weeks ago

Resolves this issue.

imajus commented 2 weeks ago

FYI the solution is based on the fact that Xunit output can only write to console or process.stdout (if it's present) in a browser. To my knowledge, the process.stdout option is only available for PhantomJS driver.

So the only way to get the Xunit reporter output is to somehow "scrap" it from the console output. Given that it's garanteed to be XML tags, I've implemented a hacky, yet seemingly reliable solution to filter out all non-XML-like lines from the browser's console log when piping the output to a file. Additionally, the filtered out console log lines aren't just thrown away silently, but are instead logged in the server execution log.