我正在使用nwb来配置一个react应用程序,我会使用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);我还修改了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发布于 2016-12-18 17:49:50
要修复错误TypeError:未定义不是函数
您应该更改context.keys.forEach(context); on context.keys().forEach(context);,因为keys是function1
https://stackoverflow.com/questions/41210882
复制相似问题