1

I want to run the test cases using karma and jasmine. I have done the following, but it is giving error like:

Error:

Chrome 59.0.3071 (Linux 0.0.0): Executed 0 of 0 ERROR (0.12 secs / 0 secs)

karma.conf.js:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine','requirejs'],
    files: [
        {pattern: 'node_modules/**/*.js', included: false}, 
        //source files
        {pattern: 'src/**/*.js', included: false}, 
        //test files
        {pattern: 'tests/**/apptest-spec.js', included: false}, 

        'tests/test-main.js' 
     ],
    exclude: [
    ],
    plugins: [
        'karma-requirejs',
        'karma-chrome-launcher',
        'karma-jasmine'
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    concurrency: Infinity
  })
}

apptest-spec.js:

describe('MainCtrl', function() {
  beforeEach(module('app-module'));
  var $controller;
  beforeEach(inject(function(_$controller_){
    $controller = _$controller_; 
    }));

  describe('$scope.titleofapp', function() {
    var $scope, controller;
    beforeEach(function() {
      $scope = {};
      controller = $controller('MainCtrl', { $scope: $scope });
    });

    it('sets the title of app to "TestApp" module', function() {
      $scope.titleofapp();
      expect($scope.title).toEqual('TestApp');
    });
  });
});

MainCtrl.js:

define(['angular', './app-module'], function(angular, appModule) {
    'use strict';
    return appModule.controller('MainCtrl', ['$http', '$q', 'appService', '$stateParams', function($http, $q, $stateParams, appService){
        $scope.titleofapp = function(){
        $scope.title = 'TestApp';
        }
   }]);  
});

test-main.js:

var allTestFiles = [];
var TEST_REGEXP = /(-orchestratespec).js/;
Object.keys(window.__karma__.files).forEach(function(file) {
        if (TEST_REGEXP.test(file)) {
        var normalizedTestModule = file;
        console.log('normalized path', normalizedTestModule);// giving: '/base/tests/controllers/apptest-spec.js'
        allTestFiles.push(file);
    }
});
console.log(allTestFiles);// giving: ['/base/tests/controllers/apptest-spec.js']
require.config({
     baseUrl: '/base/tests/scripts',
    paths: {
        node_modules: '/base/node_modules',      
    },
    deps: allTestFiles,  
    callback: window.__karma__.start()
});

I am not sure where I am doing wrong, Please help me regarding this to run my test case successfully. Thanks in advance !

Note: If I reload the application in the browser, I can see in Network tab, status is cancelled is coming with url name like: http://localhost:9876/base/tests/controllers/apptest-spec.js?55a86414cf2ce1e51f1c9e865bff8594a27e181f

8
  • If you get into the browser that Karma starts, go into the debugging panel, perform a page reload and look at the network activity, is there a request for .../apptest-spec.js and is it successful?
    – Louis
    Commented Aug 23, 2017 at 13:16
  • @Louis, yes i have done above, by observation on reloading, it is giving in Network tab: localhost:9876/base/tests/controllers/…, status is cancelled.
    – Sana
    Commented Aug 23, 2017 at 13:22
  • i think this is because callback: window.__karma__.start() should be callback: window.__karma__.start (in require.config({...}); in test-main.js)
    – xmike
    Commented Aug 23, 2017 at 13:57
  • @xmike, if i write the above statement in test-main.js for callback:, then it is giving error like: Uncaught SyntaxError: Unexpected token in .
    – Sana
    Commented Aug 23, 2017 at 14:04
  • well, that means there is syntax error somewhere )) where exactly? what does it say? btw, it might be better if you switch to config.LOG_DEBUG in karma.conf.js for the period of setting up all the environment, it is helpful sometimes.
    – xmike
    Commented Aug 23, 2017 at 14:09

0