7

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).

erich z
  • 125
  • 2
  • 7
  • I think you need a way to pass a debug port to node, such as --debug=12345. However I haven't found a way to succesfully pass that to cucumberjs, and so far can't find anything on the net. – samspot Jun 20 '17 at 17:15

2 Answers2

3

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)

samspot
  • 595
  • 1
  • 11
  • 24
2

Or simply use this in Windows 10 / VSCode

launch.json

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

Or if it's Mac OS replace program with below

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

You dont need to add scripts in package.json

Faraj Farook
  • 14,385
  • 16
  • 71
  • 97
  • I was getting errors like the one below, but that was resolved after making sure I was referencing the binary in the cucumber folder, instead of the topmost `.bin` folder in `node_modules`. `Uncaught SyntaxError C:\App\node_modules\.bin\cucumber-js:2 basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")` – Nino van der Mark Jul 26 '23 at 09:39