0

Jasmine AngularJS test (passes in karma start configs/karma.conf.js)

describe('IndexController', function () {
    beforeEach(module('myApp'));

    var ctrl, scope;

    beforeEach(inject(function ($controller, $rootScope) {
        scope = $rootScope.$new();
        ctrl = $controller('IndexController', {
            $scope: scope
        });
    }));

    it('should add name parameter to scope', function () {
        expect(scope.name).toBeDefined();
    });
});

Contents of controllers.js

var myApp = angular.module('myApp', []);

myApp.controller('IndexController', function ($scope) {
        $scope.name = 'bob';
});

Output of: jasmine-node test/ --junitreport

   Message:
     TypeError: object is not a function
   Stacktrace:
     TypeError: object is not a function
    at null.<anonymous> (/tmp/tests/test/unit/controllerSpec.js:38:16)
    at Object.<anonymous> (/tmp/tests/test/unit/controllerSpec.js:36:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
A T
  • 13,008
  • 21
  • 97
  • 158
  • Which config file does it use? How does it get hold of the files it's dependent on? I've never used jasmine-node but I'm tryna see if we could debug this together – dcodesmith Dec 22 '13 at 12:04
  • Do you import angular-mocks.js on it? It is needed for module() and inject() – Jesus Rodriguez Dec 22 '13 at 13:36

2 Answers2

0

beforeEach() taks in a function. Be sure module('myApp') and inject(...) are returning actual function definitions. Jasmine's beforeEach is like "call the passed function before each test", so you may want:

beforeEach( function(){ module('myApp') } );

I am unfamiliar with karma, but do use jasmine's done() method in my beforeEach() like this:

var valueForEachTest = null;
beforeEach( function(done) {
  doSomething( function(value){
    valueForEachTest = value;
    done();
  });
} );

Not using that done() call breaks my tests because I'm doing a few asynchronous call in there (perhaps Jasmine is not waiting for beforeEach to finish?).

clay
  • 5,917
  • 2
  • 23
  • 21
0

Angular is made to run in the browser. It will not run in node. At least, not without a lot of effort.

There has been an attempt to port it to node, but that project is really intended to render angular pages server side for search engine optimization. Unless you have a really good reason, you shouldn't be trying to test Angular apps in Node.

James Talmage
  • 328
  • 1
  • 6