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

Code Coverage reported as 0% after upgrading from 3.0.1 to 4.0.1 #159

Open
ek9 opened this issue Feb 26, 2017 · 14 comments
Open

Code Coverage reported as 0% after upgrading from 3.0.1 to 4.0.1 #159

ek9 opened this issue Feb 26, 2017 · 14 comments

Comments

@ek9
Copy link

ek9 commented Feb 26, 2017

I am experiencing a weird issue where upgrading gulp-mocha from 3.0.1 to 4.0.1 causes code coverage to no longer work. The tests run as before, but istanbul is no longer able to pick things up and it reports 0% code coverage.

This affects this PR https://github.com/ek9/generator-license-cc/pull/25/files

Test coverage before update ( https://travis-ci.org/ek9/generator-license-cc/jobs/195271001 ):

  21 passing (1s)

-----------|----------|----------|----------|----------|----------------|

File       |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |

-----------|----------|----------|----------|----------|----------------|

 app/      |      100 |     93.1 |      100 |      100 |                |

  index.js |      100 |     93.1 |      100 |      100 |                |

-----------|----------|----------|----------|----------|----------------|

All files  |      100 |     93.1 |      100 |      100 |                |

-----------|----------|----------|----------|----------|----------------|

Test coverage after update ( https://travis-ci.org/ek9/generator-license-cc/jobs/205308602 ):

  21 passing (950ms)

-----------|----------|----------|----------|----------|----------------|

File       |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |

-----------|----------|----------|----------|----------|----------------|

 app/      |        0 |        0 |        0 |        0 |                |

  index.js |        0 |        0 |        0 |        0 |... 294,295,297 |

-----------|----------|----------|----------|----------|----------------|

All files  |        0 |        0 |        0 |        0 |                |

-----------|----------|----------|----------|----------|----------------|

gulpfile.js

'use strict';
var path = require('path');
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var excludeGitignore = require('gulp-exclude-gitignore');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var nsp = require('gulp-nsp');
var plumber = require('gulp-plumber');
var coveralls = require('gulp-coveralls');

// ... some parts omitted ...

gulp.task('pre-test', function () {
  return gulp.src('app/*.js')
    .pipe(excludeGitignore())
    .pipe(istanbul({
      includeUntested: true
    }))
    .pipe(istanbul.hookRequire());
});

gulp.task('test', ['pre-test'], function (cb) {
  var mochaErr;

  gulp.src('test/**/*.js')
    .pipe(plumber())
    .pipe(mocha({reporter: 'spec'}))
    .on('error', function (err) {
      mochaErr = err;
    })
    .pipe(istanbul.writeReports())
    .on('end', function () {
      cb(mochaErr);
    });
});

For full source you can check the PR or repository ek9/generator-license-cc#25 (comment)

@ek9
Copy link
Author

ek9 commented Feb 26, 2017

I tried to have a look on what is affecting this, but since it was a major rewrite, I don't think I can pin-down where the problem is without further assistance.

@ek9 ek9 changed the title Error running tests after upgrading from 3.0.1 to 4.0.1 Code Coverage reported as 0% after upgrading from 3.0.1 to 4.0.1 Feb 26, 2017
@agracio
Copy link

agracio commented Feb 27, 2017

Just tested it in my project and coverage is also 0% on 4.0.1

gulpfile.js snippet:

gulp.task('pre-coverage',  function () {
        return gulp.src(paths.out + '/src/progress.js')
            .pipe(istanbul({includeUntested: true}))
            .pipe(istanbul.hookRequire());
});

gulp.task('istanbul', ['pre-coverage'], function () {
        return gulp.src(paths.test, {read: false})
            .pipe(mocha({reporter: 'spec'}))
            .pipe(istanbul.writeReports({
                dir: paths.coverage,
                reporters: [ 'json' ],
                reportOpts: { dir: paths.coverage}
            }));
});

@skarfacegc
Copy link

I think the issue is that gulp-mocha is now acting on files rather than on the stream from the istanbul pipeline. I think this was the tradeoff for making compilers work. :/

You can probably use instanbul to instrument your src into a tmp directory and run the tests against those.

@sindresorhus
Copy link
Owner

It always acted on the file paths, not the files object. Nothing has changed in that regard.

@chris-jones-pixitmedia
Copy link

I'm experiencing the same issue after updating

@zozzz
Copy link

zozzz commented Mar 18, 2017

The problem is that spawning mocha instead use of mocha api. The gulp-istanbul plugin patch require function to import patched sources. If spawn mocha process then the patched require function is now longer available in the new process.

I think a good solution is make a option that switch between spawn and api.

mischah added a commit to micromata/generator-http-fake-backend that referenced this issue Apr 2, 2017
Can’t update gulp-mocha to 4.x as long as the issue regarding coverage isn’t fixed:
sindresorhus/gulp-mocha#159
mischah added a commit to micromata/generator-http-fake-backend that referenced this issue Apr 2, 2017
Can’t update gulp-mocha to 4.x as long as the issue regarding coverage isn’t fixed:
sindresorhus/gulp-mocha#159
@corvinrok
Copy link

Will this ever be solved by a fix in the package?? If not, what are the steps we can take in our own test implementations to work with the new package?

@tinesoft
Copy link

Any update on this?

@Enet4
Copy link

Enet4 commented May 29, 2017

Sounds unlikely. The discussions suggest that there isn't a trivial fix. Consider using gulp-mocha 3 in the mean time.

tinesoft added a commit to tinesoft/generator-ngx-library that referenced this issue May 29, 2017
@edj-boston
Copy link

I would like gulp-mocha to restore compatibility with gulp-istanbul as well.

@jamesplease
Copy link

jamesplease commented Jul 6, 2017

Looping in @shellscape , who was the primary author of the v4 updates. @shellscape , do you have any thoughts on how this could be fixed?

It seems like v4 swapped one big problem with another big problem. From the changelog:

Now spawns the mocha binary instead using its broken programmatic API.

another option would have been to update the mocha programmatic API so as to avoid the ecosystem issues.


If someone wanted to fix this, I'd recommend doing two things:

  1. Fixing the upstream issue with Mocha
  2. Reverting the changes made in v4 of this library

For now, my plan is to stick to v3 of this lib, but I also have a longer-term goal to switch to Jest. It doesn't seem to make much sense to rely on a patchwork of libs and plugins by many different authors to get basic testing functionality. Jest provides it all out of the box, so the n > 5 libs required to get mocha up and running with gulp (with code coverage) is reduced to 2 with Jest (Jest and gulp-jest)

@shellscape
Copy link
Contributor

pong. I'm not really involved here much anymore, but I'm happy to chime in and then disappear in a puff of smoke...

Switching to using the binary was precisely to avoid upstream mocha problems. I can't recall the issue and I haven't the chance to search for the source right now, but the mocha project itself recommended not using the internal API. Unless @sindresorhus has had a change of heart (and I'd doubt it given the number of issues that change resolved) it's unlikely that the module would be reverted to once again use the internal mocha API.

You might try running mocha, istanbul, and coveralls via command line to test that it's not something else, somewhere else in your gulp pipeline that's causing the issue. sounds like @zozzz pinpointed the issue with the gulp pipeline. Perhaps the blame lies with gulp-istanbul for making assumptions. You might get more traction with that project on a proper fix.

@jmeas I'm not really sure what the purpose of pushing hard for Jest here is - it comes across in poor taste.

@jamesplease
Copy link

jamesplease commented Jul 6, 2017

Thanks for the response, @shellscape !

but the mocha project itself recommended not using the internal API.

Ah, okay. I didn't know this. My suggestion for how to solve this probably wasn't any good, then.

Perhaps the blame lies with gulp-istanbul for making assumptions. You might get more traction with that project on a proper fix.

This a good suggestion. I looked more into that lib, that it's using the "old" programmatic istanbul API. I'm not too sure, but maybe upgrading it to use the new istanbul-api could help? The creator of gulp-istanbul, @SBoudrias , said he'd be willing to merge in a PR to update to that version if anyone has the time.

@jmeas I'm not really sure what the purpose of pushing hard for Jest here is - it comes across in poor taste.

The purpose was to help other devs get unblocked by this issue. Right now it's not possible to use mocha+istanbul+gulp for coverage without investing a lot of time in the tooling ecosystem, given that many of these libs aren't actively maintained by their creators. I think Jest (or, really, any solution that bundles the integrations for you) is worth consideration for devs running into this specific problem.

There are a bunch of other solutions that might make sense for devs, too. You could just use the nyc CLI directly, and skip the gulp ecosystem. Or maybe the Ava/gulp ecosystem works well, too. Or maybe you would choose to invest time to get all of these mocha/coverage/gulp libs working together. I'm not sure – I'm just throwing some options out there for devs who come to this issue.

Thanks again, @shellscape !

mischah added a commit to micromata/generator-http-fake-backend that referenced this issue Jul 19, 2017
Because we would loose our coverage with gulp-mocha 4.x because of sindresorhus/gulp-mocha#159
jlouns added a commit to jlouns/gulp-requirejs-optimize that referenced this issue Mar 24, 2018
Unable to update past 3.0.1 due to change in gulp-mocha operation.
see SBoudrias/gulp-istanbul#115 and sindresorhus/gulp-mocha#159.
Treeless added a commit to Treeless/microservice-base that referenced this issue Dec 22, 2018
@miparnisari
Copy link
Contributor

miparnisari commented Apr 13, 2020

Hey guys, what is the recommended way of getting code coverage after running gulp-mocha? gulp-istanbul doesn't work, gulp-cover doesn't either (just tried it and failed)

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