React中的酶测试无效

时间:2018-06-27 07:42:15

标签: reactjs enzyme

我正在Create-React-App中使用酶测试。在shallow渲染中可以正常工作,但是mount渲染会抛出错误:

  

TypeError:无法读取未定义的属性“收藏夹”

测试文件如下:

import React, { Component } from "react";
import configureMockStore from "redux-mock-store";
import { shallow, mount } from "enzyme";
import { Provider } from "react-redux";
import Favorites from "../Landing/Favorites";

const mockStore = configureMockStore();
const store = mockStore({});

function setup() {
  const props = {
    favorites: 42
  };
  const wrapper = mount(
    <Provider store={store}>
      <Favorites {...props} />
    </Provider>
  );
  return {
    props,
    wrapper
  };
}

describe("Favorites component", () => {
  const { wrapper } = setup();
  it("should render list of favorites cards", () => {
    expect(wrapper.prop("favorites")).toEqual(42);
  });
});

为什么会这样?

1 个答案:

答案 0 :(得分:0)

.prop的安装方式和安装方式都不同。您可以检查文档。 http://airbnb.io/enzyme/docs/api/ReactWrapper/prop.html http://airbnb.io/enzyme/docs/api/ShallowWrapper/prop.html

使用mount时,可以直接呈现“收藏夹”组件。 mount(<Favorites {...props} />)