【问题标题】:After adding enzyme for React testing all tests fail为 React 测试添加酶后,所有测试均失败
【发布时间】:2018-12-18 09:42:49
【问题描述】:

这很简单。在我的 React 应用程序中,我想使用 Enzyme 和 Jest 来测试我的应用程序。我已经为 Enzyme 创建了一个设置文件

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-15';

Enzyme.configure({ adapter: new Adapter() });

并将这一行添加到 jest 配置文件中:

"setupTestFrameworkScriptFile": "<rootDir>/test/setupEnzyme.js"

我无法进行任何测试。从字面上看,所有测试(即使是最基本的测试 2+2 是否为 4)都失败并返回

 FAIL  test/app.test.js
  ● Test suite failed to run

    TypeError: The super constructor to "inherits" must have a prototype

      at Object.<anonymous> (node_modules/parse5/lib/extensions/position_tracking/preprocessor_mixin.js:29:1)
      at Object.<anonymous> (node_modules/parse5/lib/extensions/location_info/tokenizer_mixin.js:5:41)
      at Object.<anonymous> (node_modules/parse5/lib/extensions/location_info/parser_mixin.js:5:34)
      at Object.<anonymous> (node_modules/parse5/lib/parser/index.js:6:31)

最基本的测试(这个在添加酶之前效果很好):

describe('App', () => {
  it('should be able to run tests', () => {
    expect(1 + 2).toEqual(3);
  })
});

React组件(TextField使用material-ui):

import React from 'react';
import { shallow } from 'enzyme';
import TextField from 'components/Pages/FormElements/TextField'
import getMuiTheme from 'material-ui/styles/getMuiTheme';

const muiTheme = getMuiTheme();

describe('Text Field', () => {
  it('renders without crashing', () => {
    const wrapper = shallow(<TextField />, { context: { muiTheme },
      childContextTypes: { muiTheme: React.PropTypes.object } });
  })
});

【问题讨论】:

  • 能否提供测试代码?

标签: javascript reactjs unit-testing jestjs enzyme


【解决方案1】:

尝试使用设置setupTests.js命名文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 2019-04-22
    相关资源
    最近更新 更多