14

I am attempting to get gulp working to help automate some unit testing. I have the following gulp file.

var gulp = require('gulp'),
    mocha = require('gulp-mocha');

gulp.task('unit', function() {
    return gulp.src('test/unit/**/*.js')
        .pipe(mocha({ reporter: 'spec' }))
        .on('error', handleError);
});

gulp.task('watch', function() {
    gulp.watch(['src/**/*.js', 'test/unit/**/*.js'], ['unit']);
});

gulp.task('test', ['unit', 'watch']);

When I run 'gulp unit', the tests run fine.

When I run 'gulp test', the tests run, and it appears that 'watch' is working. If I make a change to one of the test files, the tests rerun correctly, taking into account the changes I made in the test file.

If I make changes to my source files, the tests also re-run, but they DO NOT run against the updated version of the source file.

My thought is that somehow, the source file is being cached, but I cannot find any others who seem to have had this issue or find a solution.

Thanks for helping this Gulp/Node/Mocha newbie!

Kevin
  • 2,084
  • 3
  • 24
  • 36
  • Your setup looks just fine to me. Are you sure your tests are requiring the file which you've edited (and not a copy of it)? – Fabrício Matté Mar 10 '14 at 03:20
  • Yes. The tests are requiring the correct file. Remember, when I run "gulp unit" without "watch" it works great. When I run the tests using the mocha CLI, it also runs fine. It only appears to occur (the source file seemingly being cached) when the tests automatically rerun because "watch" saw a change in my source file. – Kevin Mar 11 '14 at 04:25
  • I've got the exact same problem (but jasmine, not mocha), any solution? – Karl Glaser Mar 27 '14 at 03:30
  • I have not yet found a solution. I am currently not using watch. Had to actually get some work done! :-) – Kevin Mar 28 '14 at 15:37
  • I just tried this and did not have any problems. I set up my `gulp.watch`, everything looks good, and then I deleted the contents of a src file, and those tests fail. Did I miss something? – Josh C. Jul 11 '14 at 20:00

2 Answers2

7

I had the same issue but i found a fix,

The issue is that require in nodejs is caching your src files when you are running your tests via watch

I used the following function in my test file to invalidate the cache of the src file, in replace of require.

Apparently doing this can be dangerous, please see the link at the bottom of my post for more information. Development use only ;)

Coffeescript - module-test.coffee

nocache = (module) ->
    delete require.cache[require.resolve(module)]
    return require(module)

Module = nocache("../module")
describe "Module Test Suite", () ->
    newModule = new Module();
    ...

Javascript - module-test.js

var Module, nocache;
nocache = function(module) {
    delete require.cache[require.resolve(module)];
    return require(module);
};
Module = nocache("../src/module");

describe("Module Test Suite", function () {
    newModule = new Module();
    ...

see here for more information: node.js require() cache - possible to invalidate?

Community
  • 1
  • 1
Azerothian
  • 386
  • 1
  • 6
  • 9
  • Thanks! I have also been thinking more about this. I have not tested this approach, but it looks valid to me. Also, I thought about invalidating all modules in a before all tests hook somehow. Have not tested that approach either. Either way, your answer will solve the issue. Thanks again! – Kevin Mar 31 '14 at 16:29
1

I didn't want to mod all my tests so I just jammed this function right before I start piping test files to mocha:

function freshFiles(chunk, enc, cb){
    _.forOwn(require.cache, function(value, key){
        if (key.indexOf('lib') !== -1 && key.indexOf('node_modules')===-1){
            delete require.cache[key];
        }
    });
}

This targets my lib folder but nothing in the node_modules path.

In gulp it looks like:

gulp.task('test', function () {
    var mocha = require("gulp-mocha");
    freshFiles();
    gulp.src(testSources)
        .pipe(mocha({ reporter: 'spec', growl: 'true' }))
        .on('error', gutil.log);

});
j03m
  • 5,195
  • 4
  • 46
  • 50