令人愉快的測試 Jest
標(biāo)簽:
JavaScript
Jest 是由 Facebook 出品的 JavaScript 测试框架,官方声称通过它可以完成“令人愉快的 JavaScript 测试”。
在 TDD(Test-Driven Development)开发方法中,编写测试代码尤为重要,Jest 让这一切变得更佳的简单。
快速入门
安装:
yarn:
yarn add --dev jest
or npm:
npm install --save-dev jest
被测试的模块 hello.js
:
function hello() { return 'Hello world!'; }module.exports = hello;
创建测试文件 hello.test.js
:
const hello = require('./hello'); test('hello', () => { expect(hello()).toBe('Hello world!'); });
配置 package.json
{ //... "scripts": { "test": "jest", "coverage": "jest --coverage" } //...}
运行 yarn test
开始测试:
PASS ./hello.test.js
生成覆盖率:
jest --coverage
以上就是你的第一个 Jest 单元测试。
Babel
通过安装 babel-jest
和 regenerator-runtime
可以让 Jest 支持 ES6 的新语法:
yarn add --dev babel-jest babel-core regenerator-runtime
配置 .babelrc
{ "presets": [ "es2015", "stage-2" ] }
點擊查看更多內(nèi)容
為 TA 點贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦