easyFull Stack EngineerTechnology
What is the difference between unit, integration, and end-to-end testing? When would you write each?
Posted 18/04/2026
by Mehedy Hasan Ador
Question Details
At a quality-focused company:
> "Our team writes only E2E tests because 'they test everything.' But our CI takes 45 minutes and tests are flaky. How should we structure our test pyramid?"
> "Our team writes only E2E tests because 'they test everything.' But our CI takes 45 minutes and tests are flaky. How should we structure our test pyramid?"
Suggested Solution
The Testing Pyramid
/ E2E \ ← Few, slow, expensive
/ Integration \ ← Some, moderate speed
/ Unit Tests \ ← Many, fast, cheap
Types Compared
When to Write Each
Unit Tests — Business logic
// Test pure functions, calculations, formatters
describe("formatSalary", () => {
it("formats yearly salary", () => {
expect(formatSalary(120000, "yearly")).toBe("$120,000/year");
});
it("formats monthly salary", () => {
expect(formatSalary(5000, "monthly")).toBe("$5,000/month");
});
});
// 100 tests run in <1 second
Integration Tests — API + Database
// Test API routes with real database
describe("POST /api/applications", () => {
it("creates an application", async () => {
const res = await fetch("/api/applications", {
method: "POST",
body: JSON.stringify({ company: "Google", position: "SWE" }),
headers: { Authorization: Bearer ${testToken} },
});
expect(res.status).toBe(201);
const data = await res.json();
expect(data.company).toBe("Google");
});
});
// Uses real database (test instance)
E2E Tests — Critical User Flows
// Playwright test
test("user can create and view application", async ({ page }) => {
await page.goto("/login");
await page.fill('[name="email"]', "test@example.com");
await page.fill('[name="password"]', "password123");
await page.click('button[type="submit"]');
await page.goto("/dashboard/applications");
await page.click("text=Add Application");
await page.fill('[name="company"]', "Google");
await page.fill('[name="position"]', "SWE");
await page.click("text=Save");
await expect(page.locator("text=Google")).toBeVisible();
});