This site has 38 test files on the front-end and 11 end-to-endTest that opens the app in a real browser and replays a user path, from the click to the server response, to check the whole flow works. ones, which run on every push before deploy. It is not vanity: it is what lets me change the menu, the contact form or the blog without being afraid of breaking a page I did not even open.
Even so, "front-end testing" is still a confusing subject for a lot of people, because every source calls things by a different name. In this guide I organize the types of tests, show the tools that are worth it today, with code examples, and tell you what usually goes wrong. The back-end has its own guide, with databases, contracts and load.

Why Test the Front-end
A front-end bug rarely brings the server down. It does something worse: the pay button disappears on mobile, the form accepts an empty email, the English page shows text in Portuguese. Nobody gets an alert, and the one who finds out is the user, who leaves.
Automated tests solve three things:
- Confidence to change. Refactoring a component that thirty screens use becomes safe when something warns you that one of them broke.
- Documentation that does not lie. A test that says "shows an error when the APIContract that defines how one program asks another for data or actions: which endpoints to call, what to send and what comes back. On the web, usually HTTP with JSON. fails" explains the behavior better than a comment, and it fails if the behavior changes.
- Fast feedback. DORA, Google's research program on software delivery, recommends that test results arrive in less than ten minutes, on your machine and in CI (DORA).
This became more important with AI writing code. The DORA 2025 report says that, without strong controls like automated testing, more volume of change leads to more instability, and it sums it up: AI does not fix a team, it amplifies what is already there (Google Cloud). I wrote about this in the article on AI code review.
The Types of Front-end Tests
Google's web.dev Learn Testing course (in Portuguese) is the best free taxonomy I have found. Summed up for the front-end:
| Type | What it checks | Most common tool | Speed |
|---|---|---|---|
| Static | Type errors, wrong imports, forbidden patterns, without running the code | TypeScript, ESLint | Instant |
| Unit | A small function with no dependencies: formatting a price, validating a tax ID | Vitest, Jest | Milliseconds |
| Component | An isolated component, rendered and used the way the user would use it | Testing Library, Storybook | Milliseconds |
| Integration | Several components together, with state and a (fake) network | Testing Library + MSW | Fast |
| End-to-end (E2E) | The whole flow in a real browser: log in, buy, log out | Playwright, Cypress | Seconds per test |
| Visual | Whether the screen is still pixel for pixel the same as an approved version | Playwright, Chromatic, Percy | Seconds |
| Accessibility | Contrast, labels, element roles, navigation | axe-core | Fast |
| Performance | LCP, INP, CLS and page weight | Lighthouse CI | Slow and noisy |
Some names that show up in job posts and in team conversations:
- Smoke: a handful of quick tests that tell you whether the basics are up, like opening the home page and logging in. It runs first, or right after deploy.
- Regression: makes sure what used to work still works and that a fixed bug does not come back. In practice, it is the whole suite running again. Note that definitions vary: web.dev treats regression as a type of smoke test, and the glossary of ISTQB, the best-known testing certification, treats it as change-related testing.
- Acceptance: confirms that the feature does what the business asked for. It can be manual or automated, often written in the "given, when, then" format.
Pyramid, Trophy or Honeycomb: How Much of Each
Mike Cohn's test pyramid says to have many unit tests, some integration tests and few end-to-end ones, because the ones at the top are slow and brittle (Martin Fowler). In 2015, Google's testing blog suggested 70% unit, 20% integration and 10% E2E "as a good first guess" (Google Testing Blog). The book Software Engineering at Google talks about roughly 80/15/5 (abseil.io).
On the front-end, the most cited model is Kent C. Dodds' testing trophy: a base of static analysis, few unit tests, lots of integration tests and some E2E (Kent C. Dodds). It starts from a line by Guillermo Rauch, creator of Next.js: "Write tests. Not too many. Mostly integration." The reason is practical: a test that renders the whole screen and clicks the buttons catches more real bugs than ten tests of isolated functions, and it still runs fast.
Martin Fowler ends the fight in a 2021 article: the discussion leads nowhere, because each side calls different things "unit" (Martin Fowler). What everyone agrees on:
Unit and Component Tests in Practice
For a new project, the standard today is Vitest. It uses the same configuration as Vite, its API is almost the same as Jest's, and its own docs say that, in most projects, it works as a drop-in replacement (Vitest). Angular now creates new projects with Vitest instead of Karma (Angular). Jest is still active and is the obvious choice in a large codebase that already uses it and in React Native.
In State of JS 2025, an annual survey with 13,002 responses, Jest still shows up as the most used, but the survey's own text says Vitest is rising so fast that it would be no surprise if it passed Jest the following year (State of JS). The sample is made of people who chose to answer, so it counts as a trend, not as a census.
What matters most is not the runner, it is how you test the component. Testing Library has a principle I follow to the letter: the more your tests resemble the way your software is used, the more confidence they give you (Testing Library). In practice:
- Find the element the way the user finds it. The recommended order is
getByRole(button, heading, field), thengetByLabelText,getByTextand, last,getByTestId(Testing Library). As a bonus, if the test cannot find the button by its role, a screen reader cannot either. - Interact with
user-event, notfireEvent. user-event simulates the full interaction: it does not click a hidden element or type into a disabled field (Testing Library). - Do not test implementation details. Checking the value of a
useStateor the name of an internal function leads to false negatives (the test breaks in a refactor that changed nothing for the user) and false positives (the test passes with a broken screen) (Kent C. Dodds).
A component test with Vitest, Testing Library and user-event looks like this:
// Counter.test.tsx
import { test, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Counter } from './Counter'
test('adds one on each click', async () => {
const user = userEvent.setup()
render(<Counter />)
await user.click(screen.getByRole('button', { name: 'Add' }))
expect(screen.getByText('Total: 1')).toBeInTheDocument()
})The test does not know whether the component uses useState, useReducer or Zustand. If you swap the state tomorrow, it keeps passing, which is exactly what you want.
Hooks: prefer testing the hook through the component that uses it. For a reusable library hook, renderHook comes from @testing-library/react itself. Snapshots: they work for small, stable output; a 600-line snapshot nobody reads turns into an "update" button (Kent C. Dodds). And a DOM snapshot does not compare browsers: that is what visual testing is for.
In Vue, the foundation is Vue Test Utils; in Angular, TestBed, now with Vitest by default. And in Next.js with async Server Components, the docs themselves say Vitest does not support them yet and recommend E2E for them (Next.js).
Mocking the Network with MSW
Almost every screen calls an API. You can replace fetch with a mock using vi.mock, but then the test is tied to how the code calls the network. MSW (Mock Service Worker) intercepts the request at the network level: the component makes a normal fetch and gets the response you defined. The same mock file works for tests, for Storybook and for developing without a back-end (MSW).
// src/mocks/node.ts
import { setupServer } from 'msw/node'
import { http, HttpResponse } from 'msw/http'
// In Node, use an absolute URL: there is no "origin" to resolve '/api/user'
export const server = setupServer(
http.get('https://api.example.com/user', () => HttpResponse.json({ name: 'Ana' })),
)// Profile.test.tsx
import { test, expect, beforeAll, afterEach, afterAll } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { http, HttpResponse } from 'msw/http'
import { server } from './mocks/node'
import { Profile } from './Profile'
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
test('shows an error when the API fails', async () => {
server.use(http.get('https://api.example.com/user', () => new HttpResponse(null, { status: 500 })))
const user = userEvent.setup()
render(<Profile />)
await user.click(screen.getByRole('button', { name: /load/i }))
expect(await screen.findByRole('alert')).toHaveTextContent(/try again/i)
})This is an integration test in the trophy sense: the whole screen, with state and network, testing the error path, which is the one people forget most. findByRole waits for the element to appear, with no setTimeout in the test.
End-to-end Tests with Playwright
E2E opens a real browser, navigates the site and checks the result. It is the test that most resembles the user, and also the slowest one and the one that breaks most often for silly reasons. That is why it only covers the journeys that cannot fail: login, sign-up, checkout, contact form.
In 2026, Playwright is the most common choice for a new project. In State of JS 2025, it and Vitest were the tools that gained the most usage. Cypress is still active and has a very good debugging experience, but it has limitations that its own docs list (Cypress):
| Playwright | Cypress | |
|---|---|---|
| Browsers | Chromium, Firefox and WebKit | Chrome, Edge, Firefox; experimental WebKit |
| Parallelism and splitting in CI | Free, with --shard |
Native parallelism requires Cypress Cloud |
| Multiple tabs and domains | Yes | One domain per test (cy.origin for the others) |
| Debugging | Trace viewer, UI mode, codegen | Interactive runner with "time travel" |
| Languages | JS/TS, Python, Java, .NET | JS/TS |
An E2E test with Playwright, with an accessibility check included:
import { test, expect } from '@playwright/test'
import AxeBuilder from '@axe-core/playwright'
test('login leads to the dashboard, with no accessibility violations', async ({ page }) => {
await page.goto('/login')
await page.getByLabel('Email').fill('ana@example.com')
await page.getByLabel('Password').fill('test-password')
await page.getByRole('button', { name: 'Sign in' }).click()
// The assertion waits for the heading to appear, with no sleep
await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible()
const { violations } = await new AxeBuilder({ page }).analyze()
expect(violations).toEqual([])
})Notice that the selectors are the same as Testing Library's: label and role, not a CSS class. Playwright's official best practices ask for that, along with keeping each test isolated, mocking only third-party services and recording a trace on the first retry instead of a video (Playwright).
The E2E tests on this site run with Playwright and axe in two stages: the whole suite against the candidate image, before deploy, and a smoke test against the published site, after. The contact form test intercepts the submission with page.route and checks what would be sent, so no test message ever reaches the production database.
Visual, Accessibility and Performance Testing
Visual. Playwright's toHaveScreenshot compares the screen with an approved image and fails if it changes beyond a tolerance (Playwright). It is free, but the image changes with the operating system, fonts and graphics card, so the baseline needs to be generated in the same environment as CI (usually Linux, in Docker). Services like Chromatic, Percy and Argos store the images and show the difference for someone to approve. If you come across a Lost Pixel tutorial, know that the project was archived in April 2026.
Accessibility. axe-core automates a good part of the WCAGW3C guidelines that define how to make web content accessible to people with disabilities, with testable criteria at levels A, AA and AAA. rules. In a study by Deque, the company that maintains axe, automated tests found on average 57% of the issues across more than 13,000 pages (Deque). The rest needs people: whether the alt text makes sense, whether the focus order is logical, how it works with a screen reader. And jsdom does not compute contrast, so jest-axe turns that rule off (jest-axe). What WCAG 2.2 asks for and how to test each layer in React is in the React accessibility guide.
Performance. Lighthouse CI runs Lighthouse in the pipeline and fails the build if the score or a metric goes over budget. The result varies from one run to the next: the docs say the median of five runs is twice as stable as a single one, and ask you not to run several Lighthouse instances at the same time on the same machine (Lighthouse). I showed real measurements in the article on Core Web Vitals and Lighthouse.
How to Avoid Flaky Tests
A flaky test is one that passes and fails without you changing anything. It is a suite's worst enemy, because it teaches the team to click "re-run". Google measured that almost 16% of its tests had some level of flakiness (Google Testing Blog), and their book says that, as the flaky rate approaches 1%, tests start to lose value.
What helps most on the front-end:
- Never use
sleeporwaitForTimeout. Use an assertion that waits (findBy*,toBeVisible). - Each test creates its own data and does not depend on order. Playwright's
--shufflehelps find a test that depends on another. - Locate by role and label, not by CSS class or position in a list.
- Mock third-party services (payments, maps, analytics), not your own back-end, in the E2E tests for critical journeys.
- Do not hide flakiness with retries. Quarantine the test and fix it by a deadline, as Fowler recommends (Martin Fowler).
Coverage: How Much Is Enough
Code coverage measures which lines the tests executed. It does not measure whether the test checked anything: a test with no assertion raises coverage and protects nothing. Google uses 60% as "acceptable", 75% as "commendable" and 90% as "exemplary" as a reference, without imposing a top-down target, and says the value of coverage lies in showing what was not covered (Google Testing Blog).
Use the report to find gaps in important code, not as a number to hit. If you want to measure the quality of the assertions, use mutation testing, which I explain in the back-end article.
Where to Start in a Project with No Tests
- Turn on static checks. TypeScript in strict mode and ESLint with
eslint-plugin-testing-library. It is the cheapest test there is. - Install Vitest and Testing Library and write a test for the screen that causes the most trouble, not the easiest one.
- Every fixed bug gets a test that fails before the fix. In a few months you have a regression suite made of what actually broke.
- Add MSW and test the error path of the screens that call an API.
- Write three to five E2E tests with Playwright for the journeys that bring in money or headaches.
- Put everything in CI, running on every pull request. A test that only runs on someone's machine protects nobody.
If the project is built from a specification, the acceptance criteria already become the list of tests. I explained how I do that in the article on Spec-Driven Development.
Read and Watch More
In Portuguese:
- web.dev. Types of automated tests (in Portuguese). Google's free course, the best taxonomy to start with.
- Google for Developers. Testing the front-end (in Portuguese). A short overview, covering contrast, forms and performance.
- Alura. Tips for unit and integration tests on the front-end (in Portuguese). A light, code-free introduction, good for someone who has never written a test.
- Alura. Demystifying unit tests in Vue (in Portuguese). Argues for testing the component's public contract, with an example that catches the classic "5" + 1 = "51".
In English:
- Kent C. Dodds. Common mistakes with React Testing Library. The list of mistakes I see most in code review.
- Ham Vocke. The Practical Test Pyramid. Long, but it is the most complete text on test layers.
Books:
- Testing JavaScript Applications, Lucas da Costa (Manning, 2021). From unit to E2E, written by a Brazilian who maintains Chai and Sinon.
- The Art of Unit Testing, 3rd edition, Roy Osherove and Vladimir Khorikov (Manning, 2024). The new edition has examples in JavaScript.
- Software Engineering at Google, Winters, Manshreck and Wright (O'Reilly, 2020). Chapters 11 to 14 are about testing, and the book is free online.
Frequently Asked Questions
What is the difference between a unit test and an integration test on the front-end?
A unit test checks a small piece on its own, like a function that formats a date. An integration test checks several pieces together, like the checkout screen with the form, the state and the call to the fake API. On the front-end, integration tests usually catch more bugs for the same effort.
Vitest or Jest in 2026?
For a new project with Vite, Next, Vue or Angular, Vitest. For a large codebase that already uses Jest, or for React Native, stay with Jest until you have a concrete reason to migrate.
Playwright or Cypress?
To start from scratch, Playwright: it runs on three browser engines, splits tests in CI for free and works in several languages. If the team already knows Cypress well and the suite is healthy, there is no rush to switch.
Do I need to test everything?
No. Test what hurts when it breaks: business rules, forms, payment flows, screens that change a lot. Trivial code and third-party libraries do not need your tests.
Do front-end tests replace manual QA?
No. Automation handles what repeats. Exploratory testing, accessibility review with a screen reader and the question "does this make sense for the user?" are still work for people.
References
- web.dev. Learn Testing: test types (in Portuguese).
- Martin Fowler. Test Pyramid and On the Diverse And Fantastical Shapes of Testing.
- Kent C. Dodds. The Testing Trophy and Testing Classifications.
- Google Testing Blog. Just Say No to More End-to-End Tests, Flaky Tests at Google and Code Coverage Best Practices.
- Testing Library. Guiding principles and query priority.
- Vitest. Comparisons and browser mode.
- MSW. Quick start.
- Playwright. Best practices and visual tests.
- Cypress. Trade-offs.
- Deque. Automated accessibility testing study.
- Next.js. Testing with Vitest.
- State of JS 2025. Testing.
- DORA. Test automation.