Node.js and browser tests together
This guide runs Node.js tests and browser tests in one vitest command. Each preset gets its own Vitest project, and the
root config owns coverage. This package’s own repository uses this layout.
-
Install the package, Vitest 5, the Playwright provider, and a coverage provider:
Terminal window pnpm add -D @repobuddy/vitest vitest @vitest/browser-playwright @vitest/coverage-v8npx playwright install chromium -
Create
vitest.config.node.ts:import { defineProject } from 'vitest/config'import { nodeTestPreset } from '@repobuddy/vitest/config/node'export default defineProject({plugins: [nodeTestPreset({ includeGeneralTests: true })],test: { name: 'node' },}) -
Create
vitest.config.browser.ts:import { defineProject } from 'vitest/config'import { browserTestPreset } from '@repobuddy/vitest/config/browser'export default defineProject({plugins: [browserTestPreset()],test: { name: 'browser' },})Turn on
includeGeneralTestsin one project only, or general tests run twice. -
Create the root
vitest.config.ts. Vitest readscoveragefrom the root config only, so set it here:import { defineConfig } from 'vitest/config'import { buddyConfigDefaults } from '@repobuddy/vitest/config/node'export default defineConfig({test: {coverage: {include: buddyConfigDefaults.include.source,exclude: buddyConfigDefaults.exclude.test,},projects: ['vitest.config.*.ts'],},}) -
Do not name a load test config
vitest.config.load.tsin this layout. Thevitest.config.*.tspattern would add it as a project. See Load tests.
Finished layout
Section titled “Finished layout”vitest.config.ts # projects + coveragevitest.config.node.ts # nodeTestPreset({ includeGeneralTests: true })vitest.config.browser.ts # browserTestPreset()Verify
Section titled “Verify”-
Run
pnpm vitest run. The output lists tests undernodeandbrowser. The browser instance shows asbrowser (chromium):✓ |node| src/sum.spec.node.ts > runs in GMT✓ |browser (chromium)| src/time-zone.spec.tsx > reports GMT -
Run
pnpm vitest run --coverage. One report covers files undersrc,source, andcode, without test files. -
Run one project with
pnpm vitest run --project node.