Jest是Facebook推出的一款單元測試工具。javascript
npm install --save-dev jest ts-jest @types/jest
在package.json中添加腳本:java
「test」: "jest"
生成Jest配置文件(生成jest.config.js)npm
npx ts-jest config:init
先簡單編寫一個ts文件json
function adds(a: number, b: number) { return a+b; } function sub(a: number, b: number) { return a-b; } export { adds, sub };
編寫測試用例:工具
import {adds,sub} from './math' test('adds: 1 + 1 = 2',()=>{ expect(adds(1,1)).toBe(2); }); test("sub: 1 - 2 = -1",()=>{ expect(sub(1,2)).toBe(-1); });
執行測試腳本:單元測試
npm run test
使用ts-jest的好處是,可以在測試用例中進行類型檢查。測試