0

I'm trying to run unit tests using karma and i'm getting the error You need to include some adapter that implements __karma__.start method!. I tried running with grunt and karma start commands. I did googling and all the solutions didn't work out. Not sure what i'm doing wrong. I included the right adapter which comes with karma-jasmine, which has the __karma__.start method, under plugins in karma.conf.js file. Here's my configuration file :-

 module.exports = function(config){
 config.set({
//  root path location that will be used to resolve all relative paths in files and exclude sections
basePath : '../',

files : [
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'node_modules/requirejs/require.js',
  'node_modules/karma-jasmine/lib/adapter.js',
  'app.js',
  'mainapp/mainapp.js',
  'mainapp/notes/notes.js',
  'mainapp/notes/partial/create/create.js',
  'mainapp/notes/partial/create/create-spec.js'
], 

// files to exclude
exclude: [
  'bower_components/angular/angular/*.min.js'
],

// karma has its own autoWatch feature but Grunt watch can also do this
autoWatch : false,

// testing framework, be sure to install the correct karma plugin
frameworks: ['jasmine', 'browserify', 'requirejs'],

// browsers to test against, be sure to install the correct browser launcher plugins
browsers : ['PhantomJS'],

// map of preprocessors that is used mostly for plugins
preprocessors: {
  'mainapp/notes/partial/create/create-spec.js'  : ['browserify']
},

reporters: ['progress'],

// list of karma plugins
plugins : [
  'karma-teamcity-reporter',
  'karma-chrome-launcher',
  'karma-phantomjs-launcher',
  'karma-babel-preprocessor',
  'karma-requirejs',
  'karma-jasmine',
  'karma-browserify'
],

singleRun: true

})}
Sandesh PS
  • 33
  • 2
  • 6
  • Canonical: [Error: You need to include some adapter that implements __karma__.start method](https://stackoverflow.com/questions/24220888/error-you-need-to-include-some-adapter-that-implements-karma-start-method) – ggorlen Jun 11 '23 at 01:21

1 Answers1

0

Using the requirejs framework turns off the automatic calling of __karma__.start. You need to create a file that a) configures RequireJS and b) calls __karma__.start to kick of the tests. Here's an example. It scans through the files that Karma is serving to find the files that contains tests. This is based on a naming convention that any file that ends with spec.js or test.js is a test file. It converts the file names to module names. It then configures RequireJS. One thing it does is pass all the test modules as deps so that all test modules are loaded right away. It sets __karma__.start as callback so that when all modules passed in deps are loaded, the tests start.

var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.test(file)) {
        // Normalize paths to RequireJS module names.
        allTestFiles.push(file);
    }
});

require.config({

    baseUrl: '/base',

    paths: {
        'chai':             'node_modules/chai/chai',
        'chai-jquery':      'node_modules/chai-jquery/chai-jquery',
        'jquery':           'node_modules/jquery/dist/jquery.min',
        'underscore':       '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min',
        'sn/sn-underscore': 'static/scripts/sn/sn-underscore',
        'vendor/jquery-ui': '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min'
    },

    deps: allTestFiles,

    callback: window.__karma__.start
});

You need to include this file in your files parameter in your karma.conf.js file. Since you use the requirejs framework, you just need to put it first in the list. For instance, if you call the file test-main.js (as suggested in Karma's documentation):

files: [
  'test-main.js',
  ...
]

If you load RequireJS yourself by listing it in files, you need to put test-main.js after RequireJS.

Jim
  • 3,821
  • 1
  • 28
  • 60
Louis
  • 146,715
  • 28
  • 274
  • 320