Unit, Integration, and End-to-End Tests
Overview
This skill maps the three test layers to concrete tooling: unit and integration tests with Vitest (a Vite-native test runner) and end-to-end browser tests with Playwright. Read it when setting up a test suite in a Vite/TypeScript stack and deciding which framework runs which layer. The strategy for how many tests live at each layer is covered by the sibling test-pyramid skill.
Official sources
Install / setup
# Unit & integration tests (Vitest)
npm install -D vitest
# End-to-end browser tests (Playwright) — scaffolds config, tests/, browsers
npm init playwright@latest
Core concepts
- Unit tests isolate a single function/module, run in milliseconds, and pinpoint failures. In Vitest they live in
*.test.ts / *.spec.ts files and use the expect matcher API.
- Integration tests exercise several units together (e.g. a service plus its DB or HTTP layer). Vitest runs these through the same runner; choose an environment such as
node or jsdom/happy-dom for DOM-dependent code.
- End-to-end (e2e) tests drive a real browser through real user journeys. Playwright is "an end-to-end test framework for modern web apps" that runs across Chromium, Firefox, and WebKit.
- Vitest is Vite-native: it reads your
vite.config.* by default, so existing Vite plugins and aliases work in tests out of the box (requires Vite >=6 and Node >=20).
- Playwright runs cross-browser in parallel, headless by default, with a headed mode and an interactive UI Mode for debugging, plus an HTML reporter.
- Auto-waiting: Playwright waits for elements to be actionable before acting, reducing flaky timing-based failures common in older e2e tools.
Best practices
- Run unit/integration tests with Vitest in watch mode during development (
vitest) and once in CI (vitest run).
- Keep e2e tests few and focused on critical paths; they are the slow, broad top of the pyramid.
- Prefer Playwright's web-first locators and auto-waiting assertions over manual sleeps to avoid flaky tests.
- Run Playwright across the browsers your users actually use; it is supported on Windows, Linux, and macOS, locally or in CI.
Common pitfalls
- Using Playwright (or any e2e tool) for logic that a Vitest unit test could check → push the assertion down to the cheapest layer.
- Mixing the
@vitest/browser Playwright provider with a separate @playwright/test install in one project can conflict → pick one driver per project (per upstream issues).
- Forgetting to select a DOM environment in Vitest for component/DOM tests → set
environment: 'jsdom' (or happy-dom) so document exists.
Examples
// Vitest unit test — sum.test.ts
import { expect, test } from 'vitest'
import { sum } from './sum'
test('adds numbers', () => {
expect(sum(2, 3)).toBe(5)
})
// Playwright e2e test — example.spec.ts
import { test, expect } from '@playwright/test'
test('homepage has title', async ({ page }) => {
await page.goto('https://example.com')
await expect(page).toHaveTitle(/Example/)
})
Further reading
Related skills
- ../testing-strategy-pyramid — how many tests to write per layer
- ../tdd-and-coverage — red-green-refactor and measuring coverage
1---2name: testing-unit-integration-e2e3description: The three test layers and the tooling that implements them — Vitest for unit/integration and Playwright for end-to-end browser tests.4domain: engineering5category: engineering6tags: [testing, vitest, playwright, e2e, integration, unit-tests]7official_sources:8 - https://vitest.dev/guide/9 - https://playwright.dev/docs/intro10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/testing-unit-integration-e2e13---1415# Unit, Integration, and End-to-End Tests1617## Overview18This skill maps the three test layers to concrete tooling: **unit** and **integration** tests with Vitest (a Vite-native test runner) and **end-to-end** browser tests with Playwright. Read it when setting up a test suite in a Vite/TypeScript stack and deciding which framework runs which layer. The strategy for how many tests live at each layer is covered by the sibling test-pyramid skill.1920## Official sources21- Docs: Vitest — https://vitest.dev/guide/ ; Playwright — https://playwright.dev/docs/intro22- Repo: https://github.com/vitest-dev/vitest ; https://github.com/microsoft/playwright23- Install: https://vitest.dev/guide/ ; https://playwright.dev/docs/intro2425## Install / setup26```bash27# Unit & integration tests (Vitest)28npm install -D vitest2930# End-to-end browser tests (Playwright) — scaffolds config, tests/, browsers31npm init playwright@latest32```3334## Core concepts35- **Unit tests** isolate a single function/module, run in milliseconds, and pinpoint failures. In Vitest they live in `*.test.ts` / `*.spec.ts` files and use the `expect` matcher API.36- **Integration tests** exercise several units together (e.g. a service plus its DB or HTTP layer). Vitest runs these through the same runner; choose an environment such as `node` or `jsdom`/`happy-dom` for DOM-dependent code.37- **End-to-end (e2e) tests** drive a real browser through real user journeys. Playwright is "an end-to-end test framework for modern web apps" that runs across Chromium, Firefox, and WebKit.38- **Vitest is Vite-native:** it reads your `vite.config.*` by default, so existing Vite plugins and aliases work in tests out of the box (requires Vite >=6 and Node >=20).39- **Playwright runs cross-browser in parallel,** headless by default, with a headed mode and an interactive UI Mode for debugging, plus an HTML reporter.40- **Auto-waiting:** Playwright waits for elements to be actionable before acting, reducing flaky timing-based failures common in older e2e tools.4142## Best practices43- Run unit/integration tests with Vitest in watch mode during development (`vitest`) and once in CI (`vitest run`).44- Keep e2e tests few and focused on critical paths; they are the slow, broad top of the pyramid.45- Prefer Playwright's web-first locators and auto-waiting assertions over manual sleeps to avoid flaky tests.46- Run Playwright across the browsers your users actually use; it is supported on Windows, Linux, and macOS, locally or in CI.4748## Common pitfalls49- Using Playwright (or any e2e tool) for logic that a Vitest unit test could check → push the assertion down to the cheapest layer.50- Mixing the `@vitest/browser` Playwright provider with a separate `@playwright/test` install in one project can conflict → pick one driver per project (per upstream issues).51- Forgetting to select a DOM environment in Vitest for component/DOM tests → set `environment: 'jsdom'` (or `happy-dom`) so `document` exists.5253## Examples54```ts55// Vitest unit test — sum.test.ts56import { expect, test } from 'vitest'57import { sum } from './sum'58test('adds numbers', () => {59 expect(sum(2, 3)).toBe(5)60})61```62```ts63// Playwright e2e test — example.spec.ts64import { test, expect } from '@playwright/test'65test('homepage has title', async ({ page }) => {66 await page.goto('https://example.com')67 await expect(page).toHaveTitle(/Example/)68})69```7071## Further reading72- Vitest config reference: https://vitest.dev/config/73- Playwright writing tests: https://playwright.dev/docs/writing-tests7475## Related skills76- ../testing-strategy-pyramid — how many tests to write per layer77- ../tdd-and-coverage — red-green-refactor and measuring coverage