Skip to content

Instantly share code, notes, and snippets.

@esamattis
Last active April 11, 2024 07:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esamattis/600d6964f3e6ab6e8e7da9a05bfe6574 to your computer and use it in GitHub Desktop.
Save esamattis/600d6964f3e6ab6e8e7da9a05bfe6574 to your computer and use it in GitHub Desktop.
Debug jest tests in vscode with pnpm
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"runtimeExecutable": "sh", // <-- The important bit!
"program": "node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "openOnFirstSessionStart"
}
]
}
@esamattis
Copy link
Author

esamattis commented Apr 27, 2022

https://twitter.com/esamatti/status/1519338647857860609

fix for

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

SyntaxError: missing ) after argument list

@alexgorbatchev
Copy link

🎉

@scytalezero
Copy link

Thank you so much for posting this!

@bdmayes
Copy link

bdmayes commented Jul 10, 2023

LEGEND! Thank you for this. I just spent way too much time trying to figure out how to be able to debug freaking typescript jest tests in a project (new job). For what it's worth, I simply:

  • installed the Orta.vscode-jest plugin
  • hit CMD+SHIFT+P (CTRL+SHIFT+P for PC) and chose Jest: Setup Extension > Setup Jest Debug Config
  • added "runtimeExecutable": "sh", to that generated config in the launch.json

Oh and also, do NOT change the name of the auto-generated launch config. The extension appears to locate the debug configuration by that name, and that name only.

@tea-artist
Copy link

Great job!

@alexandercerutti
Copy link

alexandercerutti commented Feb 16, 2024

Such configuration was not exactly working for me, but I figured out this, and it worked for me. I'm using Jest with ESM modules, pnpm, and Orta's extension (Orta.vscode-jest):

{
	"type": "node",
	"name": "vscode-jest-tests.v2",
	"request": "launch",
	"console": "integratedTerminal",
	"internalConsoleOptions": "neverOpen",
	"env": {
		"NODE_OPTIONS": "--experimental-vm-modules --no-warnings"
	},
	"runtimeExecutable": "pnpm",
	"cwd": "${workspaceFolder}",
	"args": [
		"jest",
		"-c",
		"${workspaceRoot}/jest.config.mjs",
		"--runInBand",
		"--watchAll=false",
		"--testNamePattern",
		"${jest.testNamePattern}",
		"--runTestsByPath",
		"${jest.testFile}"
	]
}

Of course, args are arbitrary and refer to the configuration I use. But this let me also debug the single tests.

BTW, I also have this in my settings.json to let it run:

{
	"jest.jestCommandLine": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" pnpm jest -c jest.config.mjs --runInBand"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment