Karma + angular-mocks TypeError: 'undefined' is not an object (evaluating 'angular.mock = {}')

26,441

Solution 1

You would need to include angular.js as well, then only angular.mocks will work else window.angular will be undefined.

 files: [ 
            'dom_munger.data.appjs',
            'path/to/angular.js', //<-- include angularjs
            'bower_components/angular-mocks/angular-mocks.js',
            'tests/spec/*.js'
        ],

Solution 2

update for 2017 and webpack 2 (but same angular<2)

  1. like @jlew said, angular-mocks expects angular on the window

  2. module reference is being replaces by webpack

So in tests you need an header like:

import angular from 'angular';
import 'angular-mocks/ngMock';

and replace each global module reference with angular.mock.module

Solution 3

angular-mocks.js presumes that angular.js has been included as well.

Share:
26,441
Daniel Bogart
Author by

Daniel Bogart

Learning to code.

Updated on June 14, 2020

Comments

  • Daniel Bogart
    Daniel Bogart almost 4 years

    I'm trying to write unit tests using Karma + Jasmine, but running into an error with angular-mocks. When running grunt test I get the following error:

    PhantomJS 1.9.8 (Mac OS X) ERROR TypeError: 'undefined' is not an object (evaluating 'angular.mock = {}') at /Users/danielbogart/Documents/coding/work/AexNav/bower_components/angular-mocks/angular->mocks.js:17 Chrome 39.0.2171 (Mac OS X 10.9.4) ERROR Uncaught TypeError: Cannot set property 'mock' of undefined at /Users/danielbogart/Documents/coding/work/AexNav/bower_components/angular-mocks/angular->mocks.js:17

    Gruntfile karma config:

        karma: {
            options: {
                frameworks: ['jasmine'],
                files: [ 
                    'dom_munger.data.appjs',
                    'tests/spec/*.js',
                    'bower_components/angular-mocks/angular-mocks.js'
                ],
                logLevel: 'ERROR',
                reporters: ['mocha'],
                autoWatch: false, //watching is handled by grunt-contrib-watch
                singleRun: true
            },
            all_tests: {
                browsers: ['PhantomJS', 'Chrome']
            },
            during_watch: {
                browsers: ['PhantomJS']
            }
        }
    

    Thanks!

  • Admin
    Admin about 9 years
    I stumbled around this as well. If you're using browserify make sure you require your bundle before you require angular-mocks.
  • Stephane
    Stephane about 9 years
    I guess what jlew meant is that the angular.js needs to be included before the angular-mocks.js is included like: files: [ 'bower_components/angular/angular.js', 'bower_components/angular-mocks/angular-mocks.js',
  • Steven Rogers
    Steven Rogers over 7 years
    It was a missing reference file in "files" in the karma configuration file for me.
  • marceloemanoel
    marceloemanoel almost 7 years
    almost worked here on my setup, for me webpack says that module "angular" has no default export. I solved using like: import * as angular from "angular"; import "angular-mocks/ngMock";