2
votes

I have a angular where i want to test my controllers and services with karma and jasmine. I wrote some tests and they worked. Then i did some big changes to my angular project. Now my tests are all failing and i can't figure out why. I commented everything out in my tests and just wanted to check if the $controller service gets loaded. But not even this works.

My module definition:

angular.module('cManagement', [
    'angular-multi-select',
    'ngCookies',
    'ui.router',
    'ui.bootstrap',
    'pascalprecht.translate',
    'ngIdle'
])

Here is my test:

use strict';
describe("cManagement", function() {

 beforeEach(module('cManagement'));

 it('should exist', inject(function($controller){
 }));
});

My Karma config:

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

        basePath : '',
        reporters: ['progress', 'junit'],
        files : [
            'node_modules/angular/angular.js',
            'node_modules/angular-cookies/angular-cookies.js',
            'node_modules/angular-multi-select/dist/angular-multi-select.js',
            'node_modules/angular-ui-router/release/angular-ui-router.js',
            'node_modules/angular-ui-bootstrap/dist/ui-bootstrap.js',
            'node_modules/angular-translate/dist/angular-translate.js',
            'node_modules/angular-mocks/angular-mocks.js',
            'assets/plugins/angular-idle/angular-idle.js',
            'app/app.module.js',
            'app/components/login/*',
            'app/**/*'
        ],

        singleRun : true,

        frameworks: ['jasmine'],

        browsers : ['PhantomJS'],

        plugins : [
                'karma-jasmine',
                'karma-junit-reporter',
                'karma-phantomjs-launcher'
                ],

        junitReporter : {
            outputDir: 'test_out',
            userBrowserName: false
        }

    });
};

Here is the error i get:

INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/ INFO [launcher]: Starting browser PhantomJS INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket 7iIMKn57rTEEJqjikf2o with id 71013283 PhantomJS 2.1.1 (Linux 0.0.0) cManagement should exist FAILED /home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4527:53 forEach@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:321:24 loadModules@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4487:12 createInjector@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4409:30 workFn@/home/chris/IdeaProjects/campaignManagement/node_modules/angular-mocks/angular-mocks.js:2799:60 PhantomJS 2.1.1 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.006 secs / 0.007 secs)

Every hint is appreciated.

1
Can you try inject in a separate beforeEach block? beforeEach(inject(function($controller){ }));gmuraleekrishna
Maybe just a typing error but "use strict';" should be "use strict;"henrikmerlander
And you have an extra tick near use strictgmuraleekrishna
Moving into a seperate beforeEach block doesn't change anthing. Neither does correcting the use strict typo.bonbon
Hey, I figured out my issue. Change the karma server to Chrome or another browser. PhantomJS seems to hide errors like this for some reason. I'm betting the real reason will show up if you use Chrome.snowe

1 Answers

10
votes

Most probably you have a dependency issue -maybe a module that cannot be loaded- but you can't tell exactly which one because PhantomJS sometimes hides relevant info from the error message.

The solution is to change (temporarily) the testing browser to Chrome or any other non-headless browser to see exactly what's wrong.

eg: you should change in Karma config or Gruntfile.js or what have you:

the line browsers: ['PhantomJS'],

into something like: browsers: ['Chrome'],

and then you should be able to see the error and correct it. Afterwards, you can switch it back to PhantomJS.

Note: Just placing for posterity the workaround as per @snowe2010's comment (the question didn't have an answer so people may disregard the solution)