在反应测试图和打字稿中,适合Renderhook的合适类型是什么?
我想用react-testing-library
测试一个自定义钩子,因此,我将此代码添加到each之前:
let renderedHook ;
beforeEach(() => {
renderedHook = renderHook(() => useFetch());
});
test('.....', () => {
expect(renderedHook.current.data).toBe(1);
});
上面的代码效果很好!但是我正在使用TypeScript,在这种情况下,让Renderedhook 适合的类型是什么?
I wanna test a custom hook with react-testing-library
therefore, I add this code into beforeEach:
let renderedHook ;
beforeEach(() => {
renderedHook = renderHook(() => useFetch());
});
test('.....', () => {
expect(renderedHook.current.data).toBe(1);
});
the above code works well! but I'm using TypeScript, what's the suitable type for let renderedHook
in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的IDE或编辑器支持“转到定义”功能,则可以检查
renderhook
的TS类型。renderhook
的返回类型是 renderhookresulteg
软件包版本:
If your IDE or editor supports the "Go to Definition" feature, you can check the TS type of
renderHook
.The return type of
renderHook
is RenderHookResultE.g.
package version:
尽管问题是关于合适的类型,但任何想要在测试中功能之前的人都需要从IDE支持中找到合适类型的人,但是您可以创建一个函数,例如设置
,您可以在每个测试中使用它,而无需考虑合适的测试,它将也显示Intellisense。设置的使用如下所示
Though the Question is about suitable types, anyone who wants beforeEach functionality in tests and don't want to find suitable types from IDE support, you can create a function like setup
Now you can use it every test without thinking about suitable test and it will show intellisense as well. The usage of the setup is shown below