admin管理员组文章数量:1296298
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!
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!
Share Improve this question asked Mar 10, 2014 at 2:41 KevinKevin 2,1543 gold badges24 silver badges37 bronze badges 5- 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é Commented Mar 10, 2014 at 3: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 Commented Mar 11, 2014 at 4:25
- I've got the exact same problem (but jasmine, not mocha), any solution? – Karl Glaser Commented Mar 27, 2014 at 3:30
- I have not yet found a solution. I am currently not using watch. Had to actually get some work done! :-) – Kevin Commented Mar 28, 2014 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. Commented Jul 11, 2014 at 20:00
2 Answers
Reset to default 7I 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?
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);
});
本文标签: javascriptGulpmochaWatch Not Reloading My Source FilesStack Overflow
版权声明:本文标题:javascript - Gulp, Mocha, Watch Not Reloading My Source Files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741633033a2389477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论