Search code examples
javascriptunit-testinggulpbambooqunit

Output gulp-qunit console output to file


I am running unit tests using QUnit and trying to integrate QUnit into our build automation and Continuous Integration process. For Atlassian Bamboo to parse the test output it requires the test output to be in an xml file. I can generate a console log that is in the required xml format by using the qunit-reporter-junit plugin. When Gulp-QUnit runs our test-runner.html file it outputs the console.log to the screen. My problem is that I cannot find a way to pipe this console.log output into a file.

I have tried the following approaches:

Using gulp-log-capture plugin (does nothing):

  gulp.task('qunit', function() {

  return gulp.src('./qunit/test-runner.html')
      .pipe(logCapture.start(console,'log'))
      .pipe(qunit())
      .pipe(logCapture.stop('build.xml'));
});

Piping the output into a write stream (which throws an error):

gulp.task('qunit', function() {

  return gulp.src('./qunit/test-runner.html')
      .pipe(qunit())
      .pipe(fs.createWriteStream('build.xml));
});

Using gulp-out plugin (which simply pipes the input html into the new file):

gulp.task('qunit', function() {

  return gulp.src('./qunit/test-runner.html')
      .pipe(qunit())
      .pipe(out('build.xml');
});

The XML is right there on the screen I just need to get it into a file somehow.


Solution

  • It turns out that phantom js takes node-like scripts that will run on execution. I basically took the run-qunit script from the examples directory of phantom-js and adjusted it to pipe console output into a build.xml file. Example script can be found here: https://github.com/ariya/phantomjs/blob/master/examples/run-qunit.js

    I simply adjusted the onConsoleMessage listener (ln48) like so:

    page.onConsoleMessage = function(msg) {        
        if( msg.search(/xml/) > -1 ) {
            fs.write('build.xml',msg);
        }
        console.log(msg);
    };
    

    To make this run as part of an automated build process, in Gulp I run the following task using the exec plugin.

    exec = require('child_process').exec;
    .
    .
    .
    gulp.task('phantom',function() {
        exec('phantomjs ./qunit/run-qunit.js ./qunit/test-runner.html');
    });
    

    Both of these adjustments successfully create a build.xml file that Atlassian Bamboo can read as part of its process.