Testing an Angularjs application that loads manually

I have a problem testing the angularjs application (karma, jasmine) when loading the application manually into the code after some ajax HTTP requests.

angular.module("app", []); angular.element(document).ready(function() { angular.bootstrap(document, ["app"]); }); 

I also have the following code in my jasmine specs

 beforeEach(module('app')); 

And when I run my specs, I always have the same error

 Error: [$injector:modulerr] Failed to instantiate module app due to Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to laod it .... 

Is there a way to wait for an angular bootstrap application in a beforeEach function?

+5
source share
1 answer

I believe this problem is caused by the fact that your boot code is included in the karma configuration. I had the same problem.

I took the bootstrap code:

 angular.element(document).ready(function() { angular.bootstrap(document, ["app"]); }); 

and moved it to his own js file called bootstrap.js. Then in my index.html I put a script tag for this at the bottom of the page (after the script where I declare my application module).

 <script src="bootstrap.js"></script> 

I made sure bootstrap.js was not included in my karma configuration and now my tests pass.

The reason karma was unsuccessful was because it did not need the bootstrap code to run, and the bootstrap code was run by karma so that it would not initialize the application.

0
source

All Articles