9

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();
});
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190

2 Answers2

9

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.

SoEzPz
  • 14,958
  • 8
  • 61
  • 64
  • Where is __dirname defined? It would seem to be undefined if i were to do it this way.. – Hcabnettek Aug 06 '15 at 19:43
  • Well I am using gulp, which is a Node app. Just adding __dirname, worked like a champ. Node must make it available to gulp. Thanks so much! – Hcabnettek Aug 06 '15 at 20:17
  • Good point about Gulp being installed by NODE. Would you say this has answered your question? – SoEzPz Aug 06 '15 at 20:35
3

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();
 });
Hugues Fontenelle
  • 5,275
  • 2
  • 29
  • 44