【问题标题】:enzyme test, context not rendering elements酶测试,上下文不渲染元素
【发布时间】:2019-12-05 23:53:26
【问题描述】:

我正在使用酶来编写我的测试,在向我的组件添加上下文之前,在这种情况下是 consumer component,呈现的快照没有显示任何元素,并且每次我添加 dive()shallow()我得到了同样的错误 如何在使用上下文时访问元素

const wrapper = shallow(
    <MyComponent {...initialProps} store={mockStore(initialState)} />,
    {context},
  );

  const contents = wrapper
    .dive() // dive in connect
    .dive() // dive in withLogss
    .dive() // dive in Motion
    .find("MyComponent")
    .dive(); // dive in Host

  expect(contents.context()).toEqual({myBoolean: true});

  it("should render properly", () => {
    expect(contents).toMatchSnapshot();
  });


Result: 

<ContextConsumer>
  <Component />
</ContextConsumer>

【问题讨论】:

  • 您使用的是浅渲染,请尝试使用 mount 代替。

标签: reactjs testing jestjs enzyme


【解决方案1】:

Enzymev3.10.0 lacked support of React's new Context API 之前。

来自v3.10.0 Changelog

shallow:支持渲染和潜水()创建上下文()提供者和消费者(#1966)

确保您使用的是enzyme v3.10.0 或更高版本 enzyme-adapter-react-16 v1.15.1 或更高版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 2020-08-26
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多