Open cdauth opened 7 months ago
similar issue here with monorepo, its cwd is down in the root. An option to specify the directory through a configuration option so I can use something like a vscode variable reference would help.
From jestRunnerConfig.ts
when it resolves cwd()
, it looks for the nearest package, but only if the package has node_modules/jest
:
So the current fallback logic for jestrunner.changeDirectoryToWorkspaceRoot
is:
jestrunner.projectPath
package.json
with jest in it's node_modules folder${workspaceFolder}
Would it make sense to change (2) to find packages with either jest
or vitest
in it's node_modules?
i forgot we already have such a logic.
it seems like if we also check for vitest it would solve the issue?
Adding support for vitest
might be nice, but it does not solve the problem that I describe here.
The problem described here is that dependency hoisting causes jest/vitest to be installed in a different location than where it should be used. For example, the directory layout might look like this:
/package.json
/node_modules/.bin/jest
/module1/package.json
/module2/package.json
jest is only listed as a dependency in /module1/package.json
and /module2/package.json
and test
scripts are defined in those files. In /package.json
, jest is not defined as a dependency and there is no test
script. But vscode-jest-runner would run the tests in /
.
If module1
and module2
would for some reason depend on different versions of jest, the directory layout would change into this:
/package.json
/module1/package.json
/module1/node_modules/.bin/jest
/module2/package.json
/module2/node_modules/.bin/jest
So vscode-jest-runner would now run the tests inside the respective module folders.
Personally I don't see a use case where in the first case (both modules using the same jest version) the tests should run on the root level of the project. So my recommendation would be to simply remove the check for node_modules/.bin/jest
altogether and simply run the tests in the nearest directory that contains a package.json
. But maybe there is some use case that I'm not aware of.
This is my exact issue as well. Also, agree that it seems like this would work as the default. But would be happy with an option.
I am using this plugin in a monorepo with several modules. In my case, I have configured
jestrunner.jestCommand
to beyarn test
(which in my case executesvitest
instead ofjest
).By default, vscode-jest-runner executes the command on the root level of my monorepo. I would like it to execute on the root level of the individual NPM package instead, for several reasons:
jest
(or in my casevitest
) installed on the root levelyarn test
command might be different for each moduleSince #156, the project path is automatically detected based on where a
package.json
file and anode_modules/.bin/jest
file is present. In case of a monorepo, this will only result in my desired behaviour if the modules have different versions of jest installed. If they all have the same version,node_modules/.bin/jest
will be available on the root level of the repo.As a workaround, I currently have
jestrunner.changeDirectoryToWorkspaceRoot
set tofalse
and manuallycd
into the appropriate directory in thejest
terminal before running the tests.One way this could be achieved would be to add a config option (such as
jestrunner.changeDirectoryToPackageRoot
) thatcd
s to the nearest ancestor directory containing apackage.json
file before running a test.On the other hand, I'm wondering whether this behaviour shouldn't be the default. The location detection for the
eslint
executable would stay the same as today, but the detection of the cwd would become independent of that and use the closest ancestor directory containing apackage.json
. Are there any setups that this would break?