vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Root option ignored if vite config also has root option

Open kleinfreund opened this issue 3 years ago • 1 comments

Describe the bug

It seems that vitest completely ignores a root option under the test property in a Vite configuration file if said file also has a root option at its root.

export default defineConfig({
	root: './docs',

	test: {
		root: '.',
	},
})

No matter what I set options.test.root to, vitest will always try and find test files in options.root rather than options.test.root. In my project, this fails because options.root and options.test.root are sibling directories.

This can be considered a bit of an edge case. I'm a Vue plugin developer and want to have a basic demo website in ./docs (i.e. that's where my index.html is) and my plugin source code in ./src. I don't want to maintain two separate Vite projects (one for docs; one for the plugin source) because Vue's architecture makes it impossible to test locally in this setup using npm link.

This was possibly introduced in https://github.com/vitest-dev/vitest/pull/1164:

https://github.com/vitest-dev/vitest/blob/95da4d66933db46da368d5d66f422afd26fba4c1/packages/vitest/src/node/create.ts#L26

Workaround: Move the Vite entry points (in my case, the index.html file) into the project root. I don't like this because in my particular case, it makes for a weird project structure.

Reproduction

I'm happy to provide a minimal reproduction, but I think that the existence of https://github.com/vitest-dev/vitest/issues/1158 and the following code serve as sufficient explanation for the behavior that I'm seeing.

const server = await createServer(mergeConfig(config, mergeConfig(viteOverrides, { root: options.root })))

Should Vitest behave this way? I think this not desirable. I think it prevents developers from locating their tests outside their general source directory (I don't do this personally, but I've seen it).

System Info

System:
    OS: Windows 10 10.0.22000
  Binaries:
    Node: 16.17.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.19.2 - ~\dev\vue-accessible-color-picker\node_modules\.bin\npm.CMD
  npmPackages:
    @vitejs/plugin-vue: ^3.1.0 => 3.1.0
    vite: ^3.1.1 => 3.1.1
    vitest: ^0.23.2 => 0.23.2

Used Package Manager

npm

Validations

kleinfreund avatar Sep 16 '22 12:09 kleinfreund

I was wrong in identifying which piece of code is responsible for what I'm describing.

It's this:

https://github.com/vitest-dev/vitest/blob/95da4d66933db46da368d5d66f422afd26fba4c1/packages/vitest/src/node/config.ts#L88-L93

Changing root: viteConfig.root, to root: options.root || viteConfig.root, fixes my issue.

kleinfreund avatar Sep 16 '22 14:09 kleinfreund

I have the same issue.

galvez avatar Jan 10 '23 13:01 galvez