-
Notifications
You must be signed in to change notification settings - Fork 85
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
Vitest workspaces support #664
Comments
We should keep this issue active as Vitest workspaces are a common thing for monorepos. |
This would be very useful! Right now we use Vitest workspaces in order to run tests with vastly differing environments ( |
Could you please share how you make it work? |
@TheDutchCoder Sure! I don't remember all the details, but I'll try sharing what we have. We have a import { defineConfig } from 'vitest/config'
// https://vitest.dev/config/
export default defineConfig({
test: {
coverage: {
all: false,
reporter: ['lcov', 'json', 'html', 'text'],
exclude: ['src/**/*test.ts', '<more excluded stuff>']
}
}
}) And a import { fileURLToPath, URL } from 'url'
import { defineWorkspace, defineProject, type Plugin } from 'vitest/config'
import { defineVitestConfig as defineNuxtVitestConfig } from '@nuxt/test-utils/config'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
export function relativeToProjectRoot (path: string): string {
return fileURLToPath(new URL(path, import.meta.url))
}
export default defineWorkspace([
'packages/*',
defineProject({
// @ts-ignore
resolve: {
alias: {
'~': relativeToProjectRoot('./src'),
'@': relativeToProjectRoot('./src'),
'~~': relativeToProjectRoot('./'),
'@@': relativeToProjectRoot('./'),
'#app': relativeToProjectRoot('./node_modules/nuxt/dist/app/index.d.ts'),
'#app/components/nuxt-link': relativeToProjectRoot('./node_modules/nuxt/dist/app/components/nuxt-link'),
'#imports': relativeToProjectRoot('./.nuxt/imports.d.ts'),
'#vue-router': relativeToProjectRoot('./node_modules/vue-router'),
'#components': relativeToProjectRoot('./.nuxt/components'),
'#components/*': relativeToProjectRoot('./.nuxt/components/*')
}
},
plugins: [vue(), vueJsx()] as Plugin[],
test: {
name: 'happy-dom',
environment: 'happy-dom',
globals: true,
include: ['src/**/*.test.ts'].map(relativeToProjectRoot),
exclude: ['src/**/*.nuxt.test.ts'].map(relativeToProjectRoot),
includeSource: ['src/**/*.ts'].map(relativeToProjectRoot),
setupFiles: [
'src/global/test/setupTestingLibrary.ts',
'<some other setup files>'
].map(relativeToProjectRoot),
}
}),
// @ts-ignore
defineNuxtVitestConfig({
// @ts-ignore
test: {
name: 'nuxt',
environment: 'nuxt',
include: ['src/**/*.nuxt.test.ts'].map(relativeToProjectRoot),
setupFiles: [
'src/global/test/setupTestingLibrary.ts',
'src/global/test/setupI18nOnNuxtEnvironment.ts',
'<more setup files>'
].map(relativeToProjectRoot),
}
})
]) The last setup file on the Nuxt environment ( import { config } from '@vue/test-utils'
import { beforeAll } from 'vitest'
beforeAll(() => {
const nuxtApp = useNuxtApp()
config.global.plugins.push({
async install (app, ...options) {
// @ts-ignore
const i18n = nuxtApp.vueApp.__VUE_I18N__
await i18n.install(app, ...options)
}
})
}) With this configuration, test files named |
@ekisu Thanks, this points me in the right direction it seems! I think some of the auto-imports still don't work. For example, one of the utils functions uses another util function. For example util function Our structure is as follows
|
here's a very minimal reproduction which triggers a context conflict with |
Describe the feature
When using vitest in a monorepo setting, one is likely to use the vitest workspaces feature: https://vitest.dev/guide/workspace
However, in this setting one would use
defineProject
instead ofdefineConfig
, to reuse the global vitest instance.It would be cool to be able to do this also for nuxt apps, in order to run nuxt tests seamlessly along the ones of other packages
Additional information
Final checks
The text was updated successfully, but these errors were encountered: