firsttris / vscode-jest-runner

Simple way to run or debug one or more tests from context menu, codelens or command plalette
https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner
MIT License
264 stars 124 forks source link

Unable to debug a unit test from VS Code with ESM #289

Open bigman73 opened 1 year ago

bigman73 commented 1 year ago

I'm building a Typescript (4.9.4) package with ESM (type: module), ES2022, and pnpm.

I managed to run jest from CLI by adding the NODE_OPTIONS:

   "test": "NODE_OPTIONS=--experimental-vm-modules pnpm exec jest"

jest runner got the same treatment and it runs from VS Code by changing the jestrunner.jestCommand setting to:

  "jestrunner.jestCommand": "NODE_OPTIONS=--experimental-vm-modules node 'node_modules/jest/bin/jest.js'"

When running a test from VS Code this way, it works fine, as expected.

Debugging is not working. The jestCommand is ignored in debug mode, as a result the NODE_OPTIONS are not applied. There's a related issue on this which got no response - https://github.com/firsttris/vscode-jest-runner/issues/284

Perhaps the correct solution is to add support for user defined NODE_OPTIONS, instead of requiring the user to change the entire command, and then jest runner can apply the NODE_OPTIONS in both run and debug modes, if the user specified it. Another option is to have two user defined commands - one for run and one for debug.

This is an example of a produced debug CLI command when running from VS Code even with the jestCommand setting modified:

cd /SOME_PATH
-template ; /usr/bin/env 'NODE_OPTIONS=--require "/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/ms-vscode.js-
debug/src/bootloader.bundle.js" --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/var/folders/q1/8lg8xnrj7xg73r6
sd3jqvz_r0000gn/T/node-cdp.56347-2.sock","deferredMode":false,"waitForDebugger":"","execPath":"/Users/MY_USER/.nvm/versions/node/v1
6.18.1/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/var/folders/q1/8lg8xnrj7xg73r6sd3jqvz_r0000gn/T/nod
e-debug-callback-726895350c794868"}' /Users/MY_USER/.nvm/versions/node/v16.18.1/bin/node node_modules/jest/bin/jest.js /Users/MY_USER/development/SOME_SERVICE/__tests__/heartbeat.test.ts -t service\ heartbeat --experimental-specifier-resolution=node -
-runInBand 

Which results in a failure since ESM experimental feature is not applied to Node:

SyntaxError: Cannot use import statement outside a module
uschtwill commented 1 year ago

Check:

jtlapp commented 1 year ago

According to this comment, "Debug" uses setting jestrunner.debugOptions rather than jestrunner.jestCommand, but I still can't get it to work in my monorepo with central dependencies. "Run" works just fine:

    "jestrunner.jestCommand": "npx jest",
    "jestrunner.debugOptions": {
      "program": "npx jest"
    },

Why doesn't "Debug" use jestrunner.jestCommand? And why is it not using program, instead reporting that it can't find a particular absolute path to jest.js?

jtlapp commented 1 year ago

UPDATE: I have the following working for both "Run" and "Debug". It requires that all of my monorepo workspaces be at the same depth:

    "jestrunner.jestCommand": "npx jest",
    "jestrunner.debugOptions": {
      "program": "../../node_modules/jest/bin/jest.js"
    },