簡體   English   中英

配置phantomjs與webpack和karma nwb一起使用

[英]Configure phantomjs to work with webpack and karma nwb

我正在使用nwb配置一個react應用,我想用chaienzyme來設置我的測試環境。 為了實現此目的,我進行了以下更改,創建了一個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM