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

React Apps की Testing का परिचय

React apps को test करना एक play से पहले की dress rehearsal जैसा है — आप check करते हैं कि stage पर सब कुछ सही से काम करता है एक real audience के देखने से पहले।

React Components क्यों Test करें

App बढ़ने के साथ, हर change के बाद manually हर feature पर click करना slow और error-prone हो जाता है। Automated tests आपको seconds में verify करने देते हैं कि एक component अब भी सही behave करता है, regressions (जो चीज़ें पहले काम करती थीं लेकिन टूट गईं) को real users तक पहुंचने से पहले पकड़ते हुए।

Note: अपने सबसे critical user flows (जैसे checkout या login) test करने से शुरू करें — real value पाने के लिए 100% coverage की ज़रूरत नहीं।
Warning: Tests Jest/Vitest के through एक Node.js environment में run होते हैं, actual browser में नहीं — वे इस CDN-only preview sandbox में run नहीं हो सकते।

उदाहरण: Why Test React Components

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

test('renders button text', () => {
  render(<Button>Click me</Button>);
  expect(screen.getByText('Click me')).toBeInTheDocument();
});

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

Behavior Test करना, Implementation नहीं

अच्छे React tests वह check करते हैं जो एक USER देखेगा या करेगा — visible text, clickable buttons, form submissions — internal details के बजाय जैसे एक component के specific state variable names।

यह tests को refactoring के against ज़्यादा resilient बनाता है, क्योंकि वे सिर्फ इसलिए नहीं टूटते कि आपने एक internal variable rename किया।

Note: अगर एक test को बदलना पड़े सिर्फ इसलिए कि आपने एक component के internals refactor किए (इसका behavior बदले बिना), यह एक sign है कि test बहुत implementation-focused है।
Warning: Internal state को directly test करना (rendered output के through करने के बजाय) आपके tests को implementation details से tightly couple करता है जो unrelated कारणों से बदल सकते हैं।

उदाहरण: Testing Behavior, Not Implementation

markup
// Run in your local React project (npm install required)
// Good: tests what the user sees
test('shows error message on invalid email', () => {
  render(<SignupForm />);
  fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'bad' } });
  fireEvent.click(screen.getByText('Submit'));
  expect(screen.getByText('Invalid email')).toBeInTheDocument();
});

Testing Pyramid: Unit, Integration, E2E

Tests generally तीन levels में fall होते हैं: unit tests isolation में एक small piece check करते हैं, integration tests कई pieces एक साथ काम करते हुए check करते हैं, और end-to-end (E2E) tests पूरे app के through एक full real user flow simulate करते हैं।

ज़्यादातर teams कई unit tests, कम integration tests, और सिर्फ कुछ E2E tests लिखती हैं।

Note: यह shape (कई unit, कुछ integration, कम E2E) speed (unit tests fast run होते हैं) को realism के against balance करता है (E2E tests ज़्यादा real-world issues पकड़ते हैं लेकिन slower run होते हैं)।
Warning: सिर्फ E2E tests पर भरोसा करना आपके test suite को slow और brittle बना देता है; सिर्फ unit tests पर भरोसा करना pieces के साथ interact करने के तरीके में bugs miss कर सकता है।

उदाहरण: The Testing Pyramid: Unit, Integration, E2E

markup
// Run in your local React project (npm install required)
// Unit: tests one function/component in isolation
test('formatPrice formats correctly', () => { expect(formatPrice(5)).toBe('$5.00'); });

// Integration: tests a form + validation working together
// (see the previous section's example)

// E2E: tests a full flow (often with Cypress/Playwright)
// cy.visit('/signup'); cy.get('#email').type('[email protected]'); cy.get('button').click();
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. Implementation details (internal state variable names) test करना instead of जो user actually देखता और करता है।
  2. सिर्फ happy path के लिए tests लिखना और error/edge cases कभी check न करना।
  3. Tests को regularly run न करना, broken tests को unnoticed pile up होने देते हुए।
चैप्टर सारांश
  • React apps को test करना typically एक test runner (जैसे Jest या Vitest) plus components render करने के लिए React Testing Library involve करता है।
  • अच्छे tests user-visible behavior पर focus करते हैं, internal implementation details पर नहीं।
  • Common test types में unit tests (एक function/component), integration tests (कई साथ काम करते हुए), और end-to-end tests (एक full user flow) शामिल हैं।
  • Tests regressions को early पकड़ते हैं, real users तक पहुंचने से पहले।
ब्राउज़र सपोर्ट

npm install (Jest/Vitest + @testing-library/react) चाहिए — tests Node में 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.