Skip to content

Run Node.js tests

This guide sets up Vitest to run Node.js tests with nodeTestPreset().

  1. Install the package and Vitest 5:

    Terminal window
    pnpm add -D @repobuddy/vitest vitest
  2. Create vitest.config.ts and add the preset. Import from @repobuddy/vitest/config/node, which does not need the Playwright peer:

    import { defineConfig } from 'vitest/config'
    import { nodeTestPreset } from '@repobuddy/vitest/config/node'
    export default defineConfig({
    plugins: [nodeTestPreset()],
    })
  3. Name Node.js test files <name>.spec.node.ts under src, source, code, or tests. See Test file names for every id and extension.

  4. To also run general tests such as a.spec.ts, pass includeGeneralTests: true.

  5. To run .tsx and .browser.ts tests in a simulated DOM, set environment to 'jsdom' or 'happy-dom' and install that package:

    Terminal window
    pnpm add -D jsdom
  6. Add the scripts to package.json:

    {
    "scripts": {
    "test": "vitest run",
    "coverage": "vitest run --coverage"
    }
    }

Set Vitest options the preset also sets (such as test.environment or test.testTimeout) through the preset option or a later plugin, not in test. The preset’s values win. See Your config and the preset.

vitest.config.ts
import { defineConfig } from 'vitest/config'
import { nodeTestPreset } from '@repobuddy/vitest/config/node'
export default defineConfig({
plugins: [nodeTestPreset({ includeGeneralTests: true })],
})
  1. Create src/sum.spec.node.ts:

    import { expect, it } from 'vitest'
    it('runs in GMT', () => {
    expect(process.env['TZ']).toBe('GMT')
    })
  2. Run pnpm test. Vitest finds and passes the test. If you set TZ in your shell, the preset keeps your value and this test fails.