karma test runner not running any tests

All we need is an easy explanation of the problem, so here it is.

I’m using karma with jasmine and followed online guide by installing using

npm install --save-dev karma

and other necessities

i ran

./node_modules/karma/bin/karma start

and

karma start karma.conf.js

which opened up a external chrome browser showing that karma is connected.
I wrote a simple unit test for one my functions it seems its not running any tests at all

This is my karma config file.

    // Karma configuration
module.exports = function(config) {
 config.set({
// base path, that will be used to resolve files and exclude
basePath: '',

// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
  'app/assets/components/angular/angular.js',
  'app/assets/components/angular-mocks/angular-mocks.js',
  'app/assets/javascripts/**/**/*.js',
  'spec/javascripts/**/*.js'
],

// list of files / patterns to exclude
exclude: [],

// web server port
port: 8080,

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
 };

my unit test

describe('Unit: AddMedicalService',function(){


beforeEach(module('DoctiblePreTreatment'));
var ctrl, scope;

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

ctrl = $controller('AddMedicalServiceModalCtrl',{
  $scope: scope
  });
}));


it('should create return true if var 1 is greater than var2 , false if other wise',
function(){
  var compare1 = function(){
    var var1 = 1;
    var var2 = 0;
    return var1 > var2;
  }

  var compare2 = function(){
    var var1 = 0;
    var var2 = 1;
    return var1 > var2;
  }

  expect(compare1).toBeTruthy();
  expect(compare2).toBeFalsy();

   });

  });

the particular function in the controller im trying to test

(function() {
app.controller('AddMedicalServiceModalCtrl',['ProviderMedicalService','Treatment','$scope','$modalInstance',function(ProviderMedicalService,Treatment,$scope,$modalInstance){
    $scope.newTreatment = {}


    $scope.checkless = function(var1,var2){
        var1 = parseInt(var1);
        var2 = parseInt(var2);

        if(var1 > var2){
            return true;
        }
        else{
            return false;
        }
    }
    }]);
   })();

what is displayed on the console when i run karma

  INFO [karma]: Karma v0.12.21 server started at http://localhost:8080/
  INFO [launcher]: Starting browser Chrome
  INFO [Chrome 36.0.1985 (Mac OS X 10.9.4)]: Connected on socket MkqZfXcO6iIX4Od23QEr with id 9498055

Additional info: I’m using angular-js with ruby on rails. I’m aware that there is the jasmine gem out there that can help me. But my boss insisted that we should try using karma to do our unit testing/E2E for anuglarjs portion and rspec for rails.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Under karma.config.js, set either singleRun or autoWatch to true. In your case both of them are set to false, hence karma is not running the tests.

singleRun: If true, it captures browsers, runs tests and exits with 0 exit code (if all tests passed) or 1 exit code (if any test failed).

singleRun: true

autoWatch: Enable or disable watching files and executing the tests whenever one of these files changes. Incase you want to watch your files.

autoWatch: true

Method 2

Simple but sometimes overlooked cause: ensure you don’t have syntax or compilation errors.

If running a Karma test for JavaScript via your IDE, you may have a syntax error which doesn’t appear when you run your tests. This leads to Karma issuing the message "no tests were found".

I ran it under WebStorm; the error appeared under the "Karma Server" tab, while the "No tests were found" message appeared under "Test Run" (both tabs under "Run" or "Debug" tool window).

Method 3

Following configuration works for me –

 reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO, // config.LOG_DEBUG,
        autoWatch: true,
        browsers: ['ChromeNS'],
        singleRun: false,
        customLaunchers: {
            ChromeHeadlessNS: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox', '--disable-gpu']
            },
            ChromeNS: {
                base: 'Chrome',
                flags: ['--no-sandbox', '--disable-gpu']
            }
        }

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply