react插件测试utils createElement出现问题



我正在将代码从现在已弃用的react/addons包更新为react-addon-test-utils包。我使用jsdom并注入一个文档和窗口元素,如下所示。

import jsdom from 'jsdom';
import chai from 'chai';
import chaiImmutable from 'chai-immutable';
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
const win = doc.defaultView;
global.document = doc;
global.window = win;
Object.keys(window).forEach((key) => {
  if (!(key in global)) {
    global[key] = window[key];
  }
});
chai.use(chaiImmutable);

这是我的单元测试

import {expect} from 'chai';
import ErrorBlock from '../src/Controls/ErrorBlock';
import React from 'react-addons-test-utils';
const {renderIntoDocument, scryRenderedDOMComponentsWithClass, Simulate}
  = React;
describe('ErrorBlock', () => {
  it('renders properly', () => {
    const id = 'test';
    const message = 'my message';
    const alertStyle = "alert-danger";
    const component = renderIntoDocument(
      <ErrorBlock id={id} message={message} alertStyle={alertStyle} />
    );
    const spanEntry = scryRenderedDOMComponentsWithClass(component, id + 'AlertMessage');
    expect (spanEntry.length).to.equal(1);
  });
});

我正在为一些项目中使用的React UI模块进行单元测试设置。然而,当我切换到新的react addons测试utils包时,我会得到以下错误:

TypeError: _reactAddonsTestUtils2.default.createElement is not a function

此错误发生在我定义组件的行上。

为什么只有在使用新软件包时才会出现此错误?

可能已经晚了,但我认为这是我们的解决方案。让更改自:

const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
const win = doc.defaultView;
global.document = doc;
global.window = win;

至:

const doc = new jsdom.JSDOM('<!doctype html><html><body></body></html>');
const win = doc.window;
global.document = win.document;
global.window = win;

它将修复我们的所有问题

最新更新