Browser mode
Setup
- Desktop breakpoints:
await page.viewport(1280, 800)in the test, orviewporton the browser instance - Headless Chromium without a GPU: WebGL (maps, canvas charts) needs
provider: playwright({ launchOptions: { args: ['--use-gl=angle', '--use-angle=swiftshader'] } }). For headed debugging, runvitest --project browser --browser.headless=false --watchand passlaunchOptions.slowMo - File parallelism:
test.fileParallelism: falseon the browser project andbrowser.instances[].fileParallelism: falseboth run browser test files one at a time (measured for each option, 3 runs: two 3s files took 6.3s with either option vs 3.4s by default). Use the per-instance option when browsers need different settings
vitest-browser-svelte
- v3 gives
/purefull export parity with the main entry and exports theRenderResulttype - The main entry calls
cleanup()inbeforeEach, so the last test's DOM stays mounted until the file ends.cleanup()removes every component rendered withrender(docs), andvitest-browser-svelte/purenever calls it on its own - Sequential tests under
/pure: callcleanup()inafterEach, or await theunmount()from each render
- Don't use
it.concurrentfor browser component tests. Concurrent tests share one page, and the retryingexpect.elementbreaks there. Evidence (measured, 3 runs each, with two or threeit.concurrenttests in one file):- The global
expect.elementpasses when called right afterrender, and throwsexpect.poll() must be called inside a testonce the test has awaited something (a 300ms wait) while another concurrent test ran. The globalexpectfinds its test through the current-test tracker, which concurrent tests do not keep (vitest/dist/chunks/index.*.js,createExpectPoll) - The test-context
expect, which the test API docs require for assertions in concurrent tests, has no.element:TypeError: expect.element is not a function. That failure also reportsCannot take a screenshot in a concurrent test because concurrent tests run at the same time in the same iframe(@vitest/browser/dist/context.js) - A shared
cleanup()inafterEachremoved a still-running test's component (document.bodywas empty when it asserted), because hooks of concurrent tests overlap (parallelism guide) - No Vitest doc or issue describes the
expect.elementgap; vitest#9751 (open) reports that concurrent browser tests also corrupt the sharedexpect.elementtimeout
- The global
- Under
/pure, a document-wide query can hit a previous test's element, and the render result's own selectors are document-wide too (screen.getByRole('button')found both of two rendered buttons, measured). Scope to the render's container withpage.elementLocator(screen.container).getByRole(...)(found one, measured), or give each render a unique id. A test that reads DOM left by the previous test passes in the full file and fails when run alone (-t,--tags-filter, shuffle)
Locators
- Locators match text exactly (Vitest 5): full and case-sensitive (measured for
getByText,getByLabelText, and thenameofgetByRole).page.getByText('Hello')does not findHello, World;getByRole('button', { name: 'Submit' })does not findSubmit form. Pass{ exact: false }per call, use aRegExp, or setbrowser.locators.exact: falseto restore substring matching - The
pagemethods aregetByRole,getByText,getByLabelText,getByPlaceholder,getByTitle,getByAltText,getByTestId(measured; there is nogetByLabelorgetByPlaceholderText) locator.query()returns the element ornullwithout waiting: use it for absence checks insideexpect.poll.locator.element()returns the DOM node and throws when nothing matchesawait expect.element(page.getByRole('listitem')).toHaveLength(3)asserts the match count (measured)
CSS selector locators (locators.extend)
.locator(selector) is intentionally protected on vitest's Locator type (vitest-dev/vitest#7969). The official escape hatch is locators.extend:
// tests/browser/setup-locators.ts
import { locators } from "vitest/browser";
import type { Locator } from "vitest/browser";
locators.extend({
css(selector: string) {
return selector;
},
});
declare module "vitest/browser" {
interface LocatorSelectors {
css(selector: string): Locator;
}
}Wire it via setupFiles in the browser project config. Then .css() is properly typed, no @ts-expect-error needed.
Critical: do NOT name the extend function locator, it shadows the internal protected locator() method and causes infinite recursion (RangeError: Maximum call stack size exceeded). Use css or another name.
Waiting
await expect.element(locator)retries a locator assertion;expect.poll(() => value)retries any expression (DOM reads, store values, request logs).expect.pollrejects at its owntimeoutvi.waitFor(() => expect(spy).toHaveBeenCalledWith(...))works in browser mode (measured, DOM text and spy calls)