Reactjs 用Jest和Enzyme对AntD模型进行单元测试

Reactjs 用Jest和Enzyme对AntD模型进行单元测试,reactjs,jestjs,enzyme,antd,Reactjs,Jestjs,Enzyme,Antd,我试图使用Jest和Enezym在React中对AntD modal进行单元测试,它给了我以下错误: 还有一个 这也是: 不变冲突:对象作为React子对象无效(找到:具有键{destroy,update}的对象)。如果要呈现子对象集合,请改用数组 这是我创建的模式 import propTypes from 'prop-types'; import { Modal } from 'antd'; const SuccessModal = props => { const { titl

我试图使用Jest和Enezym在React中对AntD modal进行单元测试,它给了我以下错误:

还有一个

这也是: 不变冲突:对象作为React子对象无效(找到:具有键{destroy,update}的对象)。如果要呈现子对象集合,请改用数组

这是我创建的模式

import propTypes from 'prop-types';
import { Modal } from 'antd';

const SuccessModal = props => {
const { title, content } = props;
return Modal.success({
title,
content,
});
   };

SuccessModal.propTypes = {
title: propTypes.string.isRequired,
content: propTypes.string.isRequired,
  };
  export default SuccessModal;
下面是我的单元测试

import React from 'react';
import { shallow } from 'enzyme';
import SuccessModal from './index';

describe('SuccessModal', () => {
  it('should render the component correctly in ', () => {
    shallow(<SuccessModal title="time to succeed" content="sucess content        success content" />);
  });
  it('check the props values', () => {
    const props = {
      title: 'this is a success title',
      content: 'sucess content sucess content',
    };

    const renderedComponent = shallow(<SuccessModal {...props} />);
    expect(renderedComponent.prop('title')).toBe('this is a success title');
    expect(renderedComponent.prop('content')).toBe('sucess content sucess content');
  });
});
从“React”导入React;
从“酶”导入{shall};
从“/index”导入成功模式;
描述('SuccessModal',()=>{
它('应该在',()=>{
浅();
});
它('检查道具值',()=>{
常量道具={
标题:“这是一个成功的标题”,
内容:“成功内容成功内容”,
};
const renderedComponent=shallow();
expect(renderedComponent.prop('title')).toBe('this is a success title');
expect(renderedComponent.prop('content')).toBe('success content success content');
});
});
我也试过这种方法

  expect(

        (      
    <SuccessModal title="this is a success title" content="sucess content sucess content" />
  ).exists(),
).toEqual(true);
expect((   <SuccessModal title="this is a success title" content="sucess content sucess content" />).text()).toContain('this is a success title');
expect(
(      
).exists(),
).toEqual(正确);
expect(().text()).toContain('这是一个成功的标题');

非常感谢您提供的任何帮助,如果需要更多的澄清,请在评论中告诉我,因为这个问题是在大约2年前提出的,那么我想使用的是antd 3.x

Modal.success返回一个对象{destroy,update},因此出现相应的错误: 不变冲突:对象作为React子对象无效(找到:具有键{destroy,update}的对象)

在这里,您需要使用声明性方法:

const SuccessModal = props => {
    const {title, content} = props;
    return (
        <Modal title={title}> 
            {content}
        </Modal>
    );
};
当作为函数调用时,аmodal被创建为
主体
标记内的一个单独的
div
。因此,要检查调用的正确性,必须检查DOM

expect(document.documentElement.outerHTML).toContain(props.title);

你找到解决这个问题的办法了吗?
expect(document.documentElement.outerHTML).toContain(props.title);