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

Snapshot Testing क्या है

Snapshot testing ऐसा है जैसे आज अपने room की एक photo लेना और इसे yesterday की एक photo से compare करना, ताकि आप instantly notice कर सकें अगर कुछ भी बिना आपके मतलब के move हुआ हो।
Syntax
markup
test("matches snapshot", () => {
  const { container } = render(<Component />);
  expect(container).toMatchSnapshot();
});

एक Snapshot Test क्या करता है

toMatchSnapshot() एक component render करता है, इसके output को text में serialize करता है, और इसे test पहली बार run होने पर एक snapshot file में save करता है।

हर subsequent run पर, Jest नए output को saved snapshot के against compare करता है और अगर कुछ भी differ करे तो test fail कर देता है।

Note: Snapshot tests UNINTENDED changes पकड़ने के लिए सबसे अच्छे हैं — वे यह नहीं बताते कि क्या correct है, सिर्फ कि कुछ बदला या नहीं।
Warning: First run snapshot बनाकर हमेशा pass होता है — initial snapshot file को carefully review करें यह confirm करने के लिए कि यह actually correct है।

उदाहरण: What a Snapshot Test Does

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

test('Card renders correctly', () => {
  const { container } = render(<Card title="Hello" />);
  expect(container).toMatchSnapshot();
});

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

एक Snapshot Diff Review करना

जब एक snapshot test fail होता है, Jest saved snapshot और नए output के बीच एक diff दिखाता है। आपको यह diff पढ़ना होगा और decide करना होगा: यह change intentional था (जिस case में आप snapshot update करें) या एक real bug था (जिस case में आप component fix करें instead)?

Note: एक failing snapshot को वैसे ही treat करें जैसे आप एक failing code review comment को treat करेंगे — accept करने से पहले investigate करें।
Warning: जब भी एक test fail हो blindly 'update snapshots' command run करना पूरे purpose को defeat कर देता है — यह silently real bugs को नए expected output के रूप में accept कर लेगा।

उदाहरण: Reading and Updating a Snapshot Diff

bash
$ npx jest
 FAIL  src/Greeting.test.js
  ✕ renders a greeting (12 ms)

  - Snapshot
  + Received

  - <h1>Hello, Guest</h1>
  + <h1>Hello, World</h1>

# after confirming the change is intentional:
$ npx jest -u
 PASS  src/Greeting.test.js
 1 snapshot updated.

⚠️ Run this in your own terminal or Node.js environment.

Snapshots कब उपयोग करें (और कब न करें)

Snapshots छोटे, stable components के लिए सबसे अच्छा काम करते हैं जहां कोई भी change meaningful और review करना आसान हो। Large, frequently-changing components के लिए, snapshots huge diffs produce करते हैं जो carefully review करना मुश्किल होते हैं, उन्हें specific behavior के बारे में targeted assertions से कम useful बनाते हुए।

Note: आप जिस behavior की चिंता करते हैं उसके लिए specific assertions (जैसे screen.getByText(...)) preferred करें, और snapshots को छोटे, ज़्यादा stable presentational components के लिए reserve करें।
Warning: एक complex page का एक giant snapshot एक tiny, correct change को भी एक overwhelming diff produce करा देता है जिसे really पढ़े बिना approve करने का temptation होता है।

उदाहरण: When to Use (and Avoid) Snapshots

markup
// Run in your local React project (npm install required)
// Good snapshot candidate: small, stable
test('Icon renders', () => {
  const { container } = render(<Icon name="star" />);
  expect(container).toMatchSnapshot();
});
// Less ideal: a whole complex Dashboard page — prefer targeted assertions there instead
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. जब भी test fail हो blindly 'update snapshots' run करना, बिना actually check किए कि change intentional था या नहीं।
  2. Huge, complex component trees को snapshot करना जहां एक tiny unrelated change एक massive, unreadable diff produce करता है।
  3. एक stale snapshot file commit करना जो अब intentional recent changes से match नहीं करती।
चैप्टर सारांश
  • Snapshot tests rendered output का एक serialized representation save करते हैं और future runs को इसके against compare करते हैं।
  • toMatchSnapshot() first run पर snapshot file बनाता है, फिर subsequent runs पर इसके against compare करता है।
  • एक failing snapshot test का मतलब है output बदल गया — आपको confirm करना होगा कि वह change intentional था या नहीं।
  • Snapshot files को code changes की तरह review किया जाना चाहिए, blindly regenerate नहीं।
ब्राउज़र सपोर्ट

npm install jest चाहिए (built-in snapshot support के साथ) — 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.