配置 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中的karma配置:

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 is not a function

您应该在 context.keys().forEach(context); 上更改 context.keys.forEach(context);,因为 keys 是函数 [1]

[1] - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys