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

`changeDirectoryToWorkspaceRoot` works in `Run`, but not in `Debug` #296

Open netroy opened 1 year ago

netroy commented 1 year ago

I have changeDirectoryToWorkspaceRoot set to false because I'm predominantly working on mono-repos, and the terminal switching directory just before the test run causes issues tsconfig paths related in the test, as jest evaluates <rootDir> incorrectly.

Setting changeDirectoryToWorkspaceRoot to false seems to disable directory switching as expected in Run mode for me, but debugging commands always start with cd <WorkspaceRoot> ;

AxTrusov commented 1 year ago

Hmm, for me it adds the cd '<path-to-root>' both when I use Run and Debug. But in my case, it kind of does not matter, because in both cases and with both true/false settings, a new VSCode terminal tab is opened, and it defaults to the monorepo root...

willwillman-kr commented 1 year ago

run works fine (does not open separate terminal) debug cds to root (does originally open a separate terminal but then after the first run in that terminal it does not open any more new ones if you do not close it)

what i noticed is that with changeDirectoryToWorkspaceRoot= true then there are 2 separate commands triggered in terminal 1) to change the directory 2) to run the test or debug

with it off there is only ever 1 command run but debug shows the cd to root in that command

i believe the root cause is this from the config which is passed to the vs code debugger: https://github.com/firsttris/vscode-jest-runner/blob/e3f7c76334c248ff426fd078a7634213b4106174/src/jestRunner.ts#L172

following the code up i see that the cwd is a getter that returns a directory path as either project || current || workspace as a fall through. https://github.com/firsttris/vscode-jest-runner/blob/e3f7c76334c248ff426fd078a7634213b4106174/src/jestRunnerConfig.ts#L56

adding "jestrunner.projectPath": "/Users/myuser/path/to/current/directory/in/terminal" to my extension settings.json shows the cd still there but to the new path and the debug works as expected

WillWillman commented 1 year ago

So i was able to look into this for an hour after work on my personal machine and think i have something sorta in the ballpark (have not checked for regressions and not positive this is the best way to do it... but maybe gives a proper maintainer an idea?)

Details: Appears that the currentPackagePath and jestBinPath does not account for the possibility where certain mono-repos have packages (each with their own package.json) that share a node_module directory in the root. this allows the user to set a path to jest fixed from the workspace root.

I suspect maybe a better way would be to have it find the closest package.json and jest executable separately (with the same idea that was already in place to bail out when either gets to the workspace root without matching)

PR: https://github.com/firsttris/vscode-jest-runner/pull/329/files

Additional Update Required: adding this to [workspace]/.vscode/settings.json { "jestrunner.jestPathFromWorkspace" : "node_modules/jest/bin/jest.js" }