browserTestPreset()
browserTestPreset() returns a Vite plugin named @repobuddy/vitest/browser-preset that turns on Vitest browser mode,
headless, with the Playwright provider and one Chromium instance.
import { defineConfig } from 'vitest/config'import { browserTestPreset } from '@repobuddy/vitest/config/browser'
export default defineConfig({ plugins: [browserTestPreset()],})function browserTestPreset(options?: { includeGeneralTests?: boolean | undefined includeLoadTests?: boolean | undefined}): PluginImport it from @repobuddy/vitest/config/browser or @repobuddy/vitest/config. Both load
@vitest/browser-playwright, so install that peer. The options type is not exported; use
Parameters<typeof browserTestPreset>[0] if you need it.
Options
Section titled “Options”| Option | Type | Default | Effect |
|---|---|---|---|
includeGeneralTests |
boolean |
false |
Puts buddyConfigDefaults.include.testGeneral (for example a.spec.ts) first in test.include |
includeLoadTests |
boolean |
false |
Adds buddyConfigDefaults.include.testLoad (a.load.ts) to the end of test.include |
Effective config
Section titled “Effective config”The plugin’s config() hook returns this test config with no options and no test.name:
{ "expect": { "poll": { "timeout": 5000 } }, "server": { "deps": { "fallbackCJS": true } }, "testTimeout": 30000, "include": [ "{src,source,code,tests}/**/*.{spec,test,unit,accept,integrate,system,perf,stress,study}.{jsx,tsx}", "{src,source,code,tests}/**/*.{spec,test,unit,accept,integrate,system,perf,stress,study}.browser*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}" ], "browser": { "enabled": true, "headless": true, "provider": "<playwright() from @vitest/browser-playwright>", "instances": [{ "browser": "chromium", "screenshotFailures": false }] }, "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}" ] }, "setupFiles": ["@repobuddy/vitest/setup/browser"]}What changes it:
| Input | Change |
|---|---|
includeGeneralTests: true |
test.include is testGeneral, then testBrowser |
includeLoadTests: true |
testLoad is added after the other globs |
test.name: 'My Test' in your config |
The default instance gets name: 'My Test (chromium)' |
test.browser.instances in your config |
The preset adds no instance; yours are used as they are |
Behavior
Section titled “Behavior”- The plugin runs in Vite’s
prephase (enforce: 'pre'). Vitest 5 sets up browser mode in its ownpreplugin, which readstest.browser.enabledbefore normal plugins run. - The provider is always Playwright. There is no option to choose another provider.
- The default Chromium instance sets
screenshotFailures: false, so Vitest does not write a screenshot when a test fails. When you settest.browser.instancesyourself, this setting is not added to your instances. - The preset adds
@repobuddy/vitest/setup/browsertotest.setupFiles. - The preset does not install browsers. Run
npx playwright install chromium(or the browsers your instances name). - In a Vitest projects setup, Vitest reads
coveragefrom the root config only. Set it there. - Vite merges the preset’s result on top of your config: arrays such as
test.includeandtest.setupFilesare concatenated, and other values from the preset replace yours. To override one, add a plugin after the preset, as shown onnodeTestPreset().
Examples
Section titled “Examples”Run browser tests and general tests in Chromium:
export default defineConfig({ plugins: [browserTestPreset({ includeGeneralTests: true })],})Run in Firefox and WebKit instead of Chromium:
export default defineConfig({ plugins: [browserTestPreset()], test: { browser: { instances: [{ browser: 'firefox' }, { browser: 'webkit' }], }, },})