简体   繁体   中英

Problems using Mocha with Webpack and Typescript

With a standard looking .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". I'm on the latest versions of all involved packages. Does anyone know any solutions to this?

Thanks in advance!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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