1

requirejs在 Karma 中使用过加载模块,效果很好。当我更改requirejs为 时webpack,业力不会运行测试用例。我使用mocha, sinon,chai作为我的测试框架。

├── 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时,错误消息是这样的: 在此处输入图像描述

4

1 回答 1

0

在我的测试文件中更改require为之后,它现在可以工作了。define它似乎webpack将用于require默认加载 Commonjs 模块。

于 2016-05-18T05:47:41.233 回答