12

I am trying to create the Jasmine unit tests described in "Angular.js in Action". The app runs properly but I keep getting this error in the node.js command prompt when trying to run my test.

My config:

module.exports = function(config) {
config.set({

// base path, that will be used to resolve files and exclude
basePath: '',


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


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

  'javascripts/angular.min.js',
  'javascripts/angular-mocks.js',
  'javascripts/app.js',
  'tests/angelloModelSpec.js',
  ],

My index.html header:

<head>
    <script src="javascripts/angular.min.js" type="text/javascript"></script>
    <script src="javascripts/angular-mocks.js"></script>
    <script src="javascripts/app.js"></script>


    <meta charset="utf-8">
    <title>Angello</title>
    <meta name="description" content="Angello Story Application">
    <link rel="stylesheet" href="app.css">
</head>

My test:

describe('Service: angelloModel', function(){

// load the service's module
beforeEach(module('Angello'));

var modelService;

// Initialize the service
beforeEach(inject(function (angelloModel){
    modelService = angelloModel;
}));

describe('#getStatuses', function(){
    it('should return seven different statuses', function () {
        expect(modelService.getStatuses().length).toBe(7);
    });
});
});

output in command prompt:

Your environment has been set up for using Node.js 0.10.26 (x64) and npm.

Your environment has been set up for using Node.js 0.10.26 (x64) and npm.

C:\Users\jmclaughlin>karma start karma.conf.js
INFO [karma]: Karma v0.10.9 server started at http://localhost:****/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Windows 7)]: Connected on socket ***************
Chrome 33.0.1750 (Windows 7) ERROR
    Uncaught TypeError: Cannot set property 'mock' of undefined
    at C:/Angello/javascripts/angular-mocks.js:17
Chrome 33.0.1750 (Windows 7): Executed 0 of 0 ERROR (0.465 secs / 0 secs)

When using version 1.0.7 of each file:

Your environment has been set up for using Node.js 0.10.26 (x64) and npm.

C:\Users\myUsername>karma start karma.conf.js
INFO [karma]: Karma v0.10.9 server started at http://localhost:****/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Windows 7)]: Connected on socket ***************
Chrome 33.0.1750 (Windows 7) ERROR
    Uncaught ReferenceError: angular is not defined
    at C:/Angello/javascripts/angular-mocks.js:16
Chrome 33.0.1750 (Windows 7): Executed 0 of 0 ERROR (0.064 secs / 0 secs)
4
  • perhaps the version of angular is very different from the version of angular mocks? they likely depend on each other? what version of each are you using? Commented Mar 4, 2014 at 20:09
  • AngularJS v1.0.7 and the mocks file comes from: AngularJS v1.2.9 Commented Mar 4, 2014 at 20:12
  • Changed the mocks version to 1.0.7 and got a different error: "angular is not defined" Commented Mar 4, 2014 at 20:19
  • this occurs to me and for me the error was using bower and npm at the same time.... Commented Mar 4, 2016 at 15:08

3 Answers 3

14

Normally this is caused by conflicting versions of angular, a wrong order of scripts definition in karma.conf.js, an incomplete import, or a syntax error.

Since I can see that you are testing a service, include that services' js file under the files (unless it is embedded in app.js) and remove the misplaced comma (see below):

files: [

'javascripts/angular.min.js',
'javascripts/angular-mocks.js',
'javascripts/app.js',
'<include your service js here>',
'tests/angelloModelSpec.js', <<-- and remove this trailing comma
],
Sign up to request clarification or add additional context in comments.

3 Comments

The service is embedded in app.js. I removed the errant comma..thanks.
angular.min.js should define window.angular = {}, and angular mocks should be able to see it. The order is correct, but somehow it does not work. Check the chrome console/karma console because it might not have loaded angular.min.js properly. best of luck
added an answer above...I was editing the wrong config file....for some reason,I had one open in the app directory and the one Jasmine was looking for was in C:\Users\myUsername\karma.conf.js
2

I found the problem! i was editing the wrong config file! I was editing a config file inside the app directory but the real config file was in C:\Users\myUsername\karma.conf.js

Thank you for your help!

2 Comments

This unfortunately does not explain to the reader why you got the error in the first place.
Angular was not defined in the correct config file.
0

You have to have the angular.min.js file above the angular-mocks.min.js file above your own app.js files in that order.

files: [
  'https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js',
  'https://cdnjs.cloudflare.com/ajax/libs/angular-mocks/1.8.2/angular-mocks.min.js',
  'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js',
  'js/app.js',
  ...
],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.