【问题标题】:why it is showing error "Cannot read property 'have' of undefined"?为什么它显示错误“无法读取未定义的属性'有'”?
【发布时间】:2018-09-23 13:15:56
【问题描述】:

我正在尝试使用 enzyme 测试我的组件。您能告诉我为什么我收到错误 "Cannot read property 'have' of undefined " 这是我的代码 https://codesandbox.io/s/oq7kwzrnj5

import React from "react";
import { shallow, mount } from "enzyme";

import Counter from "./Counter";

describe("counter", () => {
  it("test truty", () => {
    const wrapper = shallow(<Counter />);
    expect(wrapper.find("p")).to.have.length(1);
  });
});

【问题讨论】:

  • 你使用什么测试框架?
  • 开个玩笑,没看到例子

标签: reactjs react-redux jestjs enzyme


【解决方案1】:

在代码演示中,您使用的是 enzyme 3.x,而在该版本中 have 不再可用。

尝试使用 2.x. 版本,或将 3.x 的预期子句更改为:

expect(wrapper.find("p")).toHaveLength(1);

参考:Enzyme migration guide from 2 to 3

【讨论】:

    【解决方案2】:

    当您使用 jest 时,该行应如下所示:

    expect(wrapper.find("p").length).toBe(1);
    

    expect(wrapper.find('p').exists()).toBe(true)
    

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 2015-11-21
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2019-03-09
      • 2020-10-15
      相关资源
      最近更新 更多