Loading...
Loading...
Unit, integration, E2E, and the testing pyramid — what SDE-2 interviews expect.
Unit, integration, E2E, and snapshot testing — Jest, Vitest, React Testing Library, Playwright, Cypress, MSW, testing pyramid, and Web Vitals.
Frontend testing strategies: unit, integration, E2E, snapshot, and performance testing — Jest, Vitest, React Testing Library, Playwright, Cypress, MSW, the testing pyramid (70/20/10), Web Vitals, and memory leak detection.
Frontend testing is a core SDE-2 skill — interviews expect you to explain the testing pyramid, when to use unit vs integration vs E2E, and how tools like RTL, MSW, and Playwright fit together.
This module covers all test types with code examples, the 70/20/10 pyramid, snapshot testing trade-offs, performance measurement with Web Vitals, memory leak detection, and MSW for API mocking.
Good testing strategy balances speed and confidence: fast unit tests for logic, integration tests for component + API flows, and a thin layer of E2E tests for critical user journeys only.
Includes the 70/20/10 testing pyramid, RTL best practices, MSW for API mocking, Playwright login flows, snapshot testing pitfalls, Web Vitals measurement, and detecting memory leaks in React with DevTools.
Testing questions appear in every SDE-2 frontend interview. You must explain the testing pyramid, pick the right test type for the task, mock APIs with MSW, and know when snapshot tests help vs hurt — not just name tools. A mature team balances fast feedback with confidence that critical user paths work end-to-end.
Checking code and interfaces for correctness, stability, and compliance.
Frontend Testing is the process of checking code and interfaces for correctness, stability, and compliance with requirements. It identifies bugs before they reach users and ensures product quality.
Test individual units of code in isolation.
Unit tests verify individual functions, hooks, or components in isolation — the fastest and cheapest tests to write and run.
Tools: Jest, Vitest, React Testing Library
// Unit test for pure function import { add } from "./utils"; test("add(2, 3) returns 5", () => { expect(add(2, 3)).toBe(5); }); // Unit test for component import { render, screen } from "@testing-library/react"; import Button from "./Button"; test("Button renders with correct text", () => { render(<Button>Click</Button>); expect(screen.getByText("Click")).toBeInTheDocument(); });
Test interaction of several components or layers.
Integration tests verify how multiple components or layers work together — UI + business logic, form + API, context + state.
Tools: React Testing Library, MSW (Mock Service Worker)
// Integration test: Form + API import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import Form from "./Form"; import { mockServer } from "./mocks"; test("Form submits data to API", async () => { mockServer.post("/api/users", { name: "John" }); render(<Form />); await userEvent.type(screen.getByLabelText("Name"), "John"); await userEvent.click(screen.getByText("Submit")); await waitFor(() => { expect(screen.getByText("Success")).toBeInTheDocument(); }); });
Simulate real user behavior through the entire app.
E2E tests simulate real user workflows in a browser — navigation, clicks, form submissions, and server interaction. Highest confidence, slowest and most expensive to maintain.
Tools: Cypress, Playwright, Selenium
// E2E test: Login → Navigate → Logout import { test, expect } from "@playwright/test"; test("User can login and logout", async ({ page }) => { await page.goto("/login"); await page.fill('input[name="email"]', "user@example.com"); await page.fill('input[name="password"]', "password123"); await page.click('button[type="submit"]'); await expect(page).toHaveURL("/dashboard"); await page.click('button[data-testid="logout"]'); await expect(page).toHaveURL("/login"); });
Compare UI renders against a stored reference.
Snapshot tests serialize a component's rendered output and compare it against a stored reference. Useful for catching unintended layout changes — but can become brittle if overused.
Tools: Jest, Vitest
// Snapshot test import { render } from "@testing-library/react"; import Card from "./Card"; test("Card matches snapshot", () => { const { container } = render(<Card title="Hello" />); expect(container).toMatchSnapshot(); });
Measure load time, rendering speed, and user experience metrics.
Performance testing measures how fast your application loads and responds. Core Web Vitals are the standard metrics Google uses for ranking and user experience.
| Metric | What It Measures |
|---|---|
| FCP | First Contentful Paint — when first content appears |
| LCP | Largest Contentful Paint — when largest element appears |
| TTI | Time to Interactive — when app is ready for input |
| CLS | Cumulative Layout Shift — visual stability |
| INP | Interaction to Next Paint — responsiveness (replaces FID) |
Tools: Web Vitals, Lighthouse, React Profiler, Chrome DevTools
// Web Vitals in production import { onLCP, onFCP, onCLS } from "web-vitals"; onLCP((metric) => console.log("LCP:", metric.value)); onFCP((metric) => console.log("FCP:", metric.value)); onCLS((metric) => console.log("CLS:", metric.value));
Unit vs integration vs E2E vs snapshot at a glance.
| Aspect | Unit | Integration | E2E | Snapshot |
|---|---|---|---|---|
| Scope | Function/component | Multiple components | Entire flow | UI component |
| Speed | Fastest | Medium | Slowest | Fast |
| Cost | Cheapest | Medium | Most expensive | Cheap |
| Isolation | High | Medium | Low | High |
| Real user | No | No | Yes | No |
| Use case | Function logic | Component interaction | User workflows | UI stability |
70% unit, 20% integration, 10% E2E.
The testing pyramid recommends keeping most tests at the unit level, fewer at integration, and fewest at E2E. Fast feedback during development comes from unit tests; E2E validates critical paths only.
E2E (10%) Integration (20%) Unit Tests (70%)
Match the test type to the task.
| Task | Recommended Test |
|---|---|
| Checking function or hook logic | Unit |
| Checking component interaction | Integration |
| Checking appearance | Snapshot (wisely) |
| Checking user behavior | E2E |
| Measuring load and responsiveness | Performance |
What SDE-2 interviewers expect you to know.
Common interview questions about frontend testing.