vscode: Not working properly in monorepo
Describe the bug Not working properly in monorepo projects. By not working propertyly means vite could not resolve path alias becasue vitest was executed in the wrong user dir which is the root dir (which should be the package/xxx dir where vite.config exists ).
To Reproduce
Run test in monorepo with resolve.alias
defined in vite.config
of sub package.
Expected behavior Working properly in monorepo.
Screenshots
Environment
(Paste info.txt content generated by the example project)
- OS: macOS 12.0
- VSCode version: 1.74.2
- Vitest version: 0.26.0
- Vitest plugin version: 0.2.34
Additional context
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 19
- Comments: 25 (2 by maintainers)
In nx mono-repository I was able to get this working finally using the vitest.workspace.ts method, and nothing more then the default command line options:
Notes:
vitest.workspace.ts
to isolatevitest
enabled packages using the config file glob, seems to be needed forvitest
to work at the command line.vitest --run
in mode via project settings, rather leave it in watch as thevitest
plugin needs this to report success/failure cases.import { configDefaults } from 'vitest/config'
package object values. I prefer a global config I can override in specific packages (e.g.vitest.shared.ts
defines majority of test configurations)vite.config.ts
vite.config.ts
that should defineviteTsConfigPaths({ root: '../../../' })
pluginresult not found problem
. Remember to check thevscode > outputs > vitest log
. The last issue I had like this was a missing or broken alias path.I got it to work using the vs code workspace workaround. You can define your workspace using a file in the root with the name
yourmainfolder.code-workspace
and in that folder you can add a customvitest.commandLine
to each folder:This works for me, using Vitest Workspace:
And nothing else.
Plus, I install vitest only in monorepo root.
This has now been properly fixed by merging #231 and #241!
Enjoy. Monorepo support should be a lot easier now, and a new NX example has been added as well. I think we can close this.
I have a few repositories for experimenting with Vitest in a monorepo setup:
https://github.com/koistya/vitest-workspaces — bare minimum Vitest config https://github.com/kriasoft/react-starter-kit — front-end monorepo https://github.com/kriasoft/relay-starter-kit — full-stack monorepo
├──
app/vite.config.ts
— React front-end (app)├──
edge/vite.config.ts
— Cloudflare Workers (edge)├──
vitest.config.ts
— Root-level Vite config└──
vitest.workspaces.ts
— Vitest workspacesyarn test
)yarn workspace edge test
)VS Code’s multi-root workspace may work for some but for many of our shared/mono repo set ups, we have one VS Code root, one
.vscode/*
one.git/*
etc.Rather than a multi-root issue this seems more of
cwd
concern.A workaround is to edit the vitest command to include an explicit root. For me, adding the following to the workspace meant that at least one of the packages in the monorepo could show interactive test results correctly.
In terms of the
vitest-dev
extension, if running from the proper root can’t be configured as default behaviour, perhaps the basedir of the file being run could be passed as a variable, meaning users could inline this logic to thevitest.commandLine
as required.This would lead to something like the following, which would mean more than one package in a monorepo could have passing tests at one time…
@steven87vt this was massively helpful. By far one of the most helpful comments I’ve seen on GitHub. Thanks a million for this!
This workaround works for subfolders
"vitest.commandLine": "npx vitest --root packages/packageName"
Check logs in vitest in output it more informative
Only problem i am facing now it skips env variables
Just messing around but here’s a start:
"vitest.commandLine": "pnpm --if-present -r test --"
pnpm has some create filtering options to employ too.Worse, however, and really must be fixed, is the poor behaviour when the vitest process fails (owing to a poorly selected working directory).
The UI simply swallows errors and sits there indefinitely with spinners rather than finalising the task with an error case.
Behind the scenes in the
Output => Vitest
terminal you can see the failures which are not properly handled byvitest-dev
such as the following, because it’s not loading the tsconfig.json from the parent directory and vitest.conf.ts that brings in the correct vitest globals…Also this meaningful error content is completely missing from the inline-notified error if you configure the extension to create test error popups.
The project I’ve used that in is closed source (it’s at my workplace), but it’s literally just creating the file like indicated in the doc and setting one of the root path to be the folder where Vitest is installed in your repo 😉
You mean using Multi-root workspaces? Can you show me an example?