安装ts-test
Npm install -D ts-jest @types/jest
配置jest.config.js
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
// 源码搜索路径
modulePaths:[
"<rootDir>/src/",
"<rootDir>/src/ManageHome/",
"<rootDir>/src/lib/",
"<rootDir>/src/Common/"
]
}
配置vscode的launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"protocol": "inspector",
"name": "Jest Debug",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"stopOnEntry": false,
"args": ["--runInBand", "--env=jsdom", "${fileBasename}"],
"runtimeArgs": [
"--inspect-brk"
],
"cwd": "${workspaceRoot}",
"sourceMaps": true,
"console": "integratedTerminal"
}
]
}
到测试文件按F5调试