jest 安装与使用
安装
sh
npm i --save-dev jest
package.json
json
"scripts": {
"test": "jest"
}
使用例子
plaintext
.
├── src
│ └── sum.js
├── test
│ └── sum.test.js
├── .gitignore
├── jest.config.js
├── README.md
└── package.json
sum.js
javascript
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js
javascript
const sum = require("../src/sum");
test("1 + 2 = 3", () => {
expect(sum(1, 2)).toBe(3);
});
sh
npm test
sh
> jest
PASS test/sum.test.js
✓ 1 + 2 = 3 (5 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.2 s
Ran all test suites.