reactjs - 导入 enzyme 后加载模块 DomUtils 时出现 Webpack 错误

标签 reactjs typescript webpack cheerio enzyme

我有一个使用 TypeScript 和 React 0.14 的项目。

然后我用 karma/mocha/chai 设置了测试环境。和它的工作。但是当我从 enzyme 中导入和使用函数时,我在浏览器中遇到了错误(来自 Chrome 的人类可读错误):

Uncaught TypeError: ext[key].bind is not a function

DomUtils error. Debug mode.

据我所知,模块 227...232(内部 DomUtils 文件)在使用前未加载。

也许我忘记了什么?或者有人知道解决方法吗?

对于巨大的配置感到抱歉:

网络包配置:

var webpack = require("webpack"),
    path = require("path"),
    ExtractTextPlugin = require("extract-text-webpack-plugin"),
    precss = require('precss'),
    autoprefixer = require('autoprefixer');

module.exports = {
    entry: [
        path.resolve(__dirname, "app/app.tsx")
    ],
    output: {
        filename: "bundle.js",
        publicPath: "/build/",
        path: path.resolve(__dirname, "build")
    },
    module: {
        loaders: [
            {test: /\.json$/, loader: 'json'},
            {test: /\.tsx$/, exclude: /node_modules/, loader: 'es3ify!ts'},
            {test: /\.s?css$/, loader: ExtractTextPlugin.extract('style', 'css!postcss')},
            {test: /\.svg|eot|ttf|woff|woff2|ico|png|gif|jpg($|\?)/, loader: 'file'}
        ]
    },
    postcss: function () {
        return [precss, autoprefixer];
    },
    resolve: {
        root: path.resolve(__dirname, "app"),
        extensions: ["", ".js", ".ts", ".tsx", ".json"]
    },
    plugins: [
        new ExtractTextPlugin("bundle.css")
    ]
};

业力配置:

var webpack = require('webpack');
var webpackConfig = require('./webpack.config');

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai', 'sinon'],
    files: [
      'app/**/*-test.tsx'
    ],
    exclude: [],
    preprocessors: {
      'app/**/*.tsx': ['webpack']
    },
    webpack: {
      resolve: webpackConfig.resolve,
      module: webpackConfig.module,
      externals: {
        'cheereo': 'window',
        'react/addons': true,
        'react/lib/ExecutionEnvironment': true,
        'react/lib/ReactContext': true
      }
    },
    reporters: ['progress', 'spec'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['PhantomJS'],
    singleRun: true,
    concurrency: Infinity
  })
}

测试文件:

import * as React from 'react';
import { shallow } from 'enzyme';

describe('Dashboard', () => {
  it('should be OK', () => {
    const instance = shallow(<div />);
    expect(true).to.equal(true, 'true === true');
  });
});

Webpack 导入链:enzyme -> ... -> cheerio -> ... -> DomUtils

StackOverflow 我最后一次解决问题的机会,谷歌不知道答案。

最佳答案

我遇到过同样的问题,对我来说,将 domutils 作为 webpack 配置中的 extrenal 会有所帮助。根据您的配置试试这个:

webpack: {
    resolve: webpackConfig.resolve,
    module: webpackConfig.module,
    externals: {
      'domutils': 'true',
      'cheereo': 'window',
      'react/addons': true,
      'react/lib/ExecutionEnvironment': true,
      'react/lib/ReactContext': true
    }
}

关于reactjs - 导入 enzyme 后加载模块 DomUtils 时出现 Webpack 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38122544/

相关文章:

javascript - 接力现代,不提供 Prop

css - 将 react 组件居中不起作用

javascript - 我尝试动态更改 iframe 的 url,但收到错误 : Unsafe value used in resource URL

html - 如何更新 Bootstrap 进度条。

symfony - Datatables.net (1.10.22) + Webpack Encore (1.11) + Symfony (5.2.6)

javascript - onClick 事件的调度操作

reactjs - 使用 axios 发出的请求不起作用,但如果它适用于 XMLHttpRequest

javascript - 如何在 ChartJS 中跳过 x 轴标签

google-chrome - 使用 webpack 源映射时,chrome 中的文件中的断点不会被命中

node.js - 如何在 Angular CLI 中配置代理