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

Test Utilities की Typing

Test utilities कई tests द्वारा उपयोग होने वाले reusable functions, fixtures, और builders हैं। इन helpers को precise types देना test code को reliable रखता है और failures को diagnose करना आसान बनाता है।
Syntax
typescript
function createFixture(overrides?: Partial<TypeName>): TypeName {
    return { defaultProperty: defaultValue, ...overrides };
}

Typed Fixture Factories

एक typed fixture factory एक function है जो एक पूरी तरह typed test object return करता है (जैसे createUser(overrides?: Partial<User>): User), इसलिए हर test बिना हर बार पूरी shape repeat किए एक valid, type-checked object पाता है।

उदाहरण: Typed Fixture Factories

typescript
interface User {
  id: number;
  name: string;
}
function createUser(overrides?: Partial<User>): User {
  return { id: 1, name: "Test User", ...overrides };
}
console.log(createUser({ name: "Ravi" }));

Typed Assertion Helpers

Typed assertion helpers एक custom check को asserts return type वाले एक function में wrap करते हैं, TypeScript को assertion के बाद आने वाली test की हर line में value का type narrow करने देते हुए।

उदाहरण: Typed Assertion Helpers

typescript
function assertDefined<T>(value: T | undefined, msg: string): asserts value is T {
  if (value === undefined) throw new Error(msg);
}
const maybe: number | undefined = 5;
assertDefined(maybe, "must be defined");
console.log(maybe + 1);

Typed Async Helpers

Typed async helpers — जैसे एक waitFor<T>(fn: () => T | Promise<T>): Promise<T> utility — wait के through resolved type preserve करते हैं, इसलिए awaited result एक manual type assertion के बिना usable है।

उदाहरण: Typed Async Helpers

typescript
async function waitFor<T>(fn: () => T | Promise<T>): Promise<T> {
  return fn();
}
waitFor(() => 42).then(console.log);

Typed Test Builders

एक typed test builder एक fluent, chainable API उपयोग करता है (.withName(Alice).withAge(30).build()) जहां हर method return type narrow करता है, compile time पर एक missing required field catch करते हुए।

उदाहरण: Typed Test Builders

typescript
class UserTestBuilder {
  private name = "";
  private age = 0;
  withName(name: string): this { this.name = name; return this; }
  withAge(age: number): this { this.age = age; return this; }
  build() { return { name: this.name, age: this.age }; }
}
console.log(new UserTestBuilder().withName("Ravi").withAge(30).build());

Typed Mock Factories

एक typed mock factory एक interface से match करने वाला object return करता है लेकिन हर method एक jest mock function से replaced होती है, mock की shape को उस असली interface के साथ sync में रखते हुए जिसके लिए यह standing in है।

उदाहरण: Typed Mock Factories

typescript
interface UserService {
  getUser(id: number): string;
}
function createMockUserService(): UserService {
  return { getUser: (id: number) => `mock-user-${id}` };
}
console.log(createMockUserService().getUser(1));
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 में एक factory के बजाय hand-written objects से fixtures बनाना, इसलिए type में changes कई tests तोड़ते हैं।
  2. Overrides को Partial<User> के बजाय User के रूप में type करना, हर field को देना मजबूर करते हुए।
  3. asserts return type के बिना एक assertion helper लिखना, इसलिए इसे call करने के बाद TypeScript narrow नहीं करता।
🔒

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.