Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vitest 3.x reference skill covering configuration, test/describe APIs, mocking, coverage, snapshots, and concurrency.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/advanced-projects.md
1---2name: projects-workspaces3description: Multi-project configuration for monorepos and different test types4---56# Projects78Run different test configurations in the same Vitest process.910## Basic Projects Setup1112```ts13// vitest.config.ts14defineConfig({15test: {16projects: [17// Glob patterns for config files18'packages/*',1920// Inline config21{22test: {23name: 'unit',24include: ['tests/unit/**/*.test.ts'],25environment: 'node',26},27},28{29test: {30name: 'integration',31include: ['tests/integration/**/*.test.ts'],32environment: 'jsdom',33},34},35],36},37})38```3940## Monorepo Pattern4142```ts43defineConfig({44test: {45projects: [46// Each package has its own vitest.config.ts47'packages/core',48'packages/cli',49'packages/utils',50],51},52})53```5455Package config:5657```ts58// packages/core/vitest.config.ts59import { defineConfig } from 'vitest/config'6061export default defineConfig({62test: {63name: 'core',64include: ['src/**/*.test.ts'],65environment: 'node',66},67})68```6970## Different Environments7172Run same tests in different environments:7374```ts75defineConfig({76test: {77projects: [78{79test: {80name: 'happy-dom',81root: './shared-tests',82environment: 'happy-dom',83setupFiles: ['./setup.happy-dom.ts'],84},85},86{87test: {88name: 'node',89root: './shared-tests',90environment: 'node',91setupFiles: ['./setup.node.ts'],92},93},94],95},96})97```9899## Browser + Node Projects100101```ts102defineConfig({103test: {104projects: [105{106test: {107name: 'unit',108include: ['tests/unit/**/*.test.ts'],109environment: 'node',110},111},112{113test: {114name: 'browser',115include: ['tests/browser/**/*.test.ts'],116browser: {117enabled: true,118name: 'chromium',119provider: 'playwright',120},121},122},123],124},125})126```127128## Shared Configuration129130```ts131// vitest.shared.ts132export const sharedConfig = {133testTimeout: 10000,134setupFiles: ['./tests/setup.ts'],135}136137// vitest.config.ts138import { sharedConfig } from './vitest.shared'139140defineConfig({141test: {142projects: [143{144test: {145...sharedConfig,146name: 'unit',147include: ['tests/unit/**/*.test.ts'],148},149},150{151test: {152...sharedConfig,153name: 'e2e',154include: ['tests/e2e/**/*.test.ts'],155},156},157],158},159})160```161162## Project-Specific Dependencies163164Each project can have different dependencies inlined:165166```ts167defineConfig({168test: {169projects: [170{171test: {172name: 'project-a',173server: {174deps: {175inline: ['package-a'],176},177},178},179},180],181},182})183```184185## Running Specific Projects186187```bash188# Run specific project189vitest --project unit190vitest --project integration191192# Multiple projects193vitest --project unit --project e2e194195# Exclude project196vitest --project.ignore browser197```198199## Providing Values to Projects200201Share values from config to tests:202203```ts204// vitest.config.ts205defineConfig({206test: {207projects: [208{209test: {210name: 'staging',211provide: {212apiUrl: 'https://staging.api.com',213debug: true,214},215},216},217{218test: {219name: 'production',220provide: {221apiUrl: 'https://api.com',222debug: false,223},224},225},226],227},228})229230// In tests, use inject231import { inject } from 'vitest'232233test('uses correct api', () => {234const url = inject('apiUrl')235expect(url).toContain('api.com')236})237```238239## With Fixtures240241```ts242const test = base.extend({243apiUrl: ['/default', { injected: true }],244})245246test('uses injected url', ({ apiUrl }) => {247// apiUrl comes from project's provide config248})249```250251## Project Isolation252253Each project runs in its own thread pool by default:254255```ts256defineConfig({257test: {258projects: [259{260test: {261name: 'isolated',262isolate: true, // Full isolation263pool: 'forks',264},265},266],267},268})269```270271## Global Setup per Project272273```ts274defineConfig({275test: {276projects: [277{278test: {279name: 'with-db',280globalSetup: ['./tests/db-setup.ts'],281},282},283],284},285})286```287288## Key Points289290- Projects run in same Vitest process291- Each project can have different environment, config292- Use glob patterns for monorepo packages293- Run specific projects with `--project` flag294- Use `provide` to inject config values into tests295- Projects inherit from root config unless overridden296297<!--298Source references:299- https://vitest.dev/guide/projects.html300-->301