Jest 是由 Facebook 出品的 JavaScript 测试框架,官方声称通过它可以完成“令人愉快的 JavaScript 测试”。
在 TDD(Test-Driven Development)开发方法中,编写测试代码尤为重要,Jest 让这一切变得更佳的简单。
- yarn:
yarn add --dev jest
- or npm:
npm install --save-dev jest
function hello() {
return 'Hello world!';
}
module.exports = hello;
const hello = require('./hello');
test('hello', () => {
expect(hello()).toBe('Hello world!');
});
{
//...
"scripts": {
"test": "jest",
"coverage": "jest --coverage"
}
//...
}
PASS ./hello.test.js
jest --coverage
以上就是你的第一个 Jest 单元测试。
通过安装 babel-jest
和 regenerator-runtime
可以让 Jest 支持 ES6 的新语法:
yarn add --dev babel-jest babel-core regenerator-runtime
{
"presets": [
"es2015",
"stage-2"
]
}
更多内容参考相关文档:https://facebook.github.io