Mocha - Chai Karma “suite is not defined”

2019-04-12 08:34发布

I'm quite new to jscript tdd and got a problem, hope someone can show me what I'm doing worng. Running the Tests in a browser (via HTML File) everything works fine. running them through node and karma i got the following exception

I want to use Mocha and Chai within karma in node.js host. I installed via npm install [...] --save-dev mocha and karma-mocha

I've a testlibrary like this

suite('first suite', function () {
    test('SuccessTest', function () {
        expect(1).to.equal(1);
    });

    test('FailTest', function () {
        expect(1).to.equal(2);
    });
});

in node i used karma init to create the config file in which i set frameworks to

frameworks: ['mocha','chai'],

now when I run karma it got this error enter image description here

"suite is not defined"

I assumed that declaring mocha and chai as frameworks this should have worked?

I also installed in node the karma-mocha and karma-chai plugins.

What do I wrong and what do I have to do ?

where the whole karma config file

// Karma configuration
// Generated on Mon Sep 23 2013 17:24:19 GMT+0200 (Mitteleuropäische Sommerzeit)

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

    // base path, that will be used to resolve files and exclude
    basePath: '',


    // frameworks to use
    frameworks: ['mocha','chai'],


    // list of files / patterns to load in the browser
    files: [
      'tests.js'
    ],


    // list of files to exclude
    exclude: [

    ],


    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


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


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


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


    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


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

I also tried to add mocha.js and chai.js to the file load list but this didn't help

files: [
  'mocha.js',
  'chai.js',
  'tests.js'

],

When I change tests to jasmine it works.

1条回答
Evening l夕情丶
2楼-- · 2019-04-12 09:32

This is because there is no "chai" framework/plugin for Karma, but I think it's a good idea to have one.

You need to do this in some of your included files, in order to use "tdd" mocha style ("bdd" is the default one):

// in config-mocha.js
window.mocha.setup({ui: 'tdd'});

You need to load "chai" manually:

module.exports = function(config) {
  config.set({
    files: [
      'path/to/chai.js',
      'config-mocha.js',
      // .. your source and test files
    ]
  });
};
查看更多
登录 后发表回答