tou*_*tpt 5 node.js jasmine-node gulp
每次我保存一些文件时,我都会尝试运行测试.这是gulp手表:
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']);
});
Run Code Online (Sandbox Code Playgroud)
要测试例如app/maps.js我正在创建一个spec/nodejs/mapsSpec.js文件,如下所示:
'use strict';
var maps = require('../../app/maps');
describe('/maps related routes', function(){
it('should ...', function(){...}
...
Run Code Online (Sandbox Code Playgroud)
如果我更改spec文件一切正常,如果我修改app/maps.js文件,则更改会触发测试.如果我再次修改它,测试会被触发,但修改不会生效.例如,如果我在第二次添加console.log('foo'),我将不会看到它,直到我重新启动gulp watch并再次保存它.因此,与gulp.watch一起使用时,只有一次茉莉花可以使用.
我想这是因为需要在gulp进程中由nodejs缓存.那我该怎么办?
我看了一下gulp-jasmine的代码。问题是缓存中的唯一文件是 Specs.js 文件。子项的缓存(测试所需的文件)未清除。
在 gulp-jasmine 的 index.js 中有一行删除缓存:
delete require.cache[require.resolve(path.resolve(file.path))];
Run Code Online (Sandbox Code Playgroud)
如果您将下一个代码块放在删除之前,您将删除所有子级缓存,并且每次保存文件后它都会正确运行。
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 ];
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在node_modules 中更改此设置。我将提出拉取请求,所以也许在不久的将来这个问题将得到永久解决。
还写了一篇关于它的文章:http://navelpluisje.nl/entry/fix-cache-problem-jasmine-tests-with-gulp
| 归档时间: |
|
| 查看次数: |
3939 次 |
| 最近记录: |