Tests should cover behavior users can observe, not maximize a test count. Pure logic can run in Node; DOM-dependent components can use jsdom; use end-to-end browser tests only when layout, navigation or a cross-page journey affects the result. This guide wires those layers into a Vite and React project, then shows how to verify them in CI. Testing is part of the wider frontend engineering workflow; see the Vite build guide for build configuration.
Start by asking which runtime contains the behavior under test. If browser APIs, layout and navigation are not part of the requirement, avoid making every assertion pay for a browser launch. Conversely, jsdom cannot prove layout or cross-browser behavior.
| Behavior under test | Environment and tools | What it verifies | What it does not prove |
|---|---|---|---|
| Pure functions and state logic | Vitest + Node | Inputs, edge cases and state transitions | DOM rendering or browser interaction |
| Component semantics and interaction | Vitest + jsdom + Testing Library | Roles, text, events and DOM state | CSS layout or native browser differences |
| Critical user journey | Playwright in a real browser | Navigation, interaction and key paths | Uncovered browsers or production dependencies |
These layers prove different things; they do not need to be stacked in every test. If a component test verifies a state change, reserve browser assertions for journeys that depend on browser behavior.
Prepare a Repeatable Test Environment
The compatibility baseline below reflects official documentation checked on 2026-11-28: Vite requires Node.js 20.19+ or 22.12+, while Vitest 5 requires Node.js 22.12+ and Vite 6.4+. Node.js 24 LTS satisfies both sets of minimums. Create a Vite React and TypeScript starter:
node --version
npm create vite@latest frontend-test-lab -- --template react-ts
cd frontend-test-lab
npm install
npm install -D vitest @testing-library/react @testing-library/dom @testing-library/user-event @testing-library/jest-dom jsdom
You should see node --version report 24.x, npm install exit successfully and create a lockfile, and npm ls vite vitest show installed packages without an invalid dependency notice. The Vite and Vitest requirements come from their official guides; after upgrades, check peer dependencies rather than relying on a version number alone.
The Vite starter already has vite.config.ts. Use vitest/config and declare the DOM environment in the same config so the app's Vite plugins and aliases remain available:
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
setupFiles: ['./src/test/setup.ts'],
include: ['src/**/*.test.{ts,tsx}'],
},
})
Add "test": "vitest run" to package.json scripts. Create setup.ts for the Vitest matchers and React cleanup:
import '@testing-library/jest-dom/vitest'
import { cleanup } from '@testing-library/react'
import { afterEach } from 'vitest'
afterEach(() => cleanup())
You should see npm test collect src/**/*.test.tsx. If a DOM test reports document is not defined, check that environment is jsdom and that jsdom is installed. Vitest reads Vite configuration by default; a separate test config must not accidentally omit app plugins or aliases.
Test Components Through User-Visible Behavior
Testing Library is not a test runner. Paired with Vitest, it lets assertions use accessible roles, labels and visible text. Avoid starting with CSS classes or component state: those implementation details can change while user behavior remains the same. This example tests the counter button in the Vite starter:
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { expect, it } from 'vitest'
import App from './App'
it('increments the visible counter', async () => {
const user = userEvent.setup()
render(<App />)
await user.click(screen.getByRole('button', { name: /count is 0/i }))
expect(screen.getByRole('button', { name: /count is 1/i })).toBeInTheDocument()
})
You should see npm test report the test as passed and exit with code 0; changing the accessible button name or click behavior should make the assertion fail. If getByRole cannot find the control, inspect its accessible name and render conditions. Use findBy... for asynchronous content rather than hiding timing problems with a fixed delay.
Pure functions do not need jsdom. You can split them into a Node project or put // @vitest-environment node at the top of an individual test file. Start with one environment; split configurations only when the DOM environment causes a concrete problem. Role and label queries usually reflect how users find controls, while a test ID remains reasonable for a complex element without stable semantics.
Use Playwright for Critical Journeys
Use Playwright Test when the result depends on real browser behavior or a multi-page journey. It provides browser execution, assertions and reporting; browser binaries must match the installed Playwright version. This example uses Chromium:
npm install -D @playwright/test
npx playwright install chromium
You should see installation exit with code 0 and npx playwright --version print the installed version. If a Linux CI image lacks system libraries, install Playwright's system dependencies while preparing the image rather than treating a browser-install failure as an application test failure.
Create playwright.config.ts at the project root and use the Vite dev server as the test target:
import { defineConfig, devices } from '@playwright/test'
export default defineConfig({
testDir: './tests',
use: {
baseURL: 'http://127.0.0.1:5173',
trace: 'on-first-retry',
},
projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
webServer: {
command: 'npm run dev -- --host 127.0.0.1',
url: 'http://127.0.0.1:5173',
reuseExistingServer: !process.env.CI,
},
})
Add tests/home.spec.ts:
import { expect, test } from '@playwright/test'
test('home page counter responds to a click', async ({ page }) => {
await page.goto('/')
await page.getByRole('button', { name: /count is 0/i }).click()
await expect(page.getByRole('button', { name: /count is 1/i })).toBeVisible()
})
You should see npx playwright test report the Chromium test as passed. On failure, run npx playwright show-report to inspect the failed step and attachments. CI must install the matching browser and retain the report or trace; do not assume a browser cached on a developer machine exists in the CI runner.
Put Feedback Layers in CI
Run formatting, type checks and unit/component tests on each change. Run browser tests for critical user paths after the application builds. Keep the commands in package.json so local development and CI use the same flags:
{
"scripts": {
"test": "vitest run",
"test:e2e": "playwright test",
"build": "tsc -b && vite build"
}
}
You should see npm test, npm run build and npm run test:e2e exit with code 0; the E2E job must also provision its browser. If failures appear only under parallel execution, check shared state, fixed ports and external-service dependencies before tuning workers or retries. Retries can collect evidence about an intermittent failure; they should not turn an unreliable test into a silent pass.
Limitations
- jsdom simulates DOM APIs; it is not a full browser and cannot prove CSS layout, native browser behavior or cross-browser compatibility.
- The Playwright example configures Chromium only. Add Firefox, WebKit or mobile coverage according to the product support matrix and install those browsers in CI.
- Maintain test dependencies with the project's Node, Vite and React major versions and lockfile. Vitest 5 requires Node.js 22.12+ and Vite 6.4+; upgrade the runtime or select a supported dependency combination when those conditions are not met.
- E2E tests contact a real application server. Use isolated test environments and disposable data for databases, payments or email; never perform writes against production accounts.
FAQ
What Node.js and Vite versions does Vitest 5 require?
Use Node.js 22.12+ and Vite 6.4+ for Vitest 5. In the project directory represented by package-lock.json, run npm ls vite vitest to check installed versions and their dependency relationship. If CI has an older Node version, updating a developer's machine alone will not fix it; align the CI runtime, reinstall dependencies and rerun the tests.
Which behaviors belong in Playwright rather than jsdom?
Use Playwright when a result depends on browser layout, navigation or native interaction; keep component semantics and state changes in Testing Library. In the Vite starter, getByRole can locate the button named count is 0 and verify the update after a click. A passing jsdom test does not prove that the same page behaves identically in a browser.
Why doesn't jsdom prove Safari layout compatibility?
No: jsdom simulates DOM APIs rather than rendering the page in Safari 16.4+. Component tests can verify visible text, roles and state changes, but use Playwright's WebKit project in the target environment to inspect layout, native controls or browser-specific behavior. Treating a simulated DOM pass as cross-browser evidence would overstate what the test covers.
Related Providers
- Vitest: use it for unit or component tests that share Vite configuration; first check its minimum Node and Vite versions.
- Playwright: use it when a critical journey needs a real browser, reports and failure traces; CI must provision browser dependencies.
- GitHub Actions: use separate jobs for fast tests and browser tests when pull requests need automated checks and retained failure reports.
References
- Vitest getting started and compatibility: installation requirements, config reuse and one-shot test runs.
- Testing Library React guide: React Testing Library setup and user-centered testing principles.
- Playwright installation: runtime requirements, browser installation, execution and HTML reports.
- Playwright best practices: locator choices and reliable browser-test structure.
Community & Resources
- Frontend building: related engineering and build topics beyond testing.
- Vite build tool guide: understand how test configuration relates to development and production builds.
Original source: 16IDC Original ↗