带有挂载的反应酶测试用例给出"不变违规:超出最大更新深度"问题



全部,

我的应用程序运行良好,在浏览器中运行应用程序时没有看到这个问题。我正在尝试用酶"mount"测试一个非常简单的组件。当我这样做的时候,它只是进入一个大循环,并以这个错误结束。在日志中,会出现类似的内容。

Uncaught [TypeError: Cannot set property event of #<Window> which has only a getter]......
......keep printing this error with same trace for 100 times .....
and finally-
Invariant Violation: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

我的测试用例可以很好地使用浅装载,但使用浅装载时,我只能调用由子组件元素触发的任何回调函数。我觉得我在用它编写无用的测试用例。既然我不是在这里与各州打交道,为什么我会看到这个错误?

Versions-
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.6",
"react": "16.8.2",

简单组件-

const GenericDropDown = ({menuOptions, style, handleChange, dropDownID}) => (
<div style={style}>
<Select
value={dropDownID}
onChange={handleChange}
autoWidth
>
{menuOptions.map((menuOption, index) => {
return (index === 0 ? <MenuItem key={index} value={0} disabled>{menuOption}</MenuItem>
: <MenuItem key={index} value={index}>{menuOption}</MenuItem>);
}
)}
</Select>
</div>);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

测试用例-

import React from 'react';
import MenuItem from "@material-ui/core/MenuItem";
describe('<GenericDropDown>', function () {
it('should render 4 menu options', function () {
const handleAdvancedSearchCallBack = (event) => {
};
const wrapper = mount(<GenericDropDown menuOptions={EXTRA_ID_TYPES} style={{marginLeft: 10, marginRight: 10}} handleChange={handleAdvancedSearchCallBack} dropDownID={2} />);
expect(wrapper.find(MenuItem)).to.have.length(4);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

所以,最终发现我使用的JSDOM版本(16.0.0+(导致了这个问题。所有旧版本的JSDOM都运行良好。

最新更新