Run Node.js tests
This guide sets up Vitest to run Node.js tests with nodeTestPreset().
-
Install the package and Vitest 5:
Terminal window pnpm add -D @repobuddy/vitest vitest -
Create
vitest.config.tsand 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()],}) -
Name Node.js test files
<name>.spec.node.tsundersrc,source,code, ortests. See Test file names for every id and extension. -
To also run general tests such as
a.spec.ts, passincludeGeneralTests: true. -
To run
.tsxand.browser.tstests in a simulated DOM, setenvironmentto'jsdom'or'happy-dom'and install that package:Terminal window pnpm add -D jsdom -
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.
Finished config
Section titled “Finished config”import { defineConfig } from 'vitest/config'import { nodeTestPreset } from '@repobuddy/vitest/config/node'
export default defineConfig({ plugins: [nodeTestPreset({ includeGeneralTests: true })],})Verify
Section titled “Verify”-
Create
src/sum.spec.node.ts:import { expect, it } from 'vitest'it('runs in GMT', () => {expect(process.env['TZ']).toBe('GMT')}) -
Run
pnpm test. Vitest finds and passes the test. If you setTZin your shell, the preset keeps your value and this test fails.