我试图让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();
});
});
validate.ts:
export function Validate(Text:string):boolean {
if (Text === 'abc') {
return true;
}
return false;
}
我在expect(Validate('abc')).toBeTruthy();
行上放置了一个断点,这似乎确实导致代码停止了。当我按F11键进入该功能时,我确实转到了源文件,但我的光标位于该文件的末尾。我似乎可以随F10一起进行调试,但看不到正确的源代码行。
我已经将sourceMaps: true,
添加到了launch.json中。
launch.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",
}