安装
npm install --save-dev @types/jest
npm install --save-dev ts-jest
配置
tsconfig.json
{"compilerOptions": {"types": ["jest"]}
}
jest.config.js
module.exports = {preset: 'ts-jest',testEnvironment: 'node',
};
使用
// add.js
function add(a, b) {return a + b;
}module.exports = add;
测试文件
// add.test.js
const add = require('./add');describe('add function', () => {let result;beforeEach(() => {result = add(1, 2);});test('adds 1 + 2 to equal 3', () => {expect(result).toBe(3);});
});