3.24:jest拿不到异步axios获取的数据,拿不到mock的数据
mock的函数返回值不影响真正的页面效果啊!
明天尝试:创建测试函数,直接更改返回值,检查是否能够改变页面效果。
怎么测试useHistory,验证是否跳转了页面?
https://blog.logrocket.com/a-quick-guide-to-testing-react-hooks-fa584c415407/
https://www.toptal.com/react/testing-react-hooks-tutorial
https://chanonroy.medium.com/mocking-hooks-for-testing-with-jest-and-react-testing-library-d34505616d12
https://kentcdodds.com/blog/how-to-test-custom-react-hooks
https://www.newline.co/@jamesfulford/testing-custom-react-hooks-with-jest–8372a502
https://css-tricks.com/testing-react-hooks-with-enzyme-and-react-testing-library/
https://www.npmjs.com/package/jest-react-hooks-shallow
https://itnext.io/testing-components-built-using-react-hooks-with-jest-enzyme-edb87d703756
https://stackoverflow.com/questions/57025753/how-to-set-initial-state-for-usestate-hook-in-jest-and-enzyme
https://www.richardkotze.com/coding/mocking-react-hooks-unit-testing-jest
jest备忘
https://cloud.tencent.com/developer/article/1531162
https://jestjs.io/zh-Hans/docs/asynchronous
https://itxiaohao.github.io/passages/test-learn-jest-mock2/#mock-axioshttps://juejin.cn/post/6844903939289120775
https://www.dazhuanlan.com/2020/01/18/5e2318186c079/
https://testerhome.com/topics/15149
https://jestjs.io/docs/mock-function-api
https://www.jianshu.com/p/1f9f326d4636
https://www.npmjs.com/package/jest-mock-axios
nmp install jest -D安装到开发环境
在package.json文件里把scripts : test配置为jest,后面加上–watchAll就可以有新用例就自动运行测试
npx jest -init生成基本配置
打开jest.config.js文件
npx jest --coverage生成代码覆盖率报告在coverage文件夹下
匹配器(加.not.可以取反)
toBe严格相等
toEqual值相等
toBeNull等于null
tobeUndifined/toBeDefined 是否定义了
toBeTruthy是否为真
安装@babel/core@7.4.5 @babel/preset-env@7,4,5 --dev让jest支持es6
准备babel配置文件.babelrc
{
"presets":[
[
"@babel/preset-env",{
"targets":{
"node":"current"