Search code examples
visual-studio-codecucumberjs

How to debug a CucumberJS script using Visual Studio Code


I am building cucumberjs tests using Visual Studio Code. I am able to run the tests using npm from the command-line, and I am able to run them from within VS Code using a launch configuration.

However, I am unable to debug the test from within Visual Studio Code. This is on Windows 7, with VSCode 1.12.1

Basic File Structure:

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

Inside package.json, I have the following:

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

From the command-line, I can run npm test or npm run-script test with success. I have a launch.json configuration as follows:

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

When I run the Debugger from within VS Code, it just runs the test, giving me the results, but doesn't honor the breakpoints.

I would like to be able to step through my code, and it seems like launch.json is the tool to do that. I have tried calling cucumber directly from launch.json, but in that case it doesn't seem to find all the right files (including cucumberjs).


Solution

  • I was able to get it working with this 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
            }
        ]
    }
    

    Then in package.json:

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

    Hope this helps! (please note this was done on MacOS)