Rom*_*aun 5 javascript unit-testing mocha.js node.js gulp
我正试着让我一起照顾我的单元测试,然后将我的测试覆盖率输出到一个.lcov文件中.
这是我到目前为止:
gulp.task('test', function () {
var test = fs.createWriteStream('./test.lcov', {flags: 'a'});
return gulp.src('./assets/js/test/test.js', {read: false})
.pipe(mocha({reporter: 'mocha-lcov-reporter'}))
.pipe(test);
});
Run Code Online (Sandbox Code Playgroud)
该mocha-lcov-reporter代码可以在这里找到:
https://github.com/StevenLooman/mocha-lcov-reporter/blob/master/lib/lcov.js
它通过输出结果 process.stdout.write()
但是,当我把它管道给WriteStream我时,我有以下错误:
TypeError: Invalid non-string/buffer chunk
at validChunk (_stream_writable.js:152:14)
at WriteStream.Writable.write (_stream_writable.js:181:12)
at Stream.ondata (stream.js:51:26)
at Stream.emit (events.js:95:17)
at drain (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:36:16)
at Stream.stream.queue.stream.push (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:45:5)
at Stream.stream (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/index.js:27:8)
at Stream.stream.write (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:26:11)
at write (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
at flow (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7)
Run Code Online (Sandbox Code Playgroud)
看起来 gulp-mocha 并没有完全设置为真正的直通流,事实上它看起来只是将源通过管道传输到 Mocha 实例中并让 Mocha 做它的事情。
我想到的第一件事就是在 bash 中做一个简单的重定向......
$ gulp test | grep -Ev "^\[[0-9:]{0,8}\]" > ./test.lcov
Run Code Online (Sandbox Code Playgroud)
当然,这假设所有与 gulp 相关的输出都将从[00:00:00](00当前系统时间)开始。如果不是这种情况,您最终可能会在文件的顶部和底部看到 gulp 输出。
如果您正在寻找更通用的答案(阅读:使用nodejs),您可以重写process.stdout.write. 这可能是:( 大多数人都这样做,但它会起作用。技巧是你不能覆盖process.stdout为另一个流,因为它在内部被写为 getter。但是你可以重写该stdout.write函数。事实上,我只是这样做了对于我正在从事的项目,这样我就可以查看其他开发人员的 gulp 日志,以防他们的构建系统出现问题。
我选择使用不太异步的解决方案,因为与 Nodejs 中的大多数其他内容不同,它们stdout都是stderr阻塞流,并且不像您习惯的异步代码那样运行。使用这种技术,你的任务最终会看起来像这样:
gulp.task('test', function () {
// clear out old coverage file
fs.writeFileSync('./test.lcov', '');
// if you still want to see output in the console
// you need a copy of the original write function
var ogWrite = process.stdout.write;
process.stdout.write = function( chunk ){
fs.appendFile( './test.lcov', chunk );
// this will write the output to the console
ogWrite.apply( this, arguments );
};
return gulp.src('./assets/js/test/test.js', {read: false})
.pipe(mocha({reporter: 'mocha-lcov-reporter'}));
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2143 次 |
| 最近记录: |