如何使用Visual Studio代码调试CucumberJS脚本

时间:2017-05-09 19:52:37

标签: visual-studio-code cucumberjs

我正在使用Visual Studio Code构建cucumberjs测试。我可以使用命令行中的npm运行测试,并且我可以使用启动配置在VS Code中运行它们。

但是,我无法从Visual Studio Code中调试测试。这是在Windows 7上,VSCode 1.12.1

基本文件结构:

.
+-- .vscode
|   +-- launch.json
+-- features
|   +-- step_definitions
|   |   +-- sampleSteps.js
|   +-- support
|   |   +-- customWorld.js
|   +-- sampletest.feature
+-- node_modules
|   +-- .bin
|   |   +-- cucumberjs
+-- package.json
+-- README.md

在package.json中,我有以下内容:

  "scripts": {
    "test": "./node_modules/.bin/cucumberjs"
  },

从命令行,我可以成功运行npm testnpm run-script test。我有一个launch.json配置如下:

{
    // Use IntelliSense to learn about possible Node.js debug attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch via NPM",
            "runtimeExecutable": "npm",
            "windows": {
                "runtimeExecutable": "npm.cmd"
            },
            "runtimeArgs": [
                "run-script",
                "test"
            ]
        }
    ]
}

当我从VS Code中运行调试器时,它只是运行测试,给我结果,但不尊重断点。

我希望能够逐步完成我的代码,似乎launch.json就是这样做的工具。我试过直接从launch.json调用黄瓜,但在这种情况下它似乎没有找到所有正确的文件(包括cucumberjs)。

2 个答案:

答案 0 :(得分:3)

我能够使用此launch.json:

{
    // Use IntelliSense to learn about possible Node.js debug attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Via NPM",
            "runtimeExecutable": "npm",
            "windows": {
                "runtimeExecutable": "npm.cmd"
            },
            "env":{
               "NODE_PATH": "test/"
            },
            "runtimeArgs": [
                "run-script",
                "debug"
            ],
            "port": 5858
        }
    ]
}

然后在package.json中:

"scripts": {
    "debug": "node --debug-brk=5858 ./node_modules/cucumber/bin/cucumber.js --profile TEST -t '@my_tag'"
}

希望这有帮助! (请注意这是在MacOS上完成的)

答案 1 :(得分:1)

或者仅在 Windows 10 / VSCode

中使用它

launch.json

 {
     "type": "node",
     "request": "launch",
     "name": "Cucumber Tests",
     "program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber-js",
     "internalConsoleOptions": "openOnSessionStart",
 }

或者如果是 Mac OS ,请用以下内容替换程序

"program": "${workspaceRoot}/node_modules/.bin/cucumber-js",

您无需在scripts中添加package.json