我将 webpack+babel 用于 React+Redux 应用程序,使用 Mocha+Karma 进行测试。redux 测试用例正在正确执行。但是,当我尝试使用 react-addons-test-utils 进行 DOM 测试并使用 Karma 运行它时,会出现此错误
未捕获的类型错误:无法将符号值转换为位于http://localhost:9876/karma.js:339的字符串
为了正确调试它,我将几个记录器放在 karma lib 文件中(我知道我不应该有)并得到了这个
但是,当我不使用 KarmaJS 并简单地尝试运行测试时,它似乎很好。这是我的 karma.conf
"use strict";
let webpackConfig = require('./webpack.config');
const coverage = process.env.COVERAGE;
webpackConfig.externals = {};
getWebpackLoaders();
module.exports = function(config){
config.set({
basePath: '.',
frameworks:['mocha'],
autoWatchBatchDelay:500,
browsers: ['Chrome'],
customLaunchers: {
Chrome_without_security: {
base: 'Chrome',
flags: ['--disable-web-security']
}
},
preprocessors: {
'./test/**/*.js':['webpack']
},
reporters: getReporters(),
coverageReporter: {
reporters: [
{type: 'lcov', dir: 'coverage/', subdir: '.'},
{type: 'json', dir: 'coverage/', subdir: '.'},
{type: 'text-summary'}
]
},
exclude:['node_modules'],
port:9876,
files: [
'node_modules/react/dist/react-with-addons.js',
'test/test.js'
],
webpack:webpackConfig,
plugins: [
'karma-webpack',
'karma-mocha',
'karma-coverage',
'karma-chrome-launcher'
]
})
};
function getWebpackLoaders(){
if(coverage){
let loaders = webpackConfig.module.loaders;
let jsLoader = loaders[1];
jsLoader.exclude = /node_modules|\.test\.js$/ //exclude both node_modules and test
loaders.push({
test:/\.test\.js$/,
loaders:['babel-loader']
});
loaders.push({
test: /\.js$/,
loaders: ['isparta'],
exclude: /node_modules|\.test.js$/ // exclude node_modules and test files
})
}
}
function getReporters() {
var reps = ['progress'];
if (coverage) {
reps.push('coverage');
}
return reps;
}
编辑 1. 将 webpack.config 添加到此
var webpack = require('webpack');
var argv = require('minimist')(process.argv.slice(2));
var DEBUG = !argv.release;
var AUTOPREFIXER_LOADER = 'autoprefixer-loader?{browsers:[' +
'"Android >= 4", "Chrome >= 20", "Firefox >= 24", ' +
'"Explorer >= 9", "iOS >= 6", "Safari >= 6"]}';
var GLOBALS = {
'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
'__DEV__': DEBUG
};
var config = {
entry: './app.js',
output: {
filename: 'app.js',
path: './build/',
publicPath: './',
sourcePrefix: ' '
},
externals: {
react: 'React'
},
cache: DEBUG,
debug: DEBUG,
devtool: DEBUG ? '#inline-source-map' : false,
stats: {
colors: true,
reasons: DEBUG
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin(GLOBALS)
].concat(DEBUG ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.AggressiveMergingPlugin()
]),
resolve: {
extensions: ['', '.webpack.js', '.js', '.jsx']
},
module: {
preLoaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader'
}
],
loaders: [
{
test: /\.less$/,
loader: 'style-loader!css-loader!' + AUTOPREFIXER_LOADER + '!less-loader'
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.json$/,
exclude: /node_modules/,
loader: 'json-loader'
}
]
}
};
module.exports = config;