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

ts-jest का Setup

ts-jest एक Jest transformer है जो Jest के लिए TypeScript test files compile कर सकता है। एक typical setup में Jest, TypeScript, ts-jest, और एक Jest configuration शामिल है जो Jest को बताती है कि TypeScript files कैसे transform करनी हैं।
Syntax
typescript
npm install --save-dev jest ts-jest @types/jest
npx ts-jest config:init

Required Packages Install करें

ts-jest setup करने के लिए dev dependencies के रूप में jest, ts-jest, और @types/jest install करना चाहिए, क्योंकि Jest खुद एक transform के बिना TypeScript syntax नहीं समझता।

उदाहरण: Install Required Packages

typescript
// npm install --save-dev jest ts-jest @types/jest
console.log("jest, ts-jest, and @types/jest are needed as dev dependencies");

Jest Configuration

Jest की configuration को एक transform entry चाहिए जो .ts/.tsx files को ts-jest पर point करे, जो Jest को बताता है कि हर test file को execute करने से पहले TypeScript compiler के through compile करे।

उदाहरण: Jest Configuration

typescript
// jest.config.js
// module.exports = { transform: { "^.+\\.tsx?$": "ts-jest" } };
console.log("transform maps .ts/.tsx files through ts-jest before running");

TypeScript Compiler Options

Tests के लिए उपयोग किए TypeScript compiler options आपके app के tsconfig.json से slightly अलग हो सकते हैं — उदाहरण के लिए faster test compilation के लिए isolatedModules relax करना — inline या एक dedicated test tsconfig में configured।

उदाहरण: TypeScript Compiler Options

typescript
// jest.config.js
// globals: { "ts-jest": { isolatedModules: true } }
console.log("Test compiler options can relax settings for faster runs");

Test File Structure

Test files conventionally उस code के बराबर रहती हैं जो वे test करती हैं (Component.test.ts जो Component.ts के बराबर हो) या एक mirrored __tests__ directory में, दोनों को Jest की default file-matching अपने आप pick up करती है।

उदाहरण: Test File Structure

typescript
// Component.ts
// Component.test.ts   (or __tests__/Component.test.ts)
console.log("Jest auto-discovers *.test.ts files or __tests__ folders");

Tests और Type Checks चलाना

jest चलाना आपके tests execute करता है, जबकि एक separate tsc --noEmit command उन type errors को catch करता है जो ts-jest का per-file transform miss कर सकता है, क्योंकि ts-jest default रूप से full-project type checking नहीं करता।

उदाहरण: Running Tests and Type Checks

typescript
// npm test          (runs jest)
// npx tsc --noEmit  (full-project type check ts-jest's transform may miss)
console.log("Run both jest and tsc --noEmit for full coverage");
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. jest install करना लेकिन ts-jest नहीं, इसलिए Jest TypeScript syntax parse नहीं कर सकता और एक syntax error से fail होता है।
  2. Jest config में preset: "ts-jest" या transform entry set करना भूल जाना।
  3. tsconfig के types में @types/jest छोड़ देना, इसलिए type errors test files में दिखते हैं।
🔒

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.