开始使用 react-hooks-testing-library 在上一篇教程中,我们手工编写了非常原始的 React Hooks 测试代码。 所幸,由于测试 React Hooks 的需求非常普遍,因此就有了测试 Hooks 的神器:react-hooks-testing-library。 让我们先安装 react-hooks-testing-library: npm install @testing-library/react-hooks react-hooks-testing-library 提示 react-hooks-testing-library 还提供了一些工具函数用来辅助异步钩子的测试,可参考官方文档的 Async Utilities 部分。 小结 在这篇文章中,我们体验了强大的 react-hooks-testing-library,先后测试了同步和异步的钩子,最后还结合 Redux 来测了一波。
在本文中,我们研究了如何使用 react-hooks-testing-library[2] 处理它。 这就是我们使用 react-hooks-testing-library[4] 的原因,我们将在下一篇教程里讲解如何更加舒适的测试 React Hooks 的方法,敬请期待! 参考资料 [1] simulate: https://enzymejs.github.io/enzyme/docs/api/ShallowWrapper/simulate.html [2] react-hooks-testing-library javascript-design-patterns-facade-react-hooks/ [3] React文档: https://reactjs.org/docs/hooks-overview.html [4] react-hooks-testing-library
React hooks testing library React-hooks-testing-library(https://github.com/testing-library/react-hooks-testing-library 为了简化开发者测试hook的流程,React社区有人开发了这个叫做react-hooks-testing-library的库来允许我们像测试普通函数一样测试我们定义的hook,这个库其实背后也是将我们定义的 安装 直接把react-hooks-testing-library作为我们的项目devDependencies: yarn add -D @testing-library/react-hooks 注意 :要使用react-hooks-testing-library我们要确保我们安装了16.9.0版本及其以上的react和react-test-renderer: yarn add react@^16.9.0 总结 在本篇文章中我给大家介绍了什么叫做单元测试,为什么我们需要在自己的项目里面引入单元测试以及教大家如何使用Jest和react-hooks-testing-library来测试我们自定义的hook。
Testing Using React-hooks-testing-library 7. Wrapping Up 8. FAQ 8.1. CSS 不能被正常解析? 8.2. Testing Using React-hooks-testing-library React-hooks-testing-library does everything we have talked I would recommend you use React-hooks-testing-library since it’s complete, and I haven’t run into significant
一、技术选型: Jest + Enzyme + react-hooks-testing-library 1.1 jest Jest是FaceBook出品的前端测试框架,适合用于React和React }); 十、React-Hooks 单元测试 在React Native v0.59版本以后,RN也支持了React Hooks的开发,由于Enzyme对于Hooks的测试支持不理想,我们专门引入了react-hooks-testing-library
testing-library下面有一个单独的库,叫react-hooks-testing-library,让你无需通过UI行为层面,而是直接以hook的方式去测试它们。 【插入一段:尽管存在react-hooks-testing-library这样的工具,但像SWR这样优秀的三方库,在用testing-library为自己的hook API做测试的时候,依然选择在UI层面进行
Jest 基础知识 使用 Enzyme 测试 React 组件 组件 Props 测试、mount 函数渲染、快照测试 Mock 模拟 API 调用、模拟 React 组件交互 使用 react-hooks-testing-library
令人愉快的JavaScript测试框架 enzyme - 针对React的JavaScript测试实用程序 react-testing-library - 简单而完整的React DOM测试实用程序 react-hooks-testing-library