如何在Visual Studio Code (VSCode)中调试Cucumber?

13

我正在尝试在Visual Studio Code中调试Cucumber场景,并对launch.json进行了以下更改。

{
            "name": "e2e",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}\\node_modules\\.bin\\cucumber-js",
            "stopOnEntry": false,
            "args": ["--no-timeouts", "--colors"],
            "cwd": "${workspaceRoot}",
            "runtimeExecutable": null,
            "outFiles": [
                "${workspaceRoot}\\features\\step_definitions\\*.js"
            ]
},

然而,我无法使用上述配置运行调试会话。我创建的步骤定义文件是JavaScript格式。 因此,如果看起来没问题,我只需要上面脚本的帮助吗?


什么断点?在哪里? - ifconfig
代码中有断点!上面的问题是为了验证我的launch.json文件中的cucumber配置脚本。希望现在对您清楚了。 - user7890278
6个回答

15

你可以尝试以下配置,使得你在VS Code中的调试工作。在outFiles中提供你的功能文件路径。

你可以使用以下配置来使VS Code中的调试工作正常。在outFiles中填写你的特性文件路径。

{
    "name": "e2e",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber.js",
    "outFiles": [
        "${workspaceRoot}/features/*.feature"
    ]
}

============================================
cucumber ^5.0.2更新:

{
    "name": "NPM Cukes",
    "type": "node",
    "request": "launch",
    "console": "integratedTerminal",
    "program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber-js",
    "args": [
        "path/to/features/**/*.feature",
        "-r",
        "path/to/steps/**/*",
        "--tags",
        "@your-tags"
    ]
}

如果您只想调试当前功能,请将以下内容添加到launch.json中

{
    "type": "node",
    "request": "launch",
    "program": "${workspaceFolder}/node_modules/.bin/cucumber-js",
    "args": ["${relativeFile}"],
    "name": "Cukes current",
    "console": "integratedTerminal",
    "internalConsoleOptions": "neverOpen",
    "windows": {
        "program": "${workspaceFolder}/node_modules/cucumber/bin/cucumber"
    }
}   

3
程序现在已经迁移到了 @cucumber/cucumber(我的版本是 7.3.2)。 - iamsimonsmale

4
调整了 Mukesh Rawat 的答案,并确保其他文件路径正确,我成功解决了问题。:
启动.json
{
    "name": "DebugMode",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber-js",
    "args": [
        "${workspaceRoot}/features/*.feature",
        "--tags", "@debug"
    ]
}

Workspace.json

{
    "cucumberautocomplete.steps": [
        "features/steps/*.js"
    ],
    "cucumberautocomplete.syncfeatures": "features/*.feature",
    "cucumberautocomplete.strictGherkinCompletion": true,
    "settings": {},
    "folders": [
        {
            "path": "/Users/{me}/Documents/{project folder}/{project name}"
        }
    ]
}

Package.json

"scripts": {
    "debug": "node --inspect=1337 --debug-brk --nolazy node_modules/cucumber/bin/cucumber-js --tags @debug --format json:./reports/report.json",

CucumberTest.feature

@debug
Scenario: Validate I can get debug working

3
在使用Ruby时,它可以用这种方式来运行特定的功能文件。
{
    "name": "Cucumber",
    "type": "Ruby",
    "request": "launch",
    "cwd": "${workspaceRoot}",
    "program": "${workspaceRoot}/bin/cucumber",
    "args": [
        "--tags", "@Mytags",
        ]
}

1

使用截至2023年1月的最新Cucumber、Playwright和TypeScript - F5(在VSCode中运行)- 在ts步骤文件中设置debugger并使用.vscode/launch.json(您可能需要调整报告位置)

{
  "version": "0.1.0",
  "configurations": [
    {
      "name": "debugMode",
      "type": "node",
      "request": "launch",      
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen", 
      "program": "node_modules/@cucumber/cucumber/bin/cucumber-js",
      "args": [
        "./features/*.feature",
        "--require-module",
        "ts-node/register",
        "--require", 
        "./steps/*.steps.ts",        
        "--tags",
        "@demoX",       
        "--format", "progress",
        "--format", "json:./Reports/cucumber_report.json"    
      ]
    }
  ]
}

0

这是我发现在 VS Code 调试器中运行 Cucumber.js 最简单的方法:

  1. 设置 JavaScript 调试器自动附加到 "onlyWithFlag"(Ctrl+Shift+P,输入 "Toggle Auto Attach")
  2. 按以下方式运行 Cucumber.js:node --inspect ./node_modules/.bin/cucumber-js <args...>
  3. 为方便起见,在测试项目中 设置一个 NPM 运行脚本,名为 "debug",这样您就可以运行 npm run debug -- <args...>

0

这个有效

{
    "name": "DebugMode",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/node_modules/cucumber/bin/cucumber-js",
    "args": [
        "${workspaceRoot}/features/*.feature",
        "--tags", "@debug"
    ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接