javascript - 如何在 Visual Studio Code 中调试从 Grunt 运行的 Jasmine 测试?

标签 javascript node.js gruntjs jasmine visual-studio-code

我的单元测试是通过 Grunt 使用 Karma/Jasmine 运行的。当我运行时

grunt test

测试从命令行执行。

在 Visual Studio Code 中打开项目时,我可以使用 Tasks: Run Test Task 运行相同的命令。 VSCode 使用 test 参数执行 Grunt 并显示输出。

在这种情况下如何调试 VSCode 运行的测试用例?当我按下 F5 时,会打开 launch.json 模板文件。我需要为 programargs 等提供什么来启动/调试由 grunt test 运行的相同测试用例?

我尝试了以下方法:

  • 程序:/usr/local/bin/grunt
  • args: ["test"]

这成功启动了 Grunt 进程并执行了测试,但它并没有在我的测试代码中的断点处停止。

除此之外,它会在几秒钟后关闭(或崩溃)整个 VSCode 进程。不确定这是 VSCode 中的错误还是上述运行配置的结果。

最佳答案

我认为您目前无法执行类似 node --debug-brk grunt test 的操作,其中 test 将启动 jasmine 测试 - 因为 jasmine 将在没有调试标志的情况下在这些规范文件上调用 Node 到位。我试过了,这就是我得到的:

node --debug-brk=3691 --nolazy ../../../usr/local/bin/grunt kftest --schema=9.2.1 --dbtype=sqlite --target=builder/properties --spec=test/builder/properties/properties-spec.js 
Debugger listening on port 3691
Running "kftest" task
>> going to run with spec:  test/builder/properties/properties-spec.js
>> command: node --debug-brk=46307 /Users/computername/project/node_modules/jasmine-node/lib/jasmine-node/cli.js test/builder/properties/properties-spec.js
Running "shell:kftest" (shell) task
Debugger listening on port 46307

这并不太有用,因为现在 vscode 的调试器将查看 3691 而 46307 没有被任何东西检查 - 我不知道如何告诉 vscode 也监听该端口。

我最终所做的就是按照此处发布的答案进行操作:Debugging jasmine-node tests with node-inspector

基本上我的 vscode launch.json 包含一个如下所示的配置:

{
  "name": "Jasmine-Node Debugging",
  "cwd": "${workspaceRoot}",
  "program": "${workspaceRoot}/node_modules/jasmine-node/lib/jasmine-node/cli.js",
  "request": "launch",
  "type": "node",
  "args": [
    "test/builder/properties/properties-spec.js"
  ]
}

希望对您有所帮助。

关于javascript - 如何在 Visual Studio Code 中调试从 Grunt 运行的 Jasmine 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34069462/

相关文章:

javascript - 使用 javascript 连接 php 变量

javascript - 减少 Nodejs 游戏的延迟

node.js - 使用 JWT 进行环回身份验证

javascript - yeoman 构建失败 - rjs mainConfigFile 位置

使用 Backdrop JS 进行 CSS 视觉回归测试 - 无法运行 grunt :reference or grunt:test

javascript - 使用 mathJS 的 SumProduct 多维数组

php - 从一个页面到另一个页面进行通信? JavaScript、PHP?

javascript - 如何在 MVC 3 的 HTML 表格中使用幻灯片切换?

javascript - 我可以在 mongoDB 中根据填充条件查找文档吗?

sass - 在 GitHub 页面上为 Jekyll、Haml、Sass 配置 Grunt