0

我正在使用 nwb 配置一个反应应用程序,我想用它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);

我还修改了 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
4

1 回答 1

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

于 2016-12-18T17:49:50.030 回答