從0到1學(xué)習(xí)前端單元測(cè)試jest框架(一)
此步驟建立在你搭建好node環(huán)境基礎(chǔ)之下
第一步 安裝 jest?
使用npm install jest --save-dev 或者 yarn add jest --save-dev??
第二步 初始化 jest.config.js? 使用??npx jest --init? 這個(gè)命令可以在當(dāng)前路徑下初始化jest.config.js
第三步在node環(huán)境或者jsDOM(瀏覽器環(huán)境)下增加對(duì)ES6代碼的支持
yarn add jest-babel @babel/core @babel/preset-env
下載安裝完上面的依賴以后創(chuàng)建一個(gè).babelrc 的文件 里面寫上
第四步,因?yàn)槲覀円?TypeScript 寫代碼文件和測(cè)試文件,而 Jest 是不能直接執(zhí)行 Typescript 的,所以需要通過 babel 來處理。所以需要再使用yarn進(jìn)行安裝下面的這些依賴
另外一定要安裝 jest-environment-jsdom
不安裝這個(gè)一定會(huì)報(bào)錯(cuò)
會(huì)報(bào)下面的這個(gè)錯(cuò)
Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration?
? Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module.
? Configuration Documentation:
? https://jestjs.io/docs/configuration
As of Jest 28 "jest-environment-jsdom" is no longer shipped by default, make sure to install it separately.
第五步:配置babel.config.js ,內(nèi)容如下
第六步在package.json的scripts下添加?"test":?"jest"
"scripts": { ??"test": "jest"},
添加完這句就可以進(jìn)行單元測(cè)試了