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 factory on a module. Unfortunately, I keep running into issues with the tests (why, Angular, oh why must you assume a window and document object?), which state that module is not defined. I’m at a loss. I’ve also tried using angular.mocks.module, but then I get a message saying Angular isn’t defined. What am I doing wrong?

Of note, I’m using gulp as a task runner. My gulpfile (not yet perfect, tasks aren’t linked):

var gulp = require('gulp'),

    uglify = require('gulp-uglify'),
    jshint = require('gulp-jshint'),
    jasmine = require('gulp-jasmine'), 
    karma = require('gulp-karma'),

    paths = {
        scripts: "scripts/*.js",
        spec: "spec/*.js",
        dist: "dist"

gulp.task('prepare', function () {
    return gulp.src(paths.scripts)

gulp.task('test', function () {
    gulp.src([paths.scripts, paths.spec])
            configFile: 'karma.conf.js',
            action: 'run'

gulp.task('default', ['prepare', 'test']);

My karma.conf.js, generated by karma init:

// Karma configuration
// Generated on Fri Mar 14 2014 14:24:30 GMT-0400 (EDT)

module.exports = function(config) {

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',

    // frameworks to use
    // available frameworks:
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [

    // list of files to exclude
    exclude: [


    // preprocess matching files before serving them to the browser
    // available preprocessors:
    preprocessors: {


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters:
    reporters: ['progress'],

    // web server port
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,

    // start these browsers
    // available browser launchers:
    browsers: ['Chrome'],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false

And finally, my test suite (nothing’s set up yet, if I can pass this hurdle, we’ll be good):

/* Tests for memento.js. */

describe('memento core test suite', function () {
    var memento;


    beforeEach(function() {
        inject(function(_memento_) {
            memento = _memento_;

    // Check functions.
      // check to see if it has the expected function
    it('should match expected interface', function () { 

    it('should initialize', function () {'Test not implemented'));

    it('should push() a changed object', function () {'Test not implemented'));

    it('should not push() an unchanged object', function () {'Test not implemented'));

    it('should return original object on undo()', function () {'Test not implemented'));

    it('should return modified object on redo()', function () {'Test not implemented'));

    it('should not undo() if at beginning of stack', function () {'Test not implemented'));

    it('should not redo() if at end of stack', function () {'Test not implemented'));

    // TODO: Implement revert to original, clearing history.
    // it('should return seed object on revert()', function () {
    //'Test not implemented'));
    // });

    // it('should clear the stack on clear()', function () {
    //'Test not implemented'));
    // });

Does anyone see anything awry? I’m not sure if there’s something really obvious I’m missing – I could use an extra set of eyes, or many. I originally thought I’d be able to run this as a simple Jasmine test suite without Karma, but due to Angular, that has problems. If I can’t get this to work, I might just use npm’s Angular package and alter my Angular module so that it supports CommonJS…

Read More:   Is there a jQuery selector to get all elements that can get focus?

Thanks, everyone! Hope I’m not crazy.

Edit: I’ve included my devdependencies.

  "devDependencies": {
    "gulp": "~3.5.6",
    "gulp-uglify": "~0.2.1",
    "gulp-jshint": "~1.5.0",
    "gulp-jasmine": "~0.2.0",
    "angular": "~1.2.10",
    "karma": "~0.12.0",
    "gulp-karma": "0.0.4",
    "karma-jasmine": "~0.2.2",
    "karma-chrome-launcher": "~0.1.2"

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 () {
      configFile: 'karma.conf.js',
      action: 'run'

Hope this helps!


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.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts