【问题标题】:Enzyme test error with redux hooks: "could not find react-redux context value; please ensure the component is wrapped in a <Provider>"带有 redux 钩子的酶测试错误:“找不到 react-redux 上下文值;请确保组件包装在 <Provider> 中”
【发布时间】:2021-03-12 21:13:33
【问题描述】:

我正在重构我的项目中的一些代码,将类组件传递给功能组件。在我的测试中出现了一些错误。

我有一个名为“MenuWeb”的组件,我现在正在使用 redux 钩子。

这是测试代码

describe('Testing MenuWeb', () => {
  it('should render Menu Web correctly', () => {
    const wrapper = shallow(<MenuWeb store={store} />)
    expect(wrapper).toMatchSnapshot()
  })

这就是我得到的错误:

我知道酶不支持钩子,但我不知道如何使用 now react 测试库重构这个测试。如消息所述,我尝试使用提供者进行包装,但它不起作用,或者我做错了什么。

【问题讨论】:

  • 查看这个,“React — 使用浅渲染测试 Redux 组件”,Scott Carmichael link.medium.com/wq5yEYGxQbb
  • 成功了!如果你想回答我可以投票给你,我很乐意这样做:)
  • 当然!谢谢,我将其更新为答案。

标签: reactjs redux enzyme react-testing-library


【解决方案1】:

如果您的组件被包装在 Provider 服务中,您将无法利用浅层渲染,因为它被应用于该服务

参考 - Scott Carmichael https://link.medium.com/wq5yEYGxQbb 撰写的“React — 使用浅层渲染测试 Redux 组件”

【讨论】:

    猜你喜欢
    • 2023-02-24
    • 2020-09-08
    • 2021-05-15
    • 2020-06-05
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多