mardi 5 juillet 2016

no window.__coverage__ object is created by istanbul + phantomJS

I'm trying to use the following stack for my client side testing, however, it seems like istanbul, aside from generating spec files, doesn't do much.

I've tried following various examples from:

here, here and here

however, no matter what I do, istanbul doesn't seem to create an in memory reference of the window.__coverage__ object and I'm unable to parse the output due to this.

The pre-test task shown under the gulpfile.js, does generate some funky instrumented files, which doesn't seem to do or go anywhere.

Please advise

task configuration

  • gulp
  • gulp-mocha-phantomjs
  • gulp-istanbul

client side

  • sinon
  • mocha

gulpfile.js

gulp.task('pre-test', function () {
    return gulp.src(['tests/ar.config.js'])
    // Covering files
        .pipe(istanbul())
        // Write the covered files to a temporary directory
        .pipe(gulp.dest('coverage/'));
});

gulp.task('test', ['inject', 'pre-test'], function () { return gulp .src('index.html', {read: false}) .pipe(mochaPhantomJS( { reporter: 'spec', phantomjs: { hooks: 'mocha-phantomjs-istanbul', coverageFile: './coverage/coverage.json' } })) .pipe(istanbul.writeReports()) });

mocha-phantomjs-istanbul

var system = require('system');
var fs = require('fs');

function collectCoverage(page) {
    // istanbul stores coverage in global.__coverage__
    var coverage = page.evaluate(function () {
        return window.__coverage__;
    });

    // fail gracefully when we don't have coverage
    if (!coverage) {
        console.log("no coverage found!")
        return;
    }

    // read coverageFile from mocha-phantomjs args
    var phantomOpts = JSON.parse(system.args[system.args.length - 1]);
    var coverageFile = phantomOpts.coverageFile || 'coverage/coverage.json';

    // write coverage to file
    var json = JSON.stringify(coverage);
    fs.write(coverageFile, json);
}

// beforeStart and afterEnd hooks for mocha-phantomjs
module.exports = {
    afterEnd: function (runner) {
        collectCoverage(runner.page);
    }
};

output

...
✓ test x
✓ test y
✓ test z

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|


=============================== Coverage summary ===============================
Statements   : 100% ( 0/0 )

Aucun commentaire:

Enregistrer un commentaire