Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

karma test runner not running any tests

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.

like image 566
Apprentice Programmer Avatar asked Aug 08 '14 09:08

Apprentice Programmer


People also ask

How does Karma test Runner work?

Karma is essentially a tool which spawns a web server that executes source code against test code for each of the browsers connected. The results of each test against each browser are examined and displayed via the command line to the developer such that they can see which browsers and tests passed or failed.

How do I check my karma log?

0.9 server the “console. log” statements, inside the spec tests as well as in your web application code, are logged to the terminal running the karma server by default. If you want to turn this off, you can set the “client => captureConsole” to false.

Does karma run tests in parallel?

We can utilize karma-parallel plugin to execute test cases parallely. This npm package splits your unit tests into multiple suites that run in parallel with each other, on different threads of your processor.

Which is better jest or karma?

Karma is a JavaScript test runner. It helps run the testing of the frontend in a real browser, running the test against the production build in a real browser and can help find discrepancies across different browsers. Jest is a must consideration for React users.


1 Answers

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
like image 88
msapkal Avatar answered Oct 05 '22 21:10

msapkal