nodeTestPreset()
nodeTestPreset() returns a Vite plugin named @repobuddy/vitest/node-preset that sets the Vitest test config for
Node.js, jsdom, happy-dom, or edge-runtime tests.
import { defineConfig } from 'vitest/config'import { nodeTestPreset } from '@repobuddy/vitest/config/node'
export default defineConfig({ plugins: [nodeTestPreset()],})function nodeTestPreset(options?: { includeGeneralTests?: boolean | undefined includeLoadTests?: boolean | undefined environment?: 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime' | (string & {}) | undefined}): PluginImport it from @repobuddy/vitest/config/node or @repobuddy/vitest/config. The options type is not exported; use
Parameters<typeof nodeTestPreset>[0] if you need it.
Options
Section titled “Options”| Option | Type | Default | Effect |
|---|---|---|---|
includeGeneralTests |
boolean |
false |
Adds buddyConfigDefaults.include.testGeneral (for example a.spec.ts) to test.include |
includeLoadTests |
boolean |
false |
Adds buddyConfigDefaults.include.testLoad (a.load.ts) to test.include |
environment |
'node' | 'jsdom' | 'happy-dom' | 'edge-runtime' | string |
'node' |
Sets test.environment. 'jsdom' and 'happy-dom' also add buddyConfigDefaults.include.testBrowser. |
Effective config
Section titled “Effective config”The plugin’s config() hook returns this test config with no options:
{ "expect": { "poll": { "timeout": 5000 } }, "server": { "deps": { "fallbackCJS": true } }, "testTimeout": 30000, "include": [ "{src,source,code,tests}/**/*.{spec,test,unit,accept,integrate,system,perf,stress,study}.node*.{js,cjs,mjs,ts,cts,mts}" ], "environment": "node", "coverage": { "include": ["{src,source,code}/**/*.{js,mjs,cjs,ts,jsx,tsx,cts,mts}"], "exclude": [ "**/*.{spec,test,unit,accept,integrate,system,perf,stress,study,load}.{js,jsx,cjs,mjs,ts,tsx,cts,mts}", "**/*.{spec,test,unit,accept,integrate,system,perf,stress,study,load}.*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}", "**/*.stories.{js,mjs,jsx,tsx}" ] }}The options change only test.include and test.environment. test.include is built in this order:
| Options | test.include |
test.environment |
|---|---|---|
| none | testNode |
'node' |
includeGeneralTests: true |
testNode, testGeneral |
'node' |
includeLoadTests: true |
testNode, testLoad |
'node' |
environment: 'jsdom' |
testNode, testBrowser |
'jsdom' |
environment: 'happy-dom' |
testNode, testBrowser |
'happy-dom' |
environment: 'edge-runtime' |
testNode |
'edge-runtime' |
All options together give testNode, testGeneral, testLoad, then testBrowser (for jsdom or happy-dom). The
names refer to the globs in buddyConfigDefaults.include.
Behavior
Section titled “Behavior”- The hook sets
process.env.TZtoGMTwhenTZis not already set, so date output is the same on every machine. ATZyou set yourself is kept. - The plugin runs in Vite’s normal phase (no
enforce). - Vitest needs the package for a non-
nodeenvironment installed, for examplejsdomorhappy-dom. edge-runtimeand any other environment string do not add the browser test globs.- In a Vitest projects setup, Vitest reads
coveragefrom the root config only, so the preset’scoveragehas no effect there. Set it in the root config. See Node.js and browser tests together.
Your config and the preset
Section titled “Your config and the preset”Vite merges the plugin’s result on top of your config:
- Arrays are concatenated. A
test.includeyou set is kept, and the preset’s globs are added after it. - Other values from the preset replace yours. A
test.environmentortest.testTimeoutin the same config has no effect. Use theenvironmentoption instead.
To override a preset value, add a plugin after the preset:
export default defineConfig({ plugins: [ nodeTestPreset(), { name: 'override-timeout', config: () => ({ test: { testTimeout: 1000 } }) }, ],})Examples
Section titled “Examples”Run Node.js and general tests:
export default defineConfig({ plugins: [nodeTestPreset({ includeGeneralTests: true })],})Run browser test files in jsdom:
export default defineConfig({ plugins: [nodeTestPreset({ environment: 'jsdom' })],})