6

当尝试使用 jest lib 为这样的 Modal 组件编写一个简单的测试时

import { Modal } from '@material-ui/core';
import React from 'react';
import TestRenderer from 'react-test-renderer';

describe('Material Modal test', () => {
  it('It should render', () => {
    const testRenderer = TestRenderer.create(
      <Modal open={true}>
        <div>Test</div>
      </Modal>
    );

    console.log(testRenderer.toJSON());
  });
});

我收到一个错误:

 console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:120
      Warning: An invalid container has been provided. This may indicate that another renderer is being used in addition to the test renderer. (For example, ReactDOM.createPortal inside of a ReactTestRenderer tree.) This is not supported.
          in div (created by ForwardRef(Modal))
          in ForwardRef(Portal) (created by ForwardRef(Modal))
          in ForwardRef(Modal) (at spinnerWaitingWindow.spec.tsx:10)
    console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Uncaught [TypeError: parentInstance.children.indexOf is not a function]

有没有人知道为什么会发生这种情况以及如何设置测试库?

4

0 回答 0