4

My team is working on moving from Grunt to Gulp. I have a Grunt test task that works fine, but when I try to run the tests (using gulp-karma) I get an error that says "ReferenceError: Can't find variable: module"

I Googled and found a lot of posts saying to check the location of my angular-mocks.js file, and it's in the correct space (my Grunt task for the same code does work).

To verify that it wasn't something weird in my code a spun up a new yo angular app and was able to replicate the error.

My guess is that I'm missing a configuration value or a step or something. Any advice?

Thanks.

James Bender
  • 1,175
  • 1
  • 11
  • 22

2 Answers2

6

The answer by @mcranston18 will absolutely work. But as an alternative option, gulp-karma is currently an unnecessary plugin. Part of the advantage of gulp is that you don't have to use a plugin for everything under the sun. Instead, you can wire into karma directly.

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);
});
Jay Harris
  • 9,935
  • 1
  • 26
  • 27
5

When you are running gulp tests, gulp-karma is looking for files within gulpfile.js, not karma.conf.js as you have defined. Remember, when you return gulp.src(tests), you are passing in the array of files needed for the tests to pass.

To fix your problem, in your gulpfile.js, update your tests array to include your bower files, app files, and test files:

var tests = [
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'bower_components/angular-animate/angular-animate.js',
  'bower_components/angular-cookies/angular-cookies.js',
  'bower_components/angular-resource/angular-resource.js',
  'bower_components/angular-route/angular-route.js',
  'bower_components/angular-sanitize/angular-sanitize.js',
  'bower_components/angular-touch/angular-touch.js',
  'app/scripts/app.js',
  'app/scripts/**/*.js',
  'test/mock/**/*.js',
  'test/spec/**/*.js'
];

You can delete the files array in karma.conf.js as they are not needed.

(p.s. I also made a reference to app.js as app/scripts/**/*.js would not have matched app.js which needs to be loaded in first)

mcranston18
  • 4,680
  • 3
  • 36
  • 37
  • 1
    This is also noted in [gulp-karma#27](https://github.com/lazd/gulp-karma/issues/27) and is one of the primary issues of `gulp-karma`. They are working on potential resolutions. – Jay Harris Dec 08 '14 at 20:23
  • 1
    BTW, If you wanted your files defined in the karma.conf.js, you could just pass in an empty array to gulp.src (i.e. `return gulp.src([])` ) – mcranston18 Dec 08 '14 at 20:26