- 环境准备
package.json
{
"name": "ts-jest",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@types/jest": "^26.0.14",
"@types/node": "^14.11.10",
"jest": "^26.5.3",
"ts-jest": "^26.4.1",
"typescript": "3.9.7"
}
}
- 创建 jest.config.js
yarn ts-jest config:init
- sum.ts
module.exports = function(a: number, b: number): number {
return a + b;
}
- sum.test.ts
const sum = require('./sum');
test('sum', () => {
expect(sum(1,2)).toBe(3);
});
- 执行测试
yarn jest
image.png
网友评论