reactjs - 使用 Jest+Enzyme+React 不会指出错误的正确线
问题描述
当我在测试中出现错误时(假设我使用了 Jest 不存在的函数),Jest 不会指出正确的错误行。所以如果我使用一个不存在的函数:
expect(wrapper.find(Link)).to.have(...)
它显示:
TypeError: Cannot read property 'have' of undefined
> 1 | import React from 'react';
| ^
2 | import { shallow, mount, render } from 'enzyme';
3 | import Logo from '../header/logo.js';
4 | import { BrowserRouter as Router, Link } from 'react-router-dom';
at Object.<anonymous> (src/__tests__/scenes/header/logo-test.js:1:596)
所以可以在我的文件中挖掘找到正确的行,但我希望 Jest 能指出正确的行吗?我不确定为什么会发生此调试问题。
解决方案
看看完整的错误。我同意错误以
Cannot read property 'have' of undefined
但是错误日志如下所示,您可以清楚地看到问题出在哪一行。如果你期待别的东西,请纠正我。
10 | const wrapper = shallow(<Field />);
11 | console.log(wrapper.debug());
12 | expect(wrapper.contains(<p id="hello" />)).toBeTruthy();
> 13 | expect(wrapper.find("p").prop("id")).to.have("hello");
14 | });
15 | });
同样,我创建了一个沙箱,其中出现了此类错误,我可以清楚地看到上述行为https://codesandbox.io/s/7z43vjlx
推荐阅读
- flutter - 如何在颤动的水平列表视图中将屏幕上的选定项目居中?
- reactjs - Nextjs 在异步函数调用后无法设置状态
- angular - Prevent Google Analytics code usage on other domain
- python - GPyTorch Multi-class Classification ; What is num_features in "gpytorch.likelihoods.SoftmaxLikelihood"?
- javascript - How concatenate string with value in recharts - ReactJS
- spring-boot - keystore password incorrect in Spring boot SSL
- node.js - NextJs - Function in API folder in pages gives 'res.status is not a function' error
- datatables - Manipulate column in datatable row
- laravel - Laravel Join returns empty array
- swagger - How can I setup a path to use a parameter reference and override the name property?