我试图配置Karma来运行用mocha和chai编写的react / redux测试,但遇到以下错误:
WARNING in ./src/Actions/menu-actions.test.js
Module build failed: Error: Plugin 0 specified in "base" provided an invalid property of "Instrumenter"
at Plugin.init (/Users/adc/project/node_modules/babel-core/lib/transformation/plugin.js:131:13)
at Function.normalisePlugin (/Users/adc/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:152:12)
at /Users/adc/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
at Array.map (native)
at Function.normalisePlugins (/Users/adc/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
at OptionManager.mergeOptions (/Users/adc/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
at OptionManager.init (/Users/adc/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (/Users/adc/project/node_modules/babel-core/lib/transformation/file/index.js:212:65)
at new File (/Users/adc/project/node_modules/babel-core/lib/transformation/file/index.js:135:24)
at Pipeline.transform (/Users/adc/project/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
at transpile (/Users/adc/project/node_modules/babel-loader/lib/index.js:46:20)
at Object.module.exports (/Users/adc/project/node_modules/babel-loader/lib/index.js:163:20)
@ ./src \.test.jsx?$
@ ./testing/test-bundler.specs.js
这看起来与我使用的webpack测试配置有关:
const path = require('path');
const webpack = require('webpack');
module.exports = {
devtool: 'inline-source-map',
module: {
loaders: [
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.css$/, loader: 'null-loader' },
{ test: /\.jsx?$/, loader: 'babel-loader', exclude: [/node_modules/] },
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: [/node_modules/, /specs/],
query: { plugins: ['istanbul'] },
},
{ test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i, loader: 'null-loader' },
],
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
},
}),
],
resolve: {
modules: [
path.resolve(__dirname, 'src'),
'src',
'node_modules',
],
extensions: ['.js', '.jsx'],
},
externals: {
jsdom: 'window',
'react/addons': true,
'react/lib/ExecutionEnvironment': true,
'react/lib/ReactContext': 'window',
},
};
我已经完成了配置并进行了一些调整以将其迁移到最新版本的webpack,但我看不到任何其他不兼容的区域。
完成后,这是我的完整karma配置,以及抛出错误的文件:
// testing/karma.conf.js
const webpackConfig = require('./webpack.test.config');
module.exports = (config) => {
config.set({
frameworks: ['mocha', 'chai'],
browsers: ['Chrome'],
files: [
{
pattern: './test-bundler.specs.js',
watched: false,
served: true,
included: true,
},
],
preprocessors: {
'./test-bundler.specs.js': ['webpack', 'sourcemap'],
},
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true,
stats: 'errors-only',
},
reporters: ['mocha', 'coverage'],
mochaReporter: {
output: 'autowatch',
},
coverageReporter: {
reporters: [
{
type: 'lcov',
dir: 'coverage',
},
{ type: 'text-summary' },
],
},
colors: true,
logLevel: config.LOG_ERROR,
});
};
// testing/test-bundler.specs.js
import 'babel-polyfill';
const context = require.context('../src', true, /\.test.jsx?$/);
context.keys().forEach(context);
// src/Actions/menu-actions.test.js
import * as menuActions from './menu-actions';
describe('menuActions', () => {
describe('updateVisibleMenuItems', () => {
it('updateVisibleMenuItems returns action type and onboarding code',
() => {
const result = menuActions.updateVisibleMenuItems(4);
expect(result).to.deep.equal({
type: 'UPDATE_VISIBLE_MENU_ITEMS',
onboardingStage: 4,
});
});
it('updateVisibleMenuItems handles different onboarding codes',
() => {
const result = menuActions.updateVisibleMenuItems(5);
expect(result).to.deep.equal({
type: 'UPDATE_VISIBLE_MENU_ITEMS',
onboardingStage: 5,
});
});
});
});