我们使用酶和Jest进行测试。 在我们的代码库中,更新到了最新版本的react-redux,所有连接的组件测试用例都开始失败(版本6)。 使用
import { createMockStore } from 'redux-test-utils';
创建商店
适用于旧版本的测试用例:
const wrapper = shallow(<SomeConnectedComponent />, {context: { store }});
无法给出错误消息
永久违反:在“ Connect(SomeConnectedComponent)”的上下文中找不到“存储”。
阅读了几篇文章,得到了使用提供程序包装器进行安装和包装的建议
const wrapper = mount(<Provider store={store}><SomeConnectedComponent /></Provider>);
以上代码可以工作,但是我希望它可以与吞咽一起用于单元测试。
编辑:
const wrapper = shallow(<Provider store={store}>
<SomeConnectedComponent />
</Provider>)
上面的代码返回空的shallowWraper对象。
使用react-redux版本> 6吞咽连接的组件的最佳方法是什么
答案 0 :(得分:3)
shallow在redux 7上确实可以使用,但是实现已更改。 所以不是
def Dealer():
print("thread started")
return
您现在要做
const wrapper = shallow(<Provider store={store <SomeConnectedComponent/></Provider>)
不再需要包装提供者,并且可以防止不必要的潜水。然后您就可以像这样遍历浅层包装器了:
const wrapper = shallow(<SomeConnectedComponent store={store}/>)
答案 1 :(得分:2)
浅表不适用于最新版本的react-redux。 从6.x版开始,它会导致上述问题。
我发现最好的解决方案是使用旧版本的react-redux进行测试, 以及较新的实际代码。
1)将旧版本添加为开发依赖项。因为那里有较新版本的react-redux,所以您将需要使用别名。可以是任何版本5.x。它将安装“ react-redux-test”
yarn add react-redux-test@npm:react-redux@5.0.6 -D
2)在_模拟_文件夹下,创建一个新文件react-redux.js并从内部导出较旧的版本
export * from 'react-redux-test';
默认情况下,该模拟将在每个测试用例文件中使用,因此旧的测试代码不再中断。
但是,如果您想使用新的react-redux库进行测试,则可以使用
jest.unmock('react-redux')
在新的测试文件上方。
升级后,有数百个测试失败,这种方法对我有用,因为我也想在新组件中使用Hooks Api。
通过这种方式,您可以使用新库的功能,直到酶/ react-redux修复为止。