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

TypeScript के साथ Jest

Jest एक JavaScript testing framework है जिसे एक TypeScript-aware transformer या compiler setup के through TypeScript के साथ उपयोग किया जा सकता है। Tests को behavior verify करना चाहिए जबकि TypeScript types test code को खुद safer बनाने में help करती हैं।
Syntax
typescript
import { functionName } from "./module";

describe("name", () => {
  it("does something", () => {
    expect(functionName(input)).toBe(expected);
  });
});

एक Simple Jest Test

एक TypeScript project में एक basic Jest test plain JavaScript जैसी ही describe/it/expect API उपयोग करता है, लेकिन ts-jest (या Babel का TypeScript preset) Jest के चलाने से पहले .ts test file को compile करता है, आपके tests में खुद type errors catch करते हुए।

उदाहरण: A Simple Jest Test

typescript
// sum.test.ts
function sum(a: number, b: number) { return a + b; }
// test("adds numbers", () => { expect(sum(1, 2)).toBe(3); });
console.log(sum(1, 2));

Typed Functions Test करना

एक typed function test करना Jest के type checker को यह flag करने देता है कि एक test function को गलत argument types से call करता है test असल में चलने से पहले, bugs की एक class catch करते हुए जो plain JavaScript tests नहीं कर सकते।

उदाहरण: Testing Typed Functions

typescript
function double(x: number): number { return x * 2; }
// test("doubles a number", () => { expect(double(5)).toBe(10); });
// double("5") would be a compile-time error, caught before the test runs
console.log(double(5));

Async Functions Test करना

Async functions test करने के लिए test body के अंदर call को await करना चाहिए (या promise return करना), और TypeScript resolved type infer करेगा इसलिए expect() assertions असली return shape के against type-checked होते हैं।

उदाहरण: Testing Async Functions

typescript
async function fetchValue(): Promise<number> { return 42; }
// test("resolves with 42", async () => { expect(await fetchValue()).toBe(42); });
fetchValue().then(console.log);

Errors Test करना

यह test करना कि एक function throw करता है expect(() => fn()).toThrow() उपयोग करता है, call को एक arrow function में wrap करते हुए ताकि Jest खुद thrown error catch कर सके बजाय test के crash होने के assertion चलने से पहले।

उदाहरण: Testing Errors

typescript
function mustBePositive(n: number): number {
  if (n < 0) throw new Error("must be positive");
  return n;
}
// test("throws on negative", () => { expect(() => mustBePositive(-1)).toThrow(); });
try { mustBePositive(-1); } catch (e) { console.log("threw as expected"); }

Typed Test Data

Typed test data — fixture objects बनाना जो एक interface को satisfy करें — compile time पर एक broken fixture catch करता है अगर जिस shape के against यह test कर रहा है वह कभी बदले, runtime पर confusingly fail होने के बजाय।

उदाहरण: Typed Test Data

typescript
interface User { id: number; name: string; }
const fixture: User = { id: 1, name: "Test User" };
// test("fixture matches User shape", () => { expect(fixture.name).toBe("Test User"); });
console.log(fixture);
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. @types/jest भूल जाना, इसलिए describe, it, और expect unknown names हैं।
  2. Objects को toBe से compare करना, जो identity check करता है, जब equal contents के लिए toEqual चाहिए।
  3. एक function जो throw करे उसे expect(() => fn()) के बजाय सीधे expect(fn()) के अंदर call करना, इसलिए error assertion से escape हो जाती है।
🔒

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.