【问题标题】:How to debug Jasmine tests run from Grunt in Visual Studio Code?如何在 Visual Studio Code 中调试从 Grunt 运行的 Jasmine 测试?
【发布时间】:2016-03-08 06:31:04
【问题描述】:

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

grunt test

测试从命令行执行。

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

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

我尝试了以下方法:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    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"
      ]
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      此启动配置适用于 VS Code 0.10.2:

      {
          "name": "grunt",
          "type": "node",
          "request": "launch",
          "program": "/usr/local/bin/grunt",
          "args": ["test"],
          "stopOnEntry": false
      }
      

      在我的“测试”任务中设置断点会使 VS Code 调试器停在那里。我必须在本地安装 grunt(在我有 Gruntfile 的文件夹中)。

      【讨论】:

      • 我已经尝试过该配置 - 一旦测试执行完成,它就会关闭整个 VS Code 应用程序......
      • 我现在也在尝试解决这个问题 - 看起来我能够在Gruntfile.js 中调试运行代码,但除此之外什么都没有......这就是我和@ nwinkler 想要。
      猜你喜欢
      • 2017-05-29
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多