← Back to TypeScript Course | Chapter 24: Testing TypeScript | Lesson 6 of 6

Integration Testing की जानकारी

Integration tests यह verify करते हैं कि एक application के कई parts साथ काम करते हैं। वे usually services, data access, HTTP boundaries, या दूसरे connected components के across realistic flows test करते हैं बजाय एक function को isolation में test करने के।

Integration Tests क्या Verify करते हैं

Integration tests verify करते हैं कि कई typed units — एक service, एक data layer, एक API client — साथ सही तरीके से काम करते हैं, उनकी boundaries पर mismatches catch करते हुए जो isolated unit tests (जो उन boundaries को mock away करते हैं) नहीं देख सकते।

उदाहरण: What Integration Tests Verify

typescript
interface Repo { save(name: string): void; }
class Service {
  constructor(private repo: Repo) {}
  register(name: string) { this.repo.save(name); }
}
const repo: Repo = { save: (name) => console.log("Saved:", name) };
new Service(repo).register("Ravi");

API Integration Test करना

API integration test करने का मतलब है एक test server के against एक असली या realistically-typed HTTP client call करना, verify करते हुए कि response असल में आपके code के expected TypeScript interface से match करता है।

उदाहरण: Testing API Integration

typescript
interface User { name: string; }
async function getUser(): Promise<User> {
  const response = await fetch("data.php");
  return response.json();
}
getUser().then((u) => console.log(u.name));

Database Integration

Database integration tests एक असली (अक्सर in-memory या containerized) database के against चलते हैं, confirm करते हुए कि आपके typed query results असल में आपका ORM या query layer declare करता है उन shapes से match करते हैं।

उदाहरण: Database Integration

typescript
interface Row { id: number; name: string; }
class InMemoryDb {
  private rows: Row[] = [];
  insert(row: Row) { this.rows.push(row); }
  find(id: number) { return this.rows.find((r) => r.id === id); }
}
const db = new InMemoryDb();
db.insert({ id: 1, name: "Ravi" });
console.log(db.find(1));

Complete Workflows Test करना

एक complete workflow test करना — जैसे "user create करें, फिर log in करें, फिर profile fetch करें" — कई typed modules को sequence में exercise करता है, integration bugs catch करते हुए जो सिर्फ तब दिखते हैं जब असली data उनके बीच flow करे।

उदाहरण: Testing Complete Workflows

typescript
interface User { id: number; name: string; }
class UserFlow {
  private users: User[] = [];
  create(name: string): User {
    const user = { id: this.users.length + 1, name };
    this.users.push(user);
    return user;
  }
  login(id: number) { return this.users.find((u) => u.id === id); }
}
const flow = new UserFlow();
const created = flow.create("Ravi");
console.log(flow.login(created.id));

Integration Test Isolation

Integration tests को एक-दूसरे से isolate करना (fresh database state, कोई shared mutable fixtures नहीं) एक test के typed side effects को इसके बाद चलने वाले किसी unrelated test में leak होकर उसे तोड़ने से रोकता है।

उदाहरण: Integration Test Isolation

typescript
class TestDatabase {
  private rows: string[] = [];
  reset() { this.rows = []; }
  insert(row: string) { this.rows.push(row); }
}
const db = new TestDatabase();
db.insert("test-data");
db.reset();
console.log(db);
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. हर dependency को mock करना इसलिए test अब यह check नहीं करता कि parts साथ काम करते हैं या नहीं।
  2. Tests के बीच database state share करना, इसलिए एक test का data दूसरे को तोड़ता है।
  3. एक live external API के against test करना, tests को flaky और slow बनाते हुए।
चैप्टर सारांश
  • ts-jest के साथ Jest आपको TypeScript में tests लिखने और चलाने देता है।
  • Typed test utilities और typed mocks test code को compiler से checked रखते हैं।
  • वही approach React components test करने और integration tests लिखने तक extend होता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.