首先确保Jest以--inspect-brk模式运行并在launch.json中配置Attach调试,然后通过npm run test:debug启动测试并用F5连接,断点即可生效。

在 VS Code 中为 Jest 测试用例设置断点,可以让你调试测试代码时更直观地查看变量值、调用栈和执行流程。整个过程不需要复杂的配置,只要正确启动 Jest 的调试模式即可。
Jest 默认不会暂停执行来等待调试器连接,因此需要以 --inspect 或 --inspect-brk 模式运行。
推荐在 package.json 中添加一个调试脚本:"scripts": {
"test:debug": "node --inspect-brk -r ts-node/register ./node_modules/jest/bin/jest.js --runInBand"
}
在项目根目录下创建 .vscode/launch.json,添加调试配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Jest",
"type": "node",
"request": "attach",
"port": 9229,
"cwd": "${workspaceFolder}",
"outFiles": ["${workspaceFolder}/**/*.js"]
}
]
}
npm run test:debug -- my-component.test.ts
基本上就这些。只要 Jest 以可调试方式运行,并且 VS Code 成功连接,断点就能正常工作。不复杂但容易忽略的是 --runInBand 和正确的启动顺序。
以上就是在VS Code中为Jest测试用例设置断点的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号