1

I'm having a hell of a time with Karma/Jasmine. I'm just trying to run the example specs from Jasmine's site.

When I run jasmine on command line, the tests run fine. However, if I try to run them using Karma test runner, I have a multitude of problems.

Here's My File Structure

Here's my file structure

Here's my karma.conf.js file:

module.exports = function(config) {
config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'requirejs'],


    // list of files / patterns to load in the browser
    files: [
        'node_modules/requirejs/require.js',
        '**/test-main.js', {
            pattern: 'spec/jasmine_examples/*.js',
            included: false
        }
    ],


    // list of files to exclude
    exclude: ['**/*conf.js'],
...port,browser etc.

Here's my test.main.js file

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

var pathToModule = function(path) {
    return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

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

require.config({
    // Karma serves files under /base, which is the basePath from your config file
    baseUrl: '/base',

    // dynamically load all test files
    deps: allTestFiles,

    // we have to kickoff jasmine, as it is asynchronous
    callback: window.__karma__.start
});

When I run karma start, I get a 404 that PlayerTest.js and SongTest.js are not under base/. However they are loaded and located in base/spec/jasmine_examples. In addition, PlayerTest.js throws an error "module not defined".

Honestly, what am I doing wrong?

Eric Wendelin
  • 43,147
  • 9
  • 68
  • 92
MickJuice
  • 539
  • 2
  • 10
  • 25

1 Answers1

0

I think you need to refer to the karma-requirejs and the karma-jasmine plugins in the karma.conf.js file -

config.set({
  plugins: [
    'karma-jasmine',
    'karma-requirejs'
  ],

From karma doc, it states:

Please note just about all frameworks in Karma require an additional plugin/framework library to be installed (via NPM). Additional information can be found in plugins.

You will not need to have require.js in the files section.

cactusme
  • 143
  • 7