Skip to content
ProductionChapter 33 / 34Advanced5 min read

Testing Next.js Apps

Next.js testing playwright vitest ke saath, unit tests Vitest aur React Testing Library se, end-to-end tests Playwright se, Roman Urdu mein setup.

On this page

1. Testing Next.js Apps kya hai?

Test typeKya check hota haiTool
UnitEk function ya component akelaVitest + React Testing Library
End-to-end (E2E)User ka poora safar, routing, server, databasePlaywright

2. Kyun zaroori hai?

Har deploy se pehle haath se login, checkout aur forms check karna thaka dene wala aur unreliable hai. Tests yeh kaam seconds mein har push par karte hain.

Sirf manual testingAutomated tests
Har release par haath se clickCI mein har push par khud chalte hain
Purane features toot jate hain pata nahi chaltaRegression foran pakri jati hai
Refactor karne se darTests pass to refactor safe
Bug production mein milta haiBug pull request mein hi milta hai

3. Kahan use hota hai?

CheezBehtar test
formatPrice() jaisa utility functionVitest unit test
Button, Card jaisa sync componentVitest + Testing Library
async Server Component jo data fetch karePlaywright E2E
Login, checkout, form submit flowPlaywright E2E
Server Action validation logicLogic ko alag function bana kar Vitest

4. Kaise kaam karta hai?

Testing pyramid
Bohot saare unit tests (tez, sasta)
Kuch integration tests
Thode E2E tests (slow, lekin asli browser)
CI har push par sab chalata hai

Vitest jsdom mein components render karta hai, browser ke bina. Playwright pehle next build aur next start se app chalata hai, phir Chromium, Firefox ya WebKit mein asli clicks karta hai.

5. Code example

Vitest setup

bash
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths
ts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  plugins: [tsconfigPaths(), react()],
  test: {
    environment: "jsdom",
  },
});

Upar wali file vitest.config.mts hai. Ab ek unit test:

tsx
import { expect, test } from "vitest";
import { render, screen } from "@testing-library/react";
import Page from "../app/page";

test("home page heading dikhata hai", () => {
  render(<Page />);
  expect(screen.getByRole("heading", { level: 1, name: "Home" })).toBeDefined();
});

Playwright setup

bash
npm init playwright@latest
ts
import { defineConfig } from "@playwright/test";

export default defineConfig({
  testDir: "./e2e",
  use: { baseURL: "http://localhost:3000" },
  webServer: {
    command: "npm run build && npm run start",
    url: "http://localhost:3000",
    reuseExistingServer: !process.env.CI,
  },
});
ts
import { test, expect } from "@playwright/test";

test("user about page par ja sakta hai", async ({ page }) => {
  await page.goto("/");
  await page.getByRole("link", { name: "About" }).click();
  await expect(page).toHaveURL("/about");
  await expect(page.getByRole("heading", { level: 1 })).toContainText("About");
});

Code kaise kaam kar raha hai?

CheezMatlab
environment: "jsdom"Node mein nakli DOM, taake components render hon
tsconfigPaths()@/components jaise path aliases tests mein bhi chalein
render aur screenComponent render kar ke user ki tarah elements dhoondo
webServerPlaywright tests se pehle production server khud chalata hai
getByRoleAccessible role se element, CSS selector se zyada mazboot
toHaveURLNavigation ke baad URL check
json
{
  "scripts": {
    "test": "vitest",
    "test:e2e": "playwright test"
  }
}

6. Common mistakes

Galati: async Server Component ko Vitest mein test karna

Why? Vitest abhi async Server Components ko render support nahi karta, test fail hota hai ya galat result deta hai.

Behtar: Async components ke liye Playwright E2E test likho, aur un ki logic alag function mein nikal kar unit test karo.

Galati: E2E tests next dev par chalana

Why? Dev mode slow hai aur production se alag behave karta hai, is liye tests flaky aur gumrah karne wale hote hain.

Behtar: Playwright webServer mein build aur start command do, taake asli production build test ho.

Galati: CSS class ya DOM structure se elements pakarna

Why? Design badalte hi selectors toot jate hain halanke feature theek chal raha hota hai.

Behtar: getByRole, getByLabel aur getByText use karo, jaise user screen dekhta hai.

Galati: Sirf E2E tests likhna

Why? E2E slow hain, CI lamba ho jata hai aur failure ki asal wajah dhoondna mushkil hota hai.

Behtar: Zyada unit tests, aur sirf important flows jaise login aur checkout ke liye E2E.

Interview tip

"Unit aur E2E test mein kab kya?" Pure logic aur chhote components ke liye unit test, kyunke tez aur saste hain. Jahan routing, server, database aur browser sab saath kaam karte hon, wahan E2E.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Unit testsVitest + React Testing Library, jsdom
E2E testsPlaywright, asli browser
Async Server ComponentsE2E se test karo
Playwright serverwebServer mein build aur start
SelectorsgetByRole, getByLabel