首页 > 解决方案 > 使用 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 能指出正确的行吗?我不确定为什么会发生此调试问题。

标签: reactjsjestjsenzyme

解决方案


看看完整的错误。我同意错误以

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


推荐阅读