Question

I'm trying to make my tests run each time I'm saving some files. Here is the gulp watch:

gulp.task('jasmine', function() {
 gulp.src('spec/nodejs/*Spec.js')
  .pipe(jasmine({verbose:true, includeStackTrace: true}));
});
gulp.task('watch', function () {
  gulp.watch(['app/*.js', 'app/!(embed)**/*.js','spec/nodejs/*.js'], ['jasmine']);
});

To test for example app/maps.js I'm creating a spec/nodejs/mapsSpec.js file like this:

'use strict';
var maps = require('../../app/maps');
describe('/maps related routes', function(){
  it('should ...', function(){...}
...

If I change a spec file everything is working well, if I modify app/maps.js file the change trigger the test. if I modify it again tests are tiggered but the modifications do not taking effect. For example if I add a console.log('foo') in a second time, I will not see it until I relaunch gulp watch and save it again. So only one run of jasmine is ok when using it with gulp.watch.

I guess it's because require is cached by nodejs in the gulp process. So how should I do ?

Was it helpful?

Solution

I took a look at the code of gulp-jasmine. The problem is that the only file from the cache is the Specs.js file. The cache of the children(the reqquired files to test) aren't cleared.

Within the index.js of gulp-jasmine is a row which deletes the cache:

delete require.cache[require.resolve(path.resolve(file.path))];

If you put the next block of code before the delete, you will delete all the children's cache and will it run correctly after every time you save your file.

var files = require.cache[require.resolve(path.resolve(file.path))];
if( typeof files !== 'undefined' ) {
    for( var i in files.children ) {
        delete require.cache[ files.children[i].id ];
    }
}

You can change this in the node_modules. I will go for a pull request, so maybe in the near future this will be solved permanently.

Also wrote a post about it on: http://navelpluisje.nl/entry/fix-cache-problem-jasmine-tests-with-gulp

OTHER TIPS

I haven't found a fix for this issue, but you can work around it via the gulp-shell task.

npm install gulp-shell --save-dev

then

var shell = require('gulp-shell');
...
gulp.task('jasmine', function() {
 gulp.src('spec/nodejs/*Spec.js')
  .pipe(shell('minijasminenode spec/*Spec.js'));
});

You'll also need jasmine installed as a direct dependency (gulp-jasmine uses minijasminenode)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top