使用Jest + Enzyme + React不会指出错误的正确行

时间:2018-09-25 13:17:24

标签: reactjs jestjs enzyme

当我在测试中出现错误时(例如,我使用的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可以指出正确的行吗?我不确定为什么会发生此调试问题。

1 个答案:

答案 0 :(得分:0)

看看完整的错误。我同意错误始于

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