Currently I am using gulp-karma for unit-testing in JS, but the current version is deprecated. How can I replace this code so I can use the new version recommended?
Old version: var karma = require('gulp-karma');
var testFiles = [
'client/todo.js',
'client/todo.util.js',
];
gulp.task('test', function() {
// Be sure to return the stream
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
});
});
New recommended version (but not using pipes):
var gulp = require('gulp');
var karma = require('karma').server;
gulp.task('tests', function(done) {
return karma.start({
configFile: __dirname + '/test/karma.conf.js',
singleRun: true
}, done);
});
I specify I need to use pipes, but I always get some errors.
I've done a quick research, so I'm not 100% sure that this is going to work, but Id give it a try.
Requirements
* Promises support (native or via a third party library)
* stream-from-promise => npm install stream-from-promise --save-dev
First wrap your karma server into a promise (some third party libraries also offer a safer promisify
fn for this):
function startKarma(){
return new Promise( function(resolve, reject){
karma.start({
configFile: __dirname + '/test/karma.conf.js',
singleRun: true
}, function(error){
return error ? reject(error) : resolve();
});
}
Now create a function to create a stream out of a promise:
var StreamFromPromise = require("stream-from-promise");
function makeStream( promise ){
return StreamFromPromise.obj( promise );
});
And now let's write your task:
gulp.task('tests', function() {
return gulp.src(testFiles)
.pipe( makeStream( startKarma() ))
.on('error', function(err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
});
});
var gulp = require('gulp'); var karma = require('karma').server; var testFiles = [ 'client/todo.js', 'client/todo.util.js', ]; gulp.task('tests', function(done) { return karma.start({ configFile: __dirname + '/test/karma.conf.js', singleRun: true, files: testFiles }, done); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.