gpt4 book ai didi

javascript - Karma 不运行测试用例

转载 作者:行者123 更新时间:2023-11-28 06:00:43 24 4
gpt4 key购买 nike

我在 Karma 中使用 requirejs 来加载模块,效果很好。当我将 requirejs 更改为 webpack 时,karma 不会运行测试用例。我使用 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

最佳答案

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

关于javascript - Karma 不运行测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com