Skip to content Skip to sidebar Skip to footer

Angular Testing With Karma: "module Is Not Defined"

I know this question has been asked many times, and I know that in most cases people are missing the angular-mocks.js file. I'm running into the same issue, attempting to test a fa

Solution 1:

The message stating that module/angular is not defined means that your angular-mocks.js file is not being loaded, despite the fact you have it listed in your karma.conf.js file.

The problem you're experiencing is gulp-karma ignoring your karma.conf.js files array. This happens when you pass a string or glob into gulp.src in the gulpfile.

To work around this, pass gulp.src a string for a bogus file, "./foobar" for instance, and this will cause the files array in the karma.conf.js file to be used instead.

gulp.task('test', function () {
  gulp.src('./foobar')
    .pipe(karma({
      configFile: 'karma.conf.js',
      action: 'run'
    }));
});

Hope this helps!

Reference: https://github.com/lazd/gulp-karma/issues/9

Solution 2:

This is not the answer to your particular issue, but I had a similar symptom in a very similar scenario. However, the root cause was different, so I'll share it here in case others find this post and they have the same issue I had.

In my case, because I introduced tests later in the game, there was a mismatch between the versions of angular (1.4.7) and angular-mocks (1.6.9).

I found out this was the root cause by using the debug of Karma in the browser. Lowering the version of angular-mocks to match angular solved the issue.

Post a Comment for "Angular Testing With Karma: "module Is Not Defined""