mocha-webpack - RUNTIME EXCEPTION加载测试时发生异常

时间:2017-08-19 01:54:04

标签: reactjs mocha enzyme jsdom mocha-webpack

运行测试时出现此错误:

function __construct{
   spl_autoload_register('TR_Autoloader' , array($this,'TR_Autoloader'));
}

我有一个使用mapbox.js和leaflet.js的react组件。我有

时出现上述错误
RUNTIME EXCEPTION  Exception occurred while loading your tests

TypeError: Cannot read property 'indexOf' of undefined
  at /Users/user/project/.tmp/mocha-webpack/1503105850474/webpack:/~/leaflet/dist/leaflet-src.js:724:1
  at /Users/user/project/.tmp/mocha-webpack/1503105850474/webpack:/~/leaflet/dist/leaflet-src.js:843:2

位于文件顶部。但是如果我把它移到import L from 'mapbox.js'; import 'leaflet.markercluster'; 作为

componentDidUpdate

然后测试正常运行。我认为这可能表明DOM存在问题,但在我的测试设置中使用jsdom并不能解决问题。

const L = require('mapbox.js');
require('leaflet.markercluster');

我的测试非常简单:

require('babel-register')();
require('babel-polyfill');
require('isomorphic-fetch');

const { JSDOM } = require('jsdom');

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;

function copyProps(src, target) {
  const props = Object.getOwnPropertyNames(src)
  .filter(prop => typeof target[prop] === 'undefined')
  .map(prop => Object.getOwnPropertyDescriptor(src, prop));
  Object.defineProperties(target, props);
}

global.window = window;
global.document = window.document;
global.navigator = {
  userAgent: 'node.js',
};
copyProps(window, global);

0 个答案:

没有答案