Angular module is executed twice only when testing

My angular application is working ok when ran normally, but seems to be executed twice when I'm doing unit tests (using Karma & PhantomJS)

It is instanciated as follows:



var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;

if ( app ) {
    console.log('App running on a mobile device');
    document.addEventListener("deviceready", function(){
        angular.bootstrap(document, ['esme']);
} else {
    console.log("App running in a browser");
    angular.element(document).ready(function() {
        angular.bootstrap(document, ['esme']);


var esme = angular.module('esme', ['ui.router', 'ngAnimate', 'ngTouch', 'DataStore', 'angular-carousel', 'SessionParams', 'hmTouchEvents']);['$location', '$rootScope', '$state', 'NotificationManager', function($location, $rootScope, $state, NotificationManager) {


    $rootScope.$on('$locationChangeSuccess', function (event, current, previous) {
            $rootScope.title = $;


describe('TestController', function() {
    var $controller;
    var $rootScope;


    beforeEach(module(function($urlRouterProvider) {

    beforeEach(inject(function(_$controller_, _$rootScope_){
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $controller = _$controller_;
        $rootScope = _$rootScope_.$new();

    describe('$scope.cheer', function() {
        var $scope, controller;

        beforeEach(function() {
            $scope = $rootScope;
            controller = $controller('Main', { $scope: $scope });

        describe("#hello()", function(){
            it('ensures hello is correct', function() {

The output of a browser console is the following:

"App running in a browser"



But the output of the unit test is the following:

INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/

[ .. Karma / PhantomJS stuff .. ]

LOG: 'App running in a browser'

WARN [web-server]: 404: /views/Main/Login.html

LOG: 'config'


LOG: 'config'

LOG: 'run'

PhantomJS 1.9.8 (Windows 7): Executed 1 of 1 SUCCESS (0.007 secs / 0.001 secs)

WARN [web-server]: 404: /views/Main/Login.html

[17:16:14] Finished 'test:unit' after 5.31 s

I'm still learning how to mock an angular application so I strongly believe that I made some obvious mistakes in the test file. Can you please help me find them and explain why did this behavior happened ?

The only clue I may have for now comes from Angular's doc, should I declare a module only for running the app ?:

Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the service have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

Thanks for your help!

1 Answers
  1. Well, after a good night, the answer was straightforward...

    Because I was doing manual bootstrapping, in my karma.conf file, I simple excluded the file where the it was done.


    files: [
    // list of files to exclude
    exclude: [
    2015-04-22 07:45:32
Related Articles
You Might Also Like