Skip to content

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.

  1. 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-v8
    npx playwright install chromium
  2. 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' },
    })
  3. 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 includeGeneralTests in one project only, or general tests run twice.

  4. Create the root vitest.config.ts. Vitest reads coverage from 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'],
    },
    })
  5. Do not name a load test config vitest.config.load.ts in this layout. The vitest.config.*.ts pattern would add it as a project. See Load tests.

vitest.config.ts # projects + coverage
vitest.config.node.ts # nodeTestPreset({ includeGeneralTests: true })
vitest.config.browser.ts # browserTestPreset()
  1. Run pnpm vitest run. The output lists tests under node and browser. The browser instance shows as browser (chromium):

    ✓ |node| src/sum.spec.node.ts > runs in GMT
    ✓ |browser (chromium)| src/time-zone.spec.tsx > reports GMT
  2. Run pnpm vitest run --coverage. One report covers files under src, source, and code, without test files.

  3. Run one project with pnpm vitest run --project node.