|
| 1 | +# Vitest AI Agent Guide |
| 2 | + |
| 3 | +This document provides comprehensive information for AI agents working on the Vitest codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Vitest is a next-generation testing framework powered by Vite. This is a monorepo using pnpm workspaces with the following key characteristics: |
| 8 | + |
| 9 | +- **Language**: TypeScript/JavaScript (ESM-first) |
| 10 | +- **Package Manager**: pnpm (required) |
| 11 | +- **Node Version**: ^18.0.0 || >=20.0.0 |
| 12 | +- **Build System**: Vite + Rollup |
| 13 | +- **Monorepo Structure**: 15+ packages in `packages/` directory |
| 14 | + |
| 15 | +## Setup and Development |
| 16 | + |
| 17 | +### Initial Setup |
| 18 | +1. Run `pnpm install` to install dependencies |
| 19 | +2. Run `pnpm build` to build all packages |
| 20 | +3. Install Playwright browsers when working with browser features: `npx playwright install --with-deps` |
| 21 | + |
| 22 | +### Key Scripts |
| 23 | +- `pnpm build` - Build all packages |
| 24 | +- `pnpm dev` - Watch mode for development |
| 25 | +- `pnpm lint` - Run ESLint |
| 26 | +- `pnpm lint:fix` - Fix linting issues automatically |
| 27 | +- `pnpm typecheck` - Run TypeScript type checking |
| 28 | + |
| 29 | +## Testing |
| 30 | + |
| 31 | +### Running Tests |
| 32 | +- **All tests**: `CI=true pnpm test:ci` |
| 33 | +- **Examples**: `CI=true pnpm test:examples` |
| 34 | +- **Specific test suite**: `CI=true cd test/<test-folder> && pnpm test <test-file>` |
| 35 | +- **Core directory test**: `CI=true pnpm test <test-file>` (for `test/core`) |
| 36 | +- **Browser tests**: `CI=true pnpm test:browser:playwright` or `CI=true pnpm test:browser:webdriverio` |
| 37 | + |
| 38 | +### Testing Utilities |
| 39 | +- **`runInlineTests`** from `test/test-utils/index.ts` - You must use this for complex file system setups (>1 file) |
| 40 | +- **`runVitest`** from `test/test-utils/index.ts` - You can use this to run Vitest programmatically |
| 41 | +- **No mocking policy** - You must never mock anything in tests |
| 42 | + |
| 43 | +## Project Structure |
| 44 | + |
| 45 | +### Core Packages (`packages/`) |
| 46 | +- `vitest` - Main testing framework |
| 47 | +- `vite-node` - Vite SSR runtime |
| 48 | +- `browser` - Browser testing support |
| 49 | +- `ui` - Web UI for test results |
| 50 | +- `runner` - Test runner core |
| 51 | +- `expect` - Assertion library |
| 52 | +- `spy` - Mocking and spying utilities |
| 53 | +- `snapshot` - Snapshot testing |
| 54 | +- `coverage-v8` / `coverage-istanbul` - Code coverage |
| 55 | +- `utils` - Shared utilities |
| 56 | +- `mocker` - Module mocking |
| 57 | + |
| 58 | +### Test Organization (`test/`) |
| 59 | +- `test/core` - Core functionality tests |
| 60 | +- `test/browser` - Browser-specific tests |
| 61 | +- Various test suites organized by feature |
| 62 | + |
| 63 | +### Important Directories |
| 64 | +- `docs/` - Documentation (Vite-powered) |
| 65 | +- `examples/` - Example projects and integrations |
| 66 | +- `scripts/` - Build and development scripts |
| 67 | +- `.github/` - GitHub Actions workflows |
| 68 | +- `patches/` - Package patches via pnpm |
| 69 | + |
| 70 | +## Code Style and Conventions |
| 71 | + |
| 72 | +### Formatting and Linting |
| 73 | +- **Always run** `pnpm lint:fix` after making changes |
| 74 | +- Fix non-auto-fixable errors manually |
| 75 | + |
| 76 | +### TypeScript |
| 77 | +- Strict TypeScript configuration |
| 78 | +- Use `pnpm typecheck` to verify types |
| 79 | +- Configuration files: `tsconfig.base.json`, `tsconfig.build.json`, `tsconfig.check.json` |
| 80 | + |
| 81 | +### Code Quality |
| 82 | +- ESM-first approach |
| 83 | +- Follow existing patterns in the codebase |
| 84 | +- Use utilities from `@vitest/utils/*` when available. Never import from `@vitest/utils` main entry point directly. |
| 85 | + |
| 86 | +## Common Workflows |
| 87 | + |
| 88 | +### Adding New Features |
| 89 | +1. Identify the appropriate package in `packages/` |
| 90 | +2. Follow existing code patterns |
| 91 | +3. Add tests using testing utilities |
| 92 | +4. Run `pnpm build && pnpm typecheck && pnpm lint:fix` |
| 93 | +5. Add tests with relevant test suites |
| 94 | + |
| 95 | +### Debugging |
| 96 | +- Use VS Code: `⇧⌘B` (Shift+Cmd+B) or `Ctrl+Shift+B` for dev tasks |
| 97 | +- Check `scripts/` directory for specialized development tools |
| 98 | + |
| 99 | +### Documentation |
| 100 | +- Main docs in `docs/` directory |
| 101 | +- Built with `pnpm docs:build` |
| 102 | +- Local dev server: `pnpm docs` |
| 103 | + |
| 104 | +## Dependencies and Tools |
| 105 | + |
| 106 | +### Key Dependencies |
| 107 | +- **Vite** - Build tool and dev server |
| 108 | +- **Rollup** - Bundler |
| 109 | +- **ESLint** - Linting |
| 110 | +- **TypeScript** - Type checking |
| 111 | +- **Playwright** - Browser testing |
| 112 | +- **Chai/Expect** - Assertions |
| 113 | +- **Tinypool** - Worker threading |
| 114 | +- **Tinybench** - Benchmarking |
| 115 | + |
| 116 | +### Development Tools |
| 117 | +- **tsx** - TypeScript execution |
| 118 | +- **ni/nr** - Package manager abstraction |
| 119 | +- **bumpp** - Version bumping |
| 120 | +- **changelogithub** - Changelog generation |
| 121 | + |
| 122 | +## Browser Testing |
| 123 | +- Two modes: Playwright and WebDriverIO |
| 124 | +- Separate test commands for each |
| 125 | +- Component testing supported (Vue, React, Svelte, Lit, Marko) |
| 126 | + |
| 127 | +## Performance Considerations |
| 128 | +- This is a performance-critical testing framework |
| 129 | +- Pay attention to import costs and bundle size |
| 130 | +- Use lazy loading where appropriate |
| 131 | +- Consider worker thread implications |
| 132 | + |
| 133 | +## Troubleshooting |
| 134 | + |
| 135 | +### Common Issues |
| 136 | +- Ensure pnpm is used (not npm/yarn) |
| 137 | +- Build before running tests |
| 138 | +- Check Node.js version compatibility |
| 139 | +- Playwright browsers must be installed for browser tests |
| 140 | + |
| 141 | +### Getting Help |
| 142 | +- Check existing issues and documentation |
| 143 | +- Review CONTRIBUTING.md for detailed guidelines |
| 144 | +- Follow patterns in existing code |
0 commit comments