7

I'm unable to attach VS Code debugger while running jest tests. I tried configuring launch.json file with different alternatives I found, but they always fail with the following error message, complaining about a file in node_modules folder:

Debugger attached.
Waiting for the debugger to disconnect...
local\path\to\node_modules\.bin\jest:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)      
    at internal/main/run_main_module.js:17:47

My package.json file has the following configuration:

  "scripts": {
    "compile": "tsc",
    "run": "node ./build/index.js",
    "execute": "tsc && node ./build/index.js",
    "test": "tsc && jest ./build/tests/*"
  },
  "author": "roguib",
  "license": "ISC",
  "dependencies": {
    "@types/jest": "^26.0.20",
    "ts-jest": "^26.5.1",
    "ts-node": "^9.1.1"
  },
  "devDependencies": {
    "@types/node": "^14.14.21",
    "jest": "^26.6.3"
  }

My launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Jest Tests",
            "type": "node",
            "request": "launch",
            "runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand"],
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        }
    ]
}

source

If it helps, executing npm run test normally runs my jests test so I'm unsure it's something related to jest itself.

roguib
  • 141
  • 6

2 Answers2

8

There seems to be a bug on Windows and the solution is to point VS Code directly to jest.js rather than the .bin version:

So try replacing this:

"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand"], with something like:

"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand", "--coverage", "false"],

https://github.com/facebook/jest/issues/3750

Also, if you're getting another error regarding jest config, add the -c arg:

  "args": [
              "--runInBand",
              "-c", "${workspaceFolder}/package/jest.config.js",
            ],
ACV
  • 9,964
  • 5
  • 76
  • 81
1

I solved the issue by installing ts-jest and using the following configuration in launch.json.

{
  "type": "node",
  "request": "launch",
  "name": "Jest All",
  "program": "${workspaceFolder}/node_modules/.bin/jest",
  "args": [
    "--runInBand"
  ],
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "disableOptimisticBPs": true,
  "windows": {
    "program": "${workspaceFolder}/node_modules/jest/bin/jest",
  }
},

Source.

roguib
  • 141
  • 6