[英]Configure phantomjs to work with webpack and karma nwb
我正在使用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,
},
},
};
在src
定義index.spec.js
后運行nwb test
時出現錯誤:
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
修復錯誤TypeError:undefined不是函數
您應該更改context.keys.forEach(context);
在context.keys().forEach(context);
因為keys
是功能[1]
[1] -https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.