Integration Testing की जानकारी
In this page:
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
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
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
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
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
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);
- हर dependency को mock करना इसलिए test अब यह check नहीं करता कि parts साथ काम करते हैं या नहीं।
- Tests के बीच database state share करना, इसलिए एक test का data दूसरे को तोड़ता है।
- एक 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: