Karma没有运行测试用例

时间:2016-05-18 02:19:16

标签: javascript webpack mocha karma-runner karma-webpack

我在Karma中使用requirejs来加载模块,它运行正常。当我将requirejs更改为webpack时,业力不会运行测试用例。我使用mochasinonchai作为我的测试框架。

├── src
|   ├── js
|       └── a.js
├── test
|   ├── spec
|        └── test_a.js

这是我的karma.conf.js

var webpackConfig = require("../webpack.config");

module.exports = function(config) {
    config.set({
        basePath: '../',

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

        files: [
            'test/**/test_*.js'
        ],

        preprocessors: {
        'test/**/test_*.js': ['webpack']
        },

        webpack: webpackConfig,

        proxies: {
           '/data/': '/base/test/data/'
        },

        exclude: [],

        client: {
            mocha: {
               reporter: 'html', 
               ui: 'bdd'
            }
        },

        reporters: ['progress'],

        port: 9876,

        colors: true,

        logLevel: config.LOG_DEBUG,

        autoWatch: false,

        browsers: ['PhantomJS', 'Chrome'],

        singleRun: false,

        concurrency: Infinity
    })
}

我的webpack.config.js是:

var webpack = require('webpack');
var path = require('path');

module.exports = {
    context: path.join(__dirname, 'src'),

    entry: {
        bundle1: './initializer.js'
    },

    output: {
        path: path.join(__dirname, 'src'),
        filename: '[name].js'
    },

    resolve: {
        extensions: ['', '.js'],
        modulesDirectories: ["./src", "node_modules"]
    },

    devtool: 'inline-source-map',

    module: {
        loaders: []
    }
}

我的test_a.js是:

require([
   'jquery',
   'src/js/a'
], function($, A) { // $ and A load successfully

    describe('A', function() {
        beforeEach(function() {
            //****
        });

        afterEach(function() {
            //****
        });

        describe('#on()', function() { // will come to this line
            it('should ....', function() { //will come to this line too
                assert.ok(1 > 0, "A is working"); // never run into the function
            });
        });
   }

}

当我运行karma时,错误消息如下所示: enter image description here

1 个答案:

答案 0 :(得分:0)

在我的测试文件中将require更改为define后,它现在正在运行。 似乎webpack将使用require加载Commonjs模块作为默认值。