Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 单元测试 #92

Open
hsipeng opened this issue Sep 16, 2019 · 0 comments
Open

React 单元测试 #92

hsipeng opened this issue Sep 16, 2019 · 0 comments

Comments

@hsipeng
Copy link
Owner

hsipeng commented Sep 16, 2019

为何必须做单元测试

  • 单元测试对于任何 React 项目(及其他任何项目)来说都是必须的
  • 我们需要自动化的测试套件,根本目标是支持随时随地的代码调整、持续改进,从而提升团队响应力
  • 使用 TDD 开发是得到好的单元测试的唯一途径
  • 好的单元测试具备几大特征:不关注内部实现、只测一条分支、表达力极强、不包含逻辑、运行速度快
  • 单元测试也有测试策略:在 React 的典型架构下,一个典型的测试策略为:
    • reducer、selector 层的逻辑代码要求 100% 覆盖
    • saga(副作用)层:是否拿到了正确的参数、是否调用了正确的 API、是否保存了正确的数据、业务逻辑、异常逻辑五个层面要求 100% 覆盖
    • action 层选择性覆盖:可不测
    • utils 层的纯函数要求 100% 覆盖
    • 组件层:
    • 分支渲染逻辑必测、事件、交互调用要求 100% 覆盖;
    • @connect 过的高阶组件不测
    • 纯 UI 一般不测
  • 其他高级技巧:定制测试工具(jest.extend)、参数化测试等

参考资料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant