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!
angular-mocks.js presumes that angular.js has been included as well.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With