Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cwd of current package inside monorepo #353

Open
cdauth opened this issue Jan 28, 2024 · 5 comments
Open

Support cwd of current package inside monorepo #353

cdauth opened this issue Jan 28, 2024 · 5 comments

Comments

@cdauth
Copy link

cdauth commented Jan 28, 2024

I am using this plugin in a monorepo with several modules. In my case, I have configured jestrunner.jestCommand to be yarn test (which in my case executes vitest instead of jest).

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:

  • I don't have jest (or in my case vitest) installed on the root level
  • The implementation of my yarn test command might be different for each module

Since #156, the project path is automatically detected based on where a package.json file and a node_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 to false and manually cd into the appropriate directory in the jest terminal before running the tests.

One way this could be achieved would be to add a config option (such as jestrunner.changeDirectoryToPackageRoot) that cds to the nearest ancestor directory containing a package.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 a package.json. Are there any setups that this would break?

@bryceg
Copy link

bryceg commented Jan 30, 2024

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.

@domsleee
Copy link
Collaborator

From jestRunnerConfig.ts when it resolves cwd(), it looks for the nearest package, but only if the package has node_modules/jest:

do {
// Try to find where jest is installed relatively to the current opened file.
// Do not assume that jest is always installed at the root of the opened project, this is not the case
// such as in multi-module projects.
const pkg = path.join(currentFolderPath, 'package.json');
const jest = path.join(currentFolderPath, 'node_modules', 'jest');
if (fs.existsSync(pkg) && fs.existsSync(jest)) {
return currentFolderPath;
}
currentFolderPath = path.join(currentFolderPath, '..');
} while (currentFolderPath !== this.currentWorkspaceFolderPath);

So the current fallback logic for jestrunner.changeDirectoryToWorkspaceRoot is:

  1. jestrunner.projectPath
  2. (code snippet above) the nearest package.json with jest in it's node_modules folder
  3. ${workspaceFolder}

Would it make sense to change (2) to find packages with either jest or vitest in it's node_modules?

@firsttris
Copy link
Owner

i forgot we already have such a logic.

it seems like if we also check for vitest it would solve the issue?

@cdauth
Copy link
Author

cdauth commented Feb 22, 2024

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.

@digitalmaster
Copy link

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.

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

No branches or pull requests

5 participants