Home » Nodejs » Grunt-mocha-test Xunit reporter writes the whole console output to the xunit file

Grunt-mocha-test Xunit reporter writes the whole console output to the xunit file

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m using grunt-mocha-test for running our mocha tests. I want to be able to run the tests and generate the xunit report and get the coverage (with blanket.js). I have the following sections in my gruntfile:

mochaTest: {
        'unit-jenkins': {
            options: {
                reporter: 'XUnit',
                require: paths.test + '/blanket',
                captureFile: paths.tmp + '/xunit.xml'
            },
            src: [paths.test + '/unit/**/*.js'],        
        },
        'integration-jenkins': {
            options: {
                reporter: 'XUnit',
                require: paths.test + '/blanket',
                captureFile: paths.tmp + '/xunit.xml'
            },
            src: [paths.test + '/integration/**/*.js']
        },
        coverage: {
            options: {
                reporter: 'html-cov',
                quiet: true,
                captureFile: paths.tmp + '/coverage.html'
            },
            src: [paths.test +  '/**/*.js']
        }
    },

and

    grunt.registerTask('test-jenkins', [
    'mochaTest:unit-jenkins',       // run unit tests
    'mochaTest:integration-jenkins',    // run unit tests
]);

when I run grunt test-jenkins I can see the test output and the xunit output on the console. Moreover, the xunit file is created, however, it consists of both the tests output and the xunit output, e.g.:

[14:30:17.164Z] TRACE App: HTTP Response /versions
HTTP/1.1 200 OK
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 46
ETag: "-1409762768"
Date: Mon, 17 Feb 2014 14:30:17 GMT
Connection: close
<testsuite name="Mocha Tests" tests="1" failures="0" errors="0" skipped="0" timestamp="Mon, 17 Feb 2014 14:30:17 GMT" time="0.029">
<testcase classname="Application" name="should contain description of API versions" time="0.028"/>
</testsuite>

How should grunt-mocha-test be configured so that the xunit output file consists solely of the xunit output?

Answers:

I had the same problem with Selenium. I got around it with the following task:

    var outputFile = process.env.MOCHA_OUTPUT_FILE || 'xunit_results.xml';
    grunt.registerTask('cleanXunitFile', 'Remove Selenium/WebDriver output from xunit file', function() {
        if (grunt.file.exists('./' + outputFile)) {
            var file = grunt.file.read('./' + outputFile); 
            if (file.indexOf("<testsuite")) {
                grunt.file.write('./' + outputFile, file.substring(file.indexOf("<testsuite")));
            }
        }
        else {
            grunt.log.error("'cleanXunitFile' task was specified but file " + outputFile + " does not exist.");
        }
    });

Questions:
Answers:

I can see the test output and the xunit output on the console. Moreover, the xunit file is created, however, it consists of both the tests output and the xunit output

I am afraid this is a know bug of mocha.

There is a pending pull request trying to address these issues, see
https://github.com/visionmedia/mocha/pull/1218