ts-jest का Setup
In this page:
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
// 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
// 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
// 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
// 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
// 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");
jestinstall करना लेकिनts-jestनहीं, इसलिए Jest TypeScript syntax parse नहीं कर सकता और एक syntax error से fail होता है।- Jest config में
preset: "ts-jest"याtransformentry set करना भूल जाना। tsconfigकेtypesमें@types/jestछोड़ देना, इसलिए type errors test files में दिखते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: