reactjs - Jest - 在 React 中测试模态会出错

标签 reactjs jestjs react-test-renderer

我正在使用 react-test-renderer用 Jest 来测试 React 组件。但是,如果我像这样测试 react-mui 模态对话框:

describe('Dashboard', function () {
  let dashboard;
  beforeEach(async () => {
    testRenderer = TestRenderer.create(<MemoryRouter><Route component={Dashboard} /></MemoryRouter>);
    dashboard = testRenderer.root.findByType(Dashboard);
    await waitForExpect(() => expect(dashboard.instance.state.hasLoaded).toBeTruthy());
  });


  it('opens dialog on clicking the new class', async () => {
    const button = testRenderer.root.findByType(Button);
    expect(dashboard.instance.state.showDialog).toBeFalsy();
    button.props.onClick();
    expect(dashboard.instance.state.showDialog).toBeTruthy();
  });
});

但是,然后我收到一个错误:

Error: Failed: "Error: Uncaught '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.%s'



我应该如何测试然后响应门户以使此测试有效?

最佳答案

尝试将其放入您的测试中:

beforeAll(() => {
    ReactDOM.createPortal = jest.fn((element, node) => {
        return element
    })
});

关于reactjs - Jest - 在 React 中测试模态会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56257998/

相关文章:

javascript - TypeError : this. props.propName.map 不是函数 react js

javascript - Jest 遇到了一个意想不到的 token React

reactjs - 测试使用 Hooks 获取数据的 React 组件

reactjs - 如何将 WebM 格式的音频输入传递给 Google Speech API

javascript - 如何在 React Native 中简单地更改触摸时的背景颜色?

javascript - 为什么我的 "mapDispatchToProps"找不到我的 Action 函数?

javascript - 使用 jest.mock 传递超出范围的变量

reactjs - Jest 测试 Babel 错误 : Plugin/Preset files are not allowed to export objects

reactjs - 如何在我的 react 测试中模拟状态和数据的值