Loading...
Loading...
Testing is process of checking code and interfaces for correctness, stability and compliance with requirements. It allows identifying bugs before they reach user and ensures quality of product.
Tests in frontend help:
Unit tests check individual units of code in isolation — functions, hooks or components. These are fastest and cheapest tests to execute.
Used for:
🛠 Tools: Jest, Vitest, Testing Library
test("add() should return sum", () => {
expect(add(2, 3)).toBe(5);
});
Snapshots are UI component snapshots in serialized form. On first run "reference" is saved, then compared with result of subsequent renders.
Help:
⚠️ Shouldn't be used for all components — snapshots can easily "get dirty".
import { render } from "@testing-library/react";
import Button from "./Button";
test("button matches snapshot", () => {
const { asFragment } = render(<Button label="Click" />);
expect(asFragment()).toMatchSnapshot();
});
Integration tests check interaction of several components or layers of application (e.g., UI + business logic).
Checks:
🛠 Tools: React Testing Library, MSW (for mocks)
// Example: entering text in field and submitting form
fireEvent.change(screen.getByLabelText("Name"), {
target: { value: "Ivan" }
});
fireEvent.click(screen.getByText("Submit"));
expect(mockSubmit).toHaveBeenCalledWith("Ivan");
E2E (end to end) simulates real user behavior, going through entire UI and backend. This is highest level of testing.
Checks:
Tools: Cypress, Playwright
// Cypress
cy.visit("/login");
cy.get("input[name=email]").type("user@example.com");
cy.get("input[name=password]").type("123456");
cy.get("button[type=submit]").click();
cy.contains("Welcome!");
| Task | Recommended Test |
|---|---|
| Checking function or hook logic | Unit |
| Checking component interaction | Integration |
| Checking appearance | Snapshot (wisely) |
| Checking user behavior | E2E |