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

React के लिए Jest की Basics

Jest आपके testing game का referee जैसा है — यह आपके सभी tests run करता है, results को आपकी उम्मीद के against check करता है, और report करता है कौन जीता या हारा।
Syntax
markup
describe("group name", () => {
  test("description", () => {
    expect(actual).toBe(expected);
    expect(objectValue).toEqual({ key: "value" });
  });
});

test() और expect() से एक Basic Test लिखना

एक Jest test test() (या इसका alias it()) का एक call है एक description string और test logic वाले एक function के साथ। अंदर, expect(value) एक assertion शुरू करता है, .toBe() जैसे एक matcher के साथ chained actual result को आपकी उम्मीद के against check करने के लिए।

Note: Test descriptions को expected behavior describe करने वाले एक sentence के रूप में लिखें, जैसे 'adds two numbers correctly', ताकि failures clearly पढ़ें।
Warning: एक test function जो कोई error throw नहीं करता (कोई assertions न होने सहित) Jest द्वारा PASSING माना जाता है — हमेशा कम से कम एक expect() शामिल करें।

उदाहरण: Writing a Basic Test with test() and expect()

markup
// Run in your local React project (npm install required)
function add(a, b) { return a + b; }

test('adds two numbers correctly', () => {
  expect(add(2, 3)).toBe(5);
});

toBe vs. toEqual

toBe strict equality (===) check करता है, जो primitives के लिए काम करता है लेकिन objects/arrays के लिए fail होता है identical contents के साथ भी, क्योंकि दो separately-created objects कभी === equal नहीं होते। toEqual इसके बजाय एक deep comparison perform करता है, check करते हुए कि actual VALUES match करती हैं, object identity चाहे कुछ भी हो।

Note: एक rule of thumb के रूप में: primitives (numbers, strings, booleans) के लिए toBe उपयोग करें, objects और arrays के लिए toEqual।
Warning: दो objects को identical contents के साथ compare करने के लिए toBe उपयोग करना fail होगा, भले ही वे 'एक जैसे दिखते' हों — यह सबसे common Jest beginner mistakes में से एक है।

उदाहरण: toBe vs. toEqual

markup
// Run in your local React project (npm install required)
test('primitive comparison uses toBe', () => {
  expect(2 + 2).toBe(4);
});

test('object comparison uses toEqual', () => {
  expect({ name: 'Asha' }).toEqual({ name: 'Asha' }); // passes
  // expect({ name: 'Asha' }).toBe({ name: 'Asha' }); // would fail!
});

describe() से Tests को Group करना

describe() related test() calls को एक shared label के under group करता है, एक test file का output organized और scan करना आसान बनाते हुए, especially tests की संख्या बढ़ने के साथ। describe blocks further organization के लिए nested भी हो सकते हैं।

Note: describe blocks को उस component या function के नाम पर नाम दें जो test हो रहा है, जैसे describe('Button component', () => { ... })।
Warning: describe() blocks default रूप से उनके अंदर के tests के बीच state isolate नहीं करते — इसके लिए beforeEach उपयोग करें, सिर्फ grouping नहीं।

उदाहरण: Grouping Tests with describe()

markup
// Run in your local React project (npm install required)
describe('add function', () => {
  test('adds positive numbers', () => {
    expect(add(2, 3)).toBe(5);
  });
  test('adds negative numbers', () => {
    expect(add(-1, -1)).toBe(-2);
  });
});
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. toBe (strict equality, ===) को toEqual (objects/arrays के लिए deep equality) के साथ confuse करना।
  2. यह भूल जाना कि test files को automatically pick up होने के लिए Jest के expected naming pattern (जैसे *.test.js) से match करना चाहिए।
  3. Related tests को group करने के लिए describe() blocks उपयोग न करना, large test files को navigate करना मुश्किल बनाते हुए।
चैप्टर सारांश
  • Jest एक JavaScript test runner है, commonly component tests के लिए React Testing Library के साथ paired।
  • test() (या it()) एक individual test case define करता है; describe() related tests को group करता है।
  • expect(value).matcher() assertions बनाता है, जैसे toBe, toEqual, या toBeInTheDocument (Testing Library के extensions से)।
  • Jest common naming patterns से match करने वाली test files auto-discover करता है, जैसे Component.test.js।
ब्राउज़र सपोर्ट

npm install jest चाहिए (या Create React App/Vite templates के साथ pre-configured आता है) — 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.