简体   繁体   English

将Mocha与Webpack和Typescript一起使用时出现问题

[英]Problems using Mocha with Webpack and Typescript

With a standard looking .setup.js : 使用标准的.setup.js

require('babel-register')();

var jsdom = require('jsdom').jsdom;

var exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    exposedProperties.push(property);
    global[property] = document.defaultView[property];
  }
});

global.navigator = {
  userAgent: 'node.js'
};

documentRef = document;

and a test bench: 和测试台:

import * as React from 'react';
import { expect } from 'chai';
import { mount, shallow } from 'enzyme';
import * as mocha from 'mocha';

class Woof extends React.Component<{},{}> {
    render() {
        return <p>woof</p>;
    }
}

describe('<Woof />', () => {
    it('passes sanity check', () => {
        const wrapper = mount(<Woof />);
        expect(true).to.equal(true);
    });
});

Mocha is failing to run the webpack bundle, erroring with a "TypeError: cannot read property 'crypto' of undefined". Mocha无法运行webpack捆绑包,并出现“ TypeError:无法读取未定义的属性'crypto'”错误。 I'm on the latest versions of all involved packages. 我正在使用所有涉及软件包的最新版本。 Does anyone know any solutions to this? 有人知道对此有任何解决方案吗?

Thanks in advance! 提前致谢!

经过一些实验后,实现此工作的一种方法是指示webpack到目标node并从包中排除node_modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM