第一步安装包: yarn add -D typescript jest ts-jest @types/jest 第二步初始化项目相关配置文件:生成tsconfig.json: tsc --init 创建jest.config.js: module.exports = { transform: { '^.+\\.tsx?$': 'ts-jest', }, testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], } 目录结构: base.spec.ts: import { sampleFunction } from "../src"; test('adds 1 + 2 to equal 3', () => { expect(sampleFunction("hello")).toBe("hellohello"); }); 结果:
第一步安装包:
第二步初始化项目相关配置文件:
生成tsconfig.json:
创建jest.config.js:
目录结构:
base.spec.ts:
结果: