gulp task can't find karma.conf.js

2019-04-22 11:24发布

I am trying to run karma test via a gulp task. I use https://github.com/karma-runner/gulp-karma and for some reason gulp cannot locate my karma.conf.js. That file is located in the same folder as the gulpfile. The root of the project. No matter what path I put, I get the same error File ./karma.conf.js does not exist. I cannot figure out how to path it correctly. Here is the code for the gulp task.

gulp.task('tdd', function (done) {
  new Server({
    configFile: 'karma.conf.js'
  }, done).start();
});

2条回答
你好瞎i
2楼-- · 2019-04-22 11:35

This is how I spool up karma using Gulp ( and both files are in the same root ).

var karma = require('karma');

gulp.task('karma', function (done) {
  karma.server.start({
    configFile: __dirname + '/karma.conf.js'
  }, done);
});

UPDATE

If you are running NODE.js then

NODE Explnation for __dirname link

"The name of the directory that the currently executing script resides in."

If you are not running NODE.js, then perhaps all you needed was

configFile: '/karma.conf.js'

But if you are running NODE then use the first example.

查看更多
爷的心禁止访问
3楼-- · 2019-04-22 11:48

If another destination directory is assigned to __dirname, then it doesn't work. Try this:

var Server = require('karma').Server
gulp.task('test', function (done) {
   new Server({
     configFile: require('path').resolve('karma.conf.js'),
     singleRun: true
   }, done).start();
 });
查看更多
登录 后发表回答