5

I am getting into Angular Testing with Karma and Jasmine. After doing the karma init and writing the first test for a home controller, I keep getting Executed 0 of 0 ERROR. It does not seem like it's being picked up in the files.

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

    basePath: '',

    frameworks: ['jasmine'],
    files: [
        'public/assets/libs/angular/angular.min.js',
        'bower_components/angular-mocks/angular-mocks.js',
        'public/app/app.module.js',
        'public/app/app.config.js',
        'public/app/**/*.js',
        'test/unit/**/*.spec.js'
    ],

    exclude: [
    ],

    preprocessors: {
    },

    reporters: ['progress'],

    port: 3030,

    colors: true,

    logLevel: config.LOG_INFO,

    autoWatch: true,

    browsers: ['Chrome'],

    singleRun: false

    }); //config.set
} //module.export

The test (fixed)

describe('HomeController', function() {

//Inject 'app' module into this spec.
beforeEach(module('app'));

//Inject instance of Scope.
var homeController;

beforeEach(inject(function($rootScope, $controller) {
    //create a new scope
    scope = $rootScope.$new();

    //create new controller, using the newly created scope
    HomeController = $controller('HomeController', {
        $scope: scope
    });

}));

//Set expectations
it('the message should say Hello World', function(HomeController) {
    expect(scope.message).toBe('Hello World');
});


});

And the HomeController:

(function() {
'use strict';

angular
    .module('app')
    .controller('HomeController', HomeController);

HomeController.$inject = ['$scope', '$log'];

function HomeController($scope, $log) {
    /*jshint validthis: true*/
    var vm = this;

    vm.message = 'Hello World';

} //HomeController()

})(); //Controller

Thanks for helping.

leocreatini
  • 676
  • 1
  • 9
  • 18

3 Answers3

6

I figured it out. The test it('...') was inside the beforeEach() block, so there were 0 tests. It is now in it's own separate block, below.

leocreatini
  • 676
  • 1
  • 9
  • 18
5

To all those also experiencing the "Executed 0 of 0 ERROR" issue:

Here is how to better debug this error since the log message in the command line might be truncated or not helpful enough:

In the Browser instance of karma (e.g. Chrome), set a breakpoint in karma.js in the following function: this.error = function (msg, url, line) { Since the string representation of the msg that will be transported to the command line log is not very helpful, analyze it here.

Esteban Gehring
  • 821
  • 8
  • 8
4

Had the same problem testing typescript - I found the problem in the chrome console:

Refused to execute script from 'xxx' because its MIME type ('video/mp2t') is not executable.

The fix turned out to be adding

mime: {
  'text/x-typescript': ['ts', 'tsx']
}

into karma.conf.js.

Joshua Davies
  • 981
  • 9
  • 16