Karma + angular-mocks TypeError: 'undefined' is not an object (evaluating 'angular.mock = {}')
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)
like @jlew said, angular-mocks expects angular on the window
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.
Comments
-
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 about 9 yearsI stumbled around this as well. If you're using browserify make sure you require your bundle before you require angular-mocks.
-
Stephane about 9 yearsI 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 over 7 yearsIt was a missing reference file in "files" in the karma configuration file for me.
-
marceloemanoel almost 7 yearsalmost 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";