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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With