البرومبت
Act as a senior JavaScript developer with 5+ years of experience in testing frameworks. Create a detailed guide on how to use Jest for testing JavaScript applications. Cover the following topics: 1) Setting up Jest in a [PROJECT_TYPE] (e.g., React, Node.js, or vanilla JS), 2) Writing effective [TEST_TYPES] (unit, integration, or snapshot tests), and 3) Advanced features like mocking [EXTERNAL_DEPENDENCIES] (APIs, databases, or modules). Include code snippets, best practices, and common pitfalls to avoid. Tailor the guide for [AUDIENCE_LEVEL] (beginners, intermediate, or advanced developers) and emphasize clarity and practicality.
أسئلة شائعة
ما هو Jest ولماذا يعتبر مفيدًا لاختبار JavaScript؟▼
Jest هو إطار اختبار شائع لـ JavaScript يتميز بسهولة الاستخدام وسرعة الأداء. يدعم Mocking وSnapshot Testing وهو مثالي لمشاريع React وNode.js.
كيف يمكن تثبيت Jest في مشروع JavaScript؟▼
يمكن تثبيت Jest باستخدام npm أو yarn عبر الأمر: npm install --save-dev jest أو yarn add --dev jest.
ما هي أنواع الاختبارات التي يدعمها Jest؟▼
يدعم Jest Unit Testing وIntegration Testing وSnapshot Testing وMocking Functions وAsync Testing.
كيف تكتب أول اختبار باستخدام Jest؟▼
أنشئ ملفًا بامتداد .test.js ثم استخدم الدالة test() أو it() لوصف الاختبار واستخدم expect() للتحقق من النتائج.
ما هي ميزة Snapshot Testing في Jest؟▼
تلتقط Snapshot Testing لقطة من مكون UI أو كائن وتقارنها بالإصدارات اللاحقة لاكتشاف التغييرات غير المتوقعة.
كيف يمكن اختبار الدوال غير المتزامنة (Async) باستخدام Jest؟▼
يمكن استخدام async/await مع Jest أو استخدام .resolves/.rejects أو استدعاء done() للتحكم في الاختبارات غير المتزامنة.