React Unit Test - 学习/实践

1.应用场景

主要用于如何编写react单元测试, 构建健壮前端项目.

这里主要使用 Jest/React test library 进行react单元测试.

2.学习/操作

1.文档阅读

https://zh-hans.reactjs.org/docs/testing.html   // react测试 -- 有些懵懵懂懂

https://reactjs.org/docs/test-utils.html   // 官网提供的讲解与一些例子

https://jestjs.io/docs/en/getting-started  // Jest 官网

http://wiki.c2.com/?ArrangeActAssert

https://testing-library.com/docs/react-testing-library/intro/   // 测试用例代码库 -- 推荐

https://www.robinwieruch.de/react-testing-library   // How to use React Testing Library Tutorial  -- 推荐阅读 --- 后续添加翻译文本

https://jestjs.io/docs/en/tutorial-react   // 有讲解到如何编写单元测试

https://github.com/facebook/jest/tree/master/examples/snapshot   // Jest  example

https://github.com/facebook/jest/tree/master/examples   // 多看学习下大神的实践

https://github.com/facebook/jest/blob/master/examples/snapshot/__tests__/link.react.test.js   // 详细的代码例子可供参考

http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html   // 可类比思考总结

视频资源

https://www.bilibili.com/video/BV1Pv411q7Hz/?spm_id_from=333.788.videocard.2   // 英文, 且没有字幕

中文视频/文档很少

2.整理输出

大概思路:

安装jest库进行react application的unit test, 也可以使用react-test-library进行react unit test的编写.

实践文档参考来源:

https://zh-hans.reactjs.org/docs/testing-recipes.html 

解读:

即可不使用react-testing-library, 直接是同jest进行编写unit test. 但是, 可能写起来会比较冗余, 所以又建议使用二次封装库.

https://testing-library.com/docs/react-testing-library/intro

代码实践: 

使用create-react-app脚手架创建react app, 并在其中实践react unit test的编写运行测试.

https://github.com/ningxiaofa/react-unit-test-learning

后续补充

...

3.问题/补充

1.突然发现, 有时候, 编写unit test不是一件很容易的事, 特别是集成使用到比较多的东西~~~

// 知识欠缺

2. window下cmd执行 单元测试:  yarn test 

一直报错, 原因Windows下cmd存在环境问题, 需要在docker容器中执行单元测试

参见:  https://blog.csdn.net/william_n/article/details/110876725

4.参考

https://zh-hans.reactjs.org/docs/testing.html   // react测试

https://testing-library.com/docs/react-testing-library/intro/   // 测试用例代码库 -- 推荐

后续补充

...

react testing library tutorial

猜你喜欢

转载自blog.csdn.net/william_n/article/details/110818678
今日推荐