我试图让 Jest 在 Visual Studio 代码中正常运行。虽然我可以毫无问题地调试 Typescript,但我无法使用 Jest 找到包含函数的正确行。
我有一个导入我的模块的测试文件:
import { Validate } from "./validate";
describe('Test the Validate', () => {
it('should return true', () => {
expect(Validate('abc')).toBeTruthy();
});
it('should return false', () => {
expect(Validate('cba')).toBeFalsy();
});
});
验证.ts:
export function Validate(Text:string):boolean {
if (Text === 'abc') {
return true;
}
return false;
}
我在该行上放置了一个断点expect(Validate('abc')).toBeTruthy();,这似乎确实导致代码停止。当我按 F11 进入该函数时,我确实转到了源文件,但我的光标位于文件末尾。我似乎可以与 F10 一起进行调试,但我没有看到正确的源代码行。
我已添加sourceMaps: true,到我的 launch.json 中。
启动.json
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"sourceMaps": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}
}
此外,像下面这样使用 ts-jest 并没有改变结果:
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/ts-jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"sourceMaps": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
}