Testing React Apps
React testing library vitest Roman Urdu mein, Vitest setup, render, screen queries aur user-event se React components ko user ki tarah test karna.
On this page
1. Testing React Apps kya hai?
Aaj kal ka common stack yeh hai: Vitest (test runner, Vite ke saath fast), React Testing Library (component render aur UI dhoondna), aur user-event (asli user jaisa click aur typing).
React Testing Library ka falsafa simple hai: component ke andar ki state ya functions test na karo, balke wohi test karo jo user dekhta aur karta hai.
2. Kyun zaroori hai?
Har change ke baad haath se har page khol kar check karna slow hai aur cheezein reh jati hain. Tests yeh kaam seconds mein, har baar, ek jaisa karte hain.
| Sirf manual testing | Automated tests ke saath |
|---|---|
| Har change par browser mein click click | npm test se sab check |
| Purane features chupke se toot jate hain | Regression foran pakra jata hai |
| Refactor karte darr lagta hai | Tests green hain to confidence |
| Bug production mein milta hai | Bug CI mein hi ruk jata hai |
3. Kahan use hota hai?
| Test type | Kya check karta hai | Tool |
|---|---|---|
| Unit | Ek function ya hook | Vitest |
| Component | Ek component ka render aur interaction | Vitest + Testing Library |
| Integration | Kai components saath, jaise form + list | Vitest + Testing Library |
| End to end | Poora app asli browser mein | Playwright |
4. Kaise kaam karta hai?
Vitest Node mein chalta hai. jsdom environment ek nakli browser DOM deta hai. Testing Library component ko us DOM mein render karti hai, aur aap screen se elements dhoondte hain.
Query priority yaad rakhein: pehle getByRole, phir getByLabelText, phir getByText. getByTestId sirf aakhri option.
5. Code example
Setup
npm install -D vitest jsdom @testing-library/react @testing-library/dom @testing-library/user-event @testing-library/jest-domimport { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
test: {
environment: "jsdom",
globals: true,
setupFiles: "./vitest.setup.ts",
},
});import "@testing-library/jest-dom/vitest";Pehli file vitest.config.ts hai aur doosri vitest.setup.ts, jo toBeInTheDocument jaise matchers add karti hai.
Example 1: Counter test
import { useState } from "react";
export function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import { Counter } from "./Counter";
describe("Counter", () => {
it("click par count barhta hai", async () => {
const user = userEvent.setup();
render(<Counter />);
const button = screen.getByRole("button", { name: /count: 0/i });
await user.click(button);
expect(screen.getByRole("button", { name: /count: 1/i })).toBeInTheDocument();
});
});Example 2: Async UI ka test
it("data aane par naam dikhata hai", async () => {
render(<UserCard userId={1} />);
expect(screen.getByText(/load ho raha hai/i)).toBeInTheDocument();
expect(await screen.findByText("Ali")).toBeInTheDocument();
});Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
environment: "jsdom" | Node mein browser jaisa DOM |
globals: true | Testing Library har test ke baad khud cleanup kar sake |
userEvent.setup() | Har test ke shuru mein user instance |
await user.click(...) | user-event ke actions async hote hain |
getByRole("button", ...) | Accessible role aur naam se dhoondna |
findByText | Element aane tak wait karta hai (async) |
toBeInTheDocument() | jest-dom ka matcher |
Interview tip
getBy, queryBy aur findBy ka farq zaroor poocha jata hai. getBy na mile to error deta hai, queryBy null deta hai (cheez ke na hone ka test), aur findBy Promise deta hai jo element aane tak wait karta hai.
6. Common mistakes
Galati: Implementation details test karna
Why? State ya internal function ke naam badalne par test toot jata hai, halanke user ke liye kuch nahi badla.
Behtar: Wohi test karo jo user dekhta hai: text, roles, labels aur unka behaviour.
Galati: user-event ke actions par await na lagana
Why? Action poora hone se pehle expect chal jata hai aur test random fail hota hai.
Behtar: userEvent.setup() lo aur har user.click ya user.type ke aage await lagao.
Galati: Async UI ke liye getBy use karna
Why? Data abhi aaya nahi hota, is liye getBy foran fail ho jata hai.
Behtar: Aane wale elements ke liye await findBy use karo.
Galati: Har cheez ke liye data-testid lagana
Why? Tests accessibility check nahi karte aur user ke nazariye se door ho jate hain.
Behtar: Pehle getByRole aur getByLabelText try karo, testid aakhri option rakho.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | Handling Events, Forms & Controlled Inputs, Async / Await |
| Saath mein | TypeScript with React, Custom Hooks, Data Fetching |
| Baad mein | Testing Next.js Apps |
Final quick revision
| Point | Detail |
|---|---|
| Runner | Vitest, environment: "jsdom" |
| Render | render() aur screen |
| Queries | getByRole pehle, findBy async ke liye |
| Interaction | userEvent.setup() aur await user.click() |
| Matchers | @testing-library/jest-dom/vitest |