examples/with-vitest/README.md
This is a community-maintained example. If you experience a problem, please submit a pull request with a fix. GitHub Issues will be closed.
This example is based on the basic example (npx create-turbo@latest) to demonstrate how to use Vitest and get the most out of Turborepo's caching.
This example demonstrates two approaches to Vitest configuration:
Package-level caching (Recommended): Each package has its own Vitest configuration that imports shared settings from @repo/vitest-config. This approach leverages Turborepo's caching effectively.
Vitest Projects: A root vitest.config.ts uses Vitest's projects feature for unified test running during development.
First, install dependencies and build the shared configuration package:
pnpm install
pnpm --filter=@repo/vitest-config build
pnpm test: Runs tests in each package using Turborepo (leverages caching)pnpm test:projects: Runs tests using Vitest's projects featurepnpm test:projects:watch: Runs tests using Vitest's projects feature in watch modepnpm report: Merges package coverage with Vitest's native --merge-reports commandpnpm view-report: Creates the merged coverage report and opens it in your browserThe example uses a shared @repo/vitest-config package that exports:
sharedConfig: Base configuration with coverage settingsbaseConfig: For Node.js packages (like math)uiConfig: For packages requiring jsdom environment (like web, docs)Package-level test tasks write Vitest blob reports to each package's coverage/blob directory, so Turborepo can cache each package's coverage blob independently. After running tests, use Vitest's native merge command to create a combined HTML and text coverage report:
pnpm test
pnpm report
The merge-blob-reports task stages the package blobs into packages/vitest-config/coverage/merged-blob, then the report task runs vitest --merge-reports to write the merged HTML report to packages/vitest-config/coverage/report.
[!TIP] Vercel Remote Cache is free for all plans. Get started today at vercel.com.
Turborepo can use a technique known as Remote Caching to share cache artifacts across machines, enabling you to share build caches with your team and CI/CD pipelines.
By default, Turborepo will cache locally. To enable Remote Caching you will need an account with Vercel. If you don't have an account you can create one, then enter the following commands:
cd my-turborepo
npx turbo login
This will authenticate the Turborepo CLI with your Vercel account.
Next, you can link your Turborepo to your Remote Cache by running the following command from the root of your Turborepo:
npx turbo link
Learn more about the power of Turborepo: