vitest
vitest copied to clipboard
vite config `root` is ignored on workspace
Describe the bug
Though we know maintaining cwd is tricky, can we respect explicit root config such as this?
// packages/client/vite.config.ts
import { defineConfig } from 'vitest/config';
import { join } from 'node:path';
export default defineConfig({
// neither works
root: './dir',
// root: join(import.meta.dirname, './dir'),
plugins: [
{
name: 'dump-root',
configResolved(config) {
// this is always "/abs-path-to/packages/client"
// regardless of `root` config
console.log('[dump-root]', config.root);
},
},
],
});
For example, this can affect Vite's project absolute path resolution as in the reproduction below.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-rwukz2?file=packages%2Fclient%2Fvitest.config.ts
npm run testfailsnpm run test-no-workspacesucceeds
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 18.20.3 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.2.3 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
@vitejs/plugin-react: ^4.2.1 => 4.3.3
@vitest/ui: latest => 2.1.4
vite: latest => 5.4.10
vitest: latest => 2.1.4
Used Package Manager
npm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
I think we might need this line:
https://github.com/vitest-dev/vitest/blob/cf0cbf6a37a461f9040d456aa934d13aa7fb9617/packages/vitest/src/node/create.ts#L42
Although it looks very weird, I would prefer if we rewrote this to a config hook that respects user's root option.
Is this issue related to it? We're having trouble getting the workspace to work from the root directory (both in CLI and the extension), whereas running the projects individually works fine.
Is this issue related to it? We're having trouble getting the workspace to work from the root directory (both in CLI and the extension), whereas running the projects individually works fine.
I think nuxt uses cwd to resolve aliases and not the root. In your reproduction, the root is set correctly.
Vitest workspace doesn't support multi-cwd projects at the moment, every projects runs in the same working directory (from where you run the command)
If you are running the command from the the folder where the config is located, Vitest cannot find any tests.
Adding a +1 and a report from:
- https://github.com/vitest-dev/vitest/discussions/8362
Relative paths don't work but your suggested join works:
import { defineConfig } from "vitest/config";
import path from "node:path";
export default defineConfig({
test: {
root: path.join(import.meta.dirname, "src"),
},
});