我正在使用 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/