reactjs - enzyme 测试,上下文不渲染元素

标签 reactjs testing jestjs enzyme

我正在使用 enzyme 编写我的测试,在向我的组件(在本例中是 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>

最佳答案

v3.10.0 之前的

enzyme lacked support of React's new Context API .

来自v3.10.0 Changelog :

shallow: Support rendering and dive()ing createContext() providers and consumers (#1966)

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

关于reactjs - enzyme 测试,上下文不渲染元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52830983/

相关文章:

reactjs - 如何使用 Enzyme (Jest) 在 React Native 单元测试中测试 Elements 值

reactjs - TypeError : (0 , _chai.describe) 不是函数

javascript - 根据 API 响应在组件内 React 渲染 PDF 文件

css - react-bootstrap 轮播宽度行为

reactjs - 在 onAuthStateChanged 监听器内分离 Firestore 监听器

reactjs - Jest 在 React 中测试异步图像上传

python - django 如何在客户端测试中设置请求用户

testing - 如何在 Clojure 中进行集成测试?

node.js - 如何用 Jest 模拟 express.Application?

javascript - VS Code断点跳转到其他行