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

In an nx monorepo set the working directory to the package directory #254

Closed
chriswheeldon-peakon opened this issue Feb 19, 2024 · 10 comments
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@chriswheeldon-peakon
Copy link

When running tests in an nx monorepo (e.g. the monorepo-nx sample) the working directory is set to the monorepo root. However, when the tests are executed using nx (e.g. nx run-many -t test or nx test <package>) the working directory is set to the package directory.

Ideally the extension would be consistent with nx and change to the package directory before executing tests.

A workaround right now is to set up a multi-root vscode workspace with an entry for each package directory e.g. for the monorepo-nx sample:

{
  "folders": [
    {
      "path": "node"
    },
    {
      "path": "library"
    }
  ],
  "settings": {
    "vitest.enable": true
  }
@chriswheeldon-peakon chriswheeldon-peakon added the p2-nice-to-have Not breaking anything but nice to have (priority) label Feb 19, 2024
@chriswheeldon-peakon
Copy link
Author

cc @ffMathy - from our conversation on #231 🙏

@sheremet-va
Copy link
Member

I don't think this is in the scope of this extension. Vitest itself doesn't support nx integration, it has its own workspace support.

@ffMathy
Copy link
Collaborator

ffMathy commented Feb 19, 2024

But when we have a vitest.workspace.ts file, is Vitest's behavior then to set the root directory to the workspace folder, or the root folder?

Either way, I think I'll update the NX sample to mimick NX behavior more. I think it's doable.

@sheremet-va
Copy link
Member

But when we have a vitest.workspace.ts file, is Vitest's behavior then to set the root directory to the workspace folder, or the root folder?

Either way, I think I'll update the NX sample to mimick NX behavior more. I think it's doable.

vitest.workspace.ts file should be supported, yes. We cannot support nx vitest behavior though.

@chriswheeldon-peakon
Copy link
Author

chriswheeldon-peakon commented Feb 20, 2024

@sheremet-va when you say "We cannot support nx vitest behavior though." do you mean in this extension or generally in vitest?

Having thought about it some more I can clearly see that this, as you said, is beyond the scope of this extension. The issue here would seem to stem from the fact that nx will change directory into the package directory when running package commands whereas vitest will not do this, even in the presence of a vitest workspaces file.

Is it worth opening a discussion on this in the vitest repo? From reading the vitest workspaces docs and in particular this line "Vitest will consider every folder in packages as a separate project even if it doesn't have a config file inside." (emphasis mine), I think it can be argued that vitest should change directory when executing package tests. My experience here is limited, but I think this would also be consistent with other monorepo tooling (f.ex nx, npm -w).

@sheremet-va
Copy link
Member

Yes, it should change the directory for projects. And from my knowledge, it already does so (when you use the workspace feature of course). If it doesn't for you, I would suggest opening an issue with a reproduction in Vitest repo.

@sebastien-comeau
Copy link

sebastien-comeau commented Feb 22, 2024

I tried everything base on the Workspace guide and no success with our monorepo. Only the frontend directory is using vitest. We are using Playwright Test for VS Code extension and it works without any extra configurations.

[INFO 12:29:12 PM] The extension is not activated because no Vitest environment was detected.

@sheremet-va
Copy link
Member

sheremet-va commented Mar 15, 2024

This is the behavior of Vitest and not the extension. The vitest.workspace file doesn't change the working directory of its projects because it's not supported in workers. You can bypass this by using pool: 'forks' and adding a setup file that changes the directory like this for example:

import { resolve } from 'node:path'
process.chdir(resolve('../project-1'))

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2024
@chriswheeldon-peakon
Copy link
Author

@sheremet-va I don't that is viable as you are not allow to chdir in a worker.

@sheremet-va
Copy link
Member

@sheremet-va I don't that is viable as you are not allow to chdir in a worker.

Sorry, I forgot to mention to also use pool: 'forks'.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

No branches or pull requests

4 participants