SBoudrias / gulp-istanbul

Istanbul unit test coverage plugin for gulp.
MIT License
186 stars 87 forks source link

Code coverage is empty when using Qunit #133

Closed MhdTlb closed 6 years ago

MhdTlb commented 6 years ago

I'm trying to generate code coverage for QUnit tests. gulpfile.js has the following tasks:

var istanbul = require('gulp-istanbul');
var qunit= require('gulp-qunit');
gulp.task('pre-test', function () {
    return gulp.src(['scripts/**/*.js', 'js/**/*.js'])
        // Covering files
        .pipe(istanbul())
        // Force `require` to return covered files
        .pipe(istanbul.hookRequire());
});

gulp.task('test', ['pre-test'], function () {
    return gulp.src(['test/unittests/index.html'])
        .pipe(qunit())
        // Creating the reports after tests ran
        .pipe(istanbul.writeReports(
            {
                dir: './coverage',
                reporters: ['lcov', 'json', 'text', 'text-summary'],
                reportOpts: { dir: './coverage' }
            }));
        // Enforce a coverage of at least 90%
        //.pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } }));
});

index.html contains references to qunit and all my javascript test files.

So after running cmd gulp test, I will get an empty report(All files 100%):


[15:36:30] Starting 'pre-test'...
[15:36:30] Finished 'pre-test' after 916 ms
[15:36:30] Starting 'test'...
[15:36:30] Testing index.html
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|

=============================== Coverage summary ===============================
Statements   : 100% ( 0/0 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 0/0 )
================================================================================
[15:36:31] Finished 'test' after 61 ms
// executing tests start here after code coverage report.

[15:36:33] gulp-qunit: ✔ QUnit assertions all passed in index.html

The weired thing, in the CMD output, is that executing unit test started after returning Coverage summary (not before as I was expecting) and all unit tests passed.

Is there a specific setup to use QUnit Html file to generate code coverage?

SBoudrias commented 6 years ago

gulp-istanbul only instrument the files in the current node context. This means it doesn't work in 2 specifics cases:

  1. When test is spawn inside a new process
  2. If your tests doesn't run in node (like in this case I assume qunit is using a browser or phantomjs to run the index.html file)
MhdTlb commented 6 years ago

Thanks @SBoudrias for the quick response. Is there any work around or other plugin that I can use for this scenario?

SBoudrias commented 6 years ago

@MhdTlb I'd recommend you to use a more modern testing tool with built-in coverage. My personal go to is Jest; and Ava is also pretty good.