Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get gulp-istanbul working with this? #10

Closed
blah238 opened this issue Sep 17, 2014 · 13 comments
Closed

How to get gulp-istanbul working with this? #10

blah238 opened this issue Sep 17, 2014 · 13 comments

Comments

@blah238
Copy link
Contributor

blah238 commented Sep 17, 2014

I am able to use gulp-istanbul with gulp-mocha, but with gulp-spawn-mocha I get no coverage output.

var gulp = require('gulp');
var istanbul = require('gulp-istanbul');
var mocha = require('gulp-spawn-mocha');

gulp.task('coverage', function (cb) {
  gulp.src(['api/**/*.js'])
    .pipe(istanbul())
    .on('finish', function () {
      gulp.src(['test/**/*.spec.js'], {
        read: false
      })
        .pipe(mocha({
          timeout: 20000
        }))
        .pipe(istanbul.writeReports())
        .on('end', cb);
    });
});
Output:
----------|-----------|-----------|-----------|-----------|
File      |   % Stmts |% Branches |   % Funcs |   % 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 )
================================================================================

What is the correct way to use gulp-istanbul (or Istanbul itself) with this?

@knpwrs
Copy link
Owner

knpwrs commented Sep 17, 2014

I don't believe the istanbul plugin will actually work like this with gulp-spawn-mocha. I actually got it working earlier in the year but never got around to releasing an implementation. I'll have to look back into it.

@knpwrs
Copy link
Owner

knpwrs commented Sep 17, 2014

I got a quick and dirty version running at the quick-istanbul branch. I'll try to push this out as soon as I can clean it up and write proper tests (and merge with with other open conflicts).

Sample output (from running gulp test on this very repo on the quick-coverage branch):

[22:23:58] Warning: gulp version mismatch:
[22:23:58] Global gulp is 3.8.7
[22:23:58] Local gulp is 3.2.5
[22:23:58] Using gulpfile ~/Workspace/JavaScript/gulp-spawn-mocha/gulpfile.js
[22:23:58] Starting 'test'...

�[0m�[0m
�[0m  gulp-spawn-mocha tests�[0m

  �[32m  ✓�[0m�[90m should buffer filenames and pass them to mocha �[0m

  �[32m  ✓�[0m�[90m should default to proper binary �[0m

  �[32m  ✓�[0m�[90m should allow for a custom mocha binary �[0m

  �[32m  ✓�[0m�[90m should allow for a custom environment �[0m

  �[32m  ✓�[0m�[90m should pass arguments to mocha �[0m

  �[32m  ✓�[0m�[90m should only pass string or number values of arguments to mocha �[0m

  �[32m  ✓�[0m�[90m should handle errors from mocha �[0m


�[92m �[0m�[32m 7 passing�[0m�[90m (58ms)�[0m


=============================== Coverage summary ===============================
Statements   : 94.12% ( 32/34 )
Branches     : 85% ( 17/20 )
Functions    : 100% ( 8/8 )
Lines        : 94.12% ( 32/34 )
================================================================================
[22:23:59] Finished 'test' after 1.05 s

It also wrote out coverage information to the coverage folder, so overall this is fairly promising.

@knpwrs
Copy link
Owner

knpwrs commented Sep 18, 2014

I just published version 0.4.0 with built-in support for Istanbul. See README.md for more information.

@knpwrs knpwrs closed this as completed Sep 18, 2014
@blah238
Copy link
Contributor Author

blah238 commented Sep 18, 2014

@KenPowers Thanks! I tried this out and while this does seem to be working on Windows (yay!), I think I still prefer the gulp-istanbul + gulp-mocha plugins for coverage, because I can tell it which files to instrument, rather than just whatever files are required during the tests (which is what istanbul cover -- _mocha does). This also helps to find files with no coverage, an issue gulp-istanbul recently resolved: SBoudrias/gulp-istanbul#32

Do you think that would be something this module could support or is that not possible?

@knpwrs
Copy link
Owner

knpwrs commented Sep 18, 2014

Unfortunately your best bet would probably be to follow the advice in gotwarlost/istanbul/issues/112. You could jerry-rig something up to recursively require all files in your lib folder. See this bluebird example of how to read directories recursively using promises and then you can require all .js files. This code could go in a file like test/setup.js which I require into mocha using the -r option.

@knpwrs
Copy link
Owner

knpwrs commented Sep 18, 2014

You could probably also read the coverage data in another task and generate a list of files with 0% coverage.

Now that I think about it I may be able to support this, but probably not today (busy busy busy). I'll look into it in a few days.

@knpwrs knpwrs reopened this Sep 18, 2014
@knpwrs
Copy link
Owner

knpwrs commented Sep 18, 2014

Alternatively I can look in to supporting something through baseline: https://github.com/gotwarlost/istanbul/search?utf8=%E2%9C%93&q=baseline

@blah238
Copy link
Contributor Author

blah238 commented Sep 18, 2014

Yeah the problem with the require jerryrigging approach is that it still reports on things that don't need to be reported on, like config files and such outside my API folder. I just want to cover the files in my API folder, not everything that is touched by tests.

@knpwrs
Copy link
Owner

knpwrs commented Sep 18, 2014

That's where the -x option would come in handy with Istanbul:

      -x <exclude-pattern> [-x <exclude-pattern>]
              one or more fileset patterns e.g. "**/vendor/**"

But I'm going to be looking into baseline files anyway.

@rudijs
Copy link

rudijs commented Sep 19, 2014

Hi,

Following the suggestions for jerryrigging require all, I went these this three lines of code.

Uses the require-walk module

'use strict';

/*
Require all .js files (synchronous) for code coverage
 */

var requireWalk = require('require-walk');

requireWalk(__dirname)();

I only want all the files in my app/ directory to be covered so I put that code into app/setup.spec.js

Cheers.

@knpwrs
Copy link
Owner

knpwrs commented Sep 19, 2014

Nice. I'm still going to leave this issue open as I investigate baseline functionality in Istanbul.

@jreading
Copy link

jreading commented Nov 4, 2015

Any word on this? Seems like Istanbul is working fine.

@knpwrs
Copy link
Owner

knpwrs commented Nov 5, 2015

It works but files that are never required won't show up in your coverage report. It may be desirable that if a file is not required in your tests that it shows up in the report with zero coverage.

@knpwrs knpwrs closed this as completed Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants