我正在尝试让 gulp 工作以帮助自动化一些单元测试。我有以下 gulp 文件。
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']);
当我运行“gulp unit”时,测试运行良好。
当我运行“gulp test”时,测试会运行,并且“watch”似乎正在运行。如果我对其中一个测试文件进行更改,则测试会正确重新运行,同时考虑到我在测试文件中所做的更改。
如果我对源文件进行更改,测试也会重新运行,但它们不会针对源文件的更新版本运行。
我的想法是不知何故,源文件正在被缓存,但我找不到任何其他似乎遇到此问题或找到解决方案的人。
感谢您帮助这个 Gulp/Node/Mocha 新手!
最佳答案
我遇到了同样的问题,但我找到了解决方法,
问题是当您通过 watch 运行测试时,nodejs 中的 require 正在缓存您的 src 文件
我在我的测试文件中使用了以下函数来使 src 文件的缓存无效,以代替 require。
显然这样做可能很危险,请参阅我帖子底部的链接以获取更多信息。仅供开发使用;)
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();
...
请参阅此处了解更多信息:node.js require() cache - possible to invalidate?
关于javascript - Gulp,Mocha,观看不重新加载我的源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291225/