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
265 stars 124 forks source link

JS: Clicking "Debug" link above test results in "Error: Cannot find module '/home/[my_home]/node_modules/.bin/jest'" #266

Closed haughki closed 2 years ago

haughki commented 2 years ago

Type: Bug

WSL 2 Ubuntu 20.04.4 LTS nvm 0.39.1 node v18.7.0 npm 8.15.0

Disabled all but these three VSCode extensions: remote-wsl|ms-|0.66.3 vscode-remote-extensionpack|ms-|0.21.0 vscode-jest-runner|fir|0.4.48

To Reproduce

Expected The test runs in the terminal

Actual A 'jest' terminal starts in the TERMINAL tab on the bottom pane. It tries to cd into the workspace dir before the command prompt is ready, and ends up in the home directory:

cd '/home/xxxxxx/projects/my-app'
Starting wsl-vpnkit service...
process already running.
xxxxxx:~$ 

Then, a "Debug Jest Tests" terminal starts, and runs the following commands which result in the error:

Starting wsl-vpnkit service...
process already running.
xxxxxx:~$  /usr/bin/env 'NODE_OPTIONS=--require /home/xxxxxx/.vscode-server/bin/6d9b74a70ca9c7733b29f0456fd8195364076dda/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.29546-2.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/xxxxxx/.nvm/versions/node/v18.7.0/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","mandatePortTracking":true,"fileCallback":"/tmp/node-debug-callback-2e4dfcdc9ab2401d"}' /home/xxxxxx/.nvm/versions/node/v18.7.0/bin/node node_modules/.bin/jest /home/xxxxxx/projects/my-app/src/supporting/__test__/MyClass.test.js -t testing --runInBand 
Debugger attached.
Waiting for the debugger to disconnect...
node:internal/modules/cjs/loader:959
  throw err;
  ^

Error: Cannot find module '/home/xxxxxx/node_modules/.bin/jest'
    at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Module._load (node:internal/modules/cjs/loader:804:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v18.7.0
xxxxxx:~$ 

Expected The test should run and break on the breakpoint

Actual The terminal spawn again as above, and I get the following:

Starting wsl-vpnkit service...
process already running.
xxxxxx:~$  /usr/bin/env 'NODE_OPTIONS=--require /home/xxxxxx/.vscode-server/bin/6d9b74a70ca9c7733b29f0456fd8195364076dda/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.2717-2.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/xxxxxx/.nvm/versions/node/v18.7.0/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","mandatePortTracking":true,"fileCallback":"/tmp/node-debug-callback-01f3dde948ed1ced"}' /home/xxxxxx/.nvm/versions/node/v18.7.0/bin/node node_modules/.bin/jest /home/xxxxxx/projects/my-app/src/supporting/__test__/MyClass.test.js -t testing --runInBand 
Debugger attached.

The terminal just hangs at this point, the test never runs, breakpoint never hit.

VS Code version: Code 1.70.1 (6d9b74a70ca9c7733b29f0456fd8195364076dda, 2022-08-10T06:08:33.642Z) OS version: Windows_NT x64 10.0.19042 Modes: Remote OS version: Linux x64 5.4.72-microsoft-standard-WSL2 Remote OS version: Linux x64 5.4.72-microsoft-standard-WSL2

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz (8 x 1896)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled| |Load (avg)|undefined| |Memory (System)|15.81GB (0.98GB free)| |Process Argv|--file-uri=vscode-remote://wsl+Ubuntu/home/xxxxxx/projects/vsc_test_bug/NO_REG2.code-workspace --remote=wsl+Ubuntu --crash-reporter-id ea39c7bc-9acf-4ab0-a8de-b9cec0c6fcb0| |Screen Reader|no| |VM|0%| |Item|Value| |---|---| |Remote|WSL: Ubuntu| |OS|Linux x64 5.4.72-microsoft-standard-WSL2| |CPUs|Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz (4 x 1896)| |Memory (System)|2.92GB (0.22GB free)| |VM|0%| |Item|Value| |---|---| |Remote|WSL: Ubuntu| |OS|Linux x64 5.4.72-microsoft-standard-WSL2| |CPUs|Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz (4 x 1896)| |Memory (System)|2.92GB (0.22GB free)| |VM|0%|
Extensions (3) Extension|Author (truncated)|Version ---|---|--- remote-wsl|ms-|0.66.3 vscode-remote-extensionpack|ms-|0.21.0 vscode-jest-runner|fir|0.4.48
firsttris commented 2 years ago

maybe this happens because of your remote wsl connection?

can you use the vanilla debug feature from vscode. the addon is just executing that.

haughki commented 2 years ago

tl;dr At this point, I don't really see this as a Jest Runner issue. See details below. I would like to see if I can sort out the weird terminal issue I describe below, and then post back to this thread. I can't remember if I can still post to the thread after you close the issue: if so, feel free to close it and I'll circle back when I get it sorted. Else, maybe you could leave open for a week -- I'll plan to figure this out in the next few days.

Can you use the vanilla debug feature...

can you use the vanilla debug feature from vscode. the addon is just executing that.

Yes. After adding a babel.config.js to the root:

module.exports = {
  presets: [
    ["@babel/preset-env", { targets: { node: "current" } }],
    "babel-preset-react-app",
  ],
}

I can attach the debugger and break running :~/projects/my-app (master)$ ./node_modules/.bin/jest --runInBand /home/xxxxxx/projects/my-app/src/supporting/__test__/MyClass.test.js -t testing


Well, I see now at least part of the problem...

The actual jest command:

home/xxxxxx/.nvm/versions/node/v18.7.0/bin/node node_modules/.bin/jest /home/xxxxxx/projects/my-app/src/supporting/__test__/MyClass.test.js -t testing --runInBand

is looking for node_modules/.bin/jest assuming the cwd is the project root, but the terminal is in my home dir. Hence: Error: Cannot find module '/home/xxxxxx/node_modules/.bin/jest' So, I think that if the jest command was an abs path, it would fix the bug, sortof....

But, Jest Runner is already trying to cd the terminal into the project root; the cd just isn't happening 'in time'. This seems to have more to do with the 'start up' of the two terminals. Meaning:

So. It works, just not on the first run. I wonder why things are getting so munged on that first run: looks sorta like a timing thing -- that the terminals are taking too long to start. Maybe has something to do with my .bashrc that runs when the term opens (that's where the 'wsl-vpnkit' is coming from)...? But, if so, that seems like a bug (somewhere) in it's own right; i.e., a new terminal should be able to run a .bashrc to set itself up before whatever else happens, right?

haughki commented 2 years ago

Solution

This was because of a line in my .bashrc:

# go home. This because of a bug in winterm settings: https://github.com/microsoft/WSL/issues/6995
~

I was telling my shell to cd to the home directory at the end of .bashrc because of an old WSL bug that I had 'fixed' with this tilde. Removing the line fixes the problem of the JS debugger looking for jest in the wrong place, etc.

Thank you for this fantastic plugin!!