为什么它显示错误“无法读取属性'有'未定义'?

时间:2018-04-13 08:31:10

标签: reactjs react-redux jestjs enzyme

我正在尝试使用enzyme来测试我的组件。你能否告诉我为什么我会收到错误“无法读取属性'有'未定义 “ 这是我的代码 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);
  });
});

enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

在代码演示中,您正在使用酶3.x ,并且在该版本中 已不再可用。

尝试使用 2.x。版本,或更改 3.x 的预期条款,例如:

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

参考:Enzyme migration guide from 2 to 3