我正在使用 nwb 配置一个反应应用程序,我想用它chai
来enzyme
设置我的测试环境。为了实现这一点,我进行了以下更改,我创建了一个tests.webpack.js
文件:
import chai from 'chai';
import chaiEnzyme from 'chai-enzyme';
import chaiAsPromised from 'chai-as-promised';
import sinonChai from 'sinon-chai';
chai.use(chaiEnzyme());
chai.use(chaiAsPromised);
chai.use(sinonChai);
const context = require.context('./src', true, /\.spec\.js/);
context.keys.forEach(context);
我还修改了 karma 配置nwb.config.js
:
const karmaChaiPlugins = require('karma-chai-plugins');
module.exports = {
type: 'react-component',
npm: {
esModules: true,
umd: {
global: 'ReactMg',
externals: {
react: 'React',
},
},
},
karma: {
testContext: 'tests.webpack.js',
plugins: [
karmaChaiPlugins,
],
frameworks: ['mocha', 'chai', 'chai-as-promised'],
},
webpack: {
compat: {
enzyme: true,
sinon: true,
},
},
};
nwb test
在定义后index.spec.js
运行时出现错误src
:
PhantomJS 2.1.1 (Linux 0.0.0) ERROR
TypeError: undefined is not a function (evaluating 'context.keys.forEach(context)')
at tests.webpack.js:73
PhantomJS 2.1.1 (Linux 0.0.0): Executed 0 of 0 ERROR (0.375 secs / 0 secs)
Karma exit code was 1