← Back to React Course | Chapter 12: Testing React Applications | Lesson 2 of 9

React Testing Library Setup करना

React Testing Library ऐसी है जैसे एक robot एक real user होने का pretend करता है — यह buttons click करता है और screen पर text पढ़ता है exactly जैसे एक person करेगा, आपके code के internals को poke करने के बजाय।
Syntax
markup
import { render, screen } from "@testing-library/react";

test("description", () => {
  render(<Component />);
  expect(screen.getByText("text")).toBeInTheDocument();
});

Testing के लिए एक Component Render करना

React Testing Library का render() function एक component को specifically test के लिए एक virtual, jsdom-based DOM में mount करता है, similar कि कैसे ReactDOM.createRoot().render() इसे एक real browser में mount करता है।

Render होने के बाद, आप इसे query और interact कर सकते हैं जैसे यह एक real page पर हो।

Note: हर test typically render() को fresh शुरुआत में call करता है, हर test को एक clean, isolated component instance देते हुए।
Warning: render() jsdom उपयोग करता है (Node में एक simulated browser environment), एक real browser नहीं — कुछ browser-specific behaviors perfectly replicate नहीं हो सकते।

उदाहरण: Rendering a Component for Testing

markup
// Run in your local React project (npm install required)
import { render, screen } from '@testing-library/react';
import Greeting from './Greeting';

test('renders greeting', () => {
  render(<Greeting name="Asha" />);
  expect(screen.getByText('Hello, Asha')).toBeInTheDocument();
});

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

screen Object से Query करना

Render होने के बाद, screen object getByText, getByRole, और getByLabelText जैसे methods provide करता है rendered output में elements ढूंढने के लिए, mirroring करते हुए कि एक user कैसे चीज़ें identify करेगा — visible text या accessible role से, internal implementation details से नहीं।

Note: getByRole (जैसे getByRole(button, { name: Submit })) Testing Library का सबसे recommended query है — यह real accessibility semantics reflect करता है।
Warning: getBy* queries एक error throw करती हैं अगर कोई match न मिले (या एक से ज़्यादा match हो) — queryBy* उपयोग करें जब आपको उम्मीद हो कि कुछ legitimately मौजूद न हो।

उदाहरण: Querying with the screen Object

markup
// Run in your local React project (npm install required)
import { render, screen } from '@testing-library/react';
import LoginButton from './LoginButton';

test('finds the login button by role', () => {
  render(<LoginButton />);
  const button = screen.getByRole('button', { name: 'Log In' });
  expect(button).toBeInTheDocument();
});

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

RTL की Philosophy क्यों मायने रखती है

Testing Library deliberately implementation details test करने को discourage करने के लिए design की गई है (जैसे एक component की internal state या specific class names), और इसके बजाय test करने को encourage करती है जो एक real user actually experience करता है — visible text, accessible roles, और interactive behavior।

यह tests को internal refactors के against ज़्यादा robust बनाता है।

Note: अगर आप एक CSS-selector-based query के लिए reach करने का सोच रहे हैं, पहले check करें कि क्या एक role, label, या text-based query काम करेगी — यह usually करती है और ज़्यादा resilient है।
Warning: Elements ढूंढने के primary तरीके के रूप में data-testid attributes को overuse करना accessibility benefits skip करता है जो role/label-based queries naturally encourage करती हैं।

उदाहरण: Why RTL's Philosophy Matters

markup
// Run in your local React project (npm install required)
// Prefer this (resilient to internal refactors):
screen.getByRole('button', { name: 'Submit' });

// Over this (brittle, tied to implementation):
// container.querySelector('.btn-submit-primary-v2');
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Testing Library की user-facing queries के बजाय enzyme-style queries (component internals ढूंढते हुए) उपयोग करना।
  2. User events के बाद होने वाले state updates को await में wrap न करना, जिससे flaky tests होते हैं।
  3. पहली choice के रूप में CSS class या test ID से query करना, जब एक ज़्यादा accessible query (role, label text) usually मौजूद होती है और preferred है।
चैप्टर सारांश
  • React Testing Library (RTL) testing के लिए real components को एक virtual DOM में render करती है, फिर उन्हें query करती है जैसे एक user करेगा।
  • render() एक component mount करता है; screen elements ढूंढने के लिए query methods प्रदान करता है।
  • RTL की philosophy: 'जितना ज़्यादा आपके tests आपके software के उपयोग होने के तरीके जैसे दिखते हैं, उतना ज़्यादा confidence वे दे सकते हैं'।
  • यह commonly underlying test runner के रूप में Jest या Vitest के साथ pair होती है।
ब्राउज़र सपोर्ट

npm install @testing-library/react चाहिए — एक Node test environment में run होता है, इस browser sandbox में नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.