Unit Test

اختبار الوحدة

GLS-000149

Intermediateengineering1 min read
unit testingjest testvitesttest functionisolated test

Definition

A test that checks a single function or component in isolation — fast and reliable.

اختبار يفحص دالة أو مكوّن واحدًا بشكل معزول — سريع وموثوق.

Why It Matters

Unit tests catch bugs early — before they reach production. A test for the glossary slug builder catches edge cases like 'Next.js' → 'nextjs' correctly.

اختبارات الوحدة تكتشف الأخطاء مبكرًا — قبل وصولها إلى الإنتاج. اختبار لباني slug الغلوساري يكتشف الحالات الحافة مثل 'Next.js' → 'nextjs' بشكل صحيح.

Full Definition

Unit tests verify that individual pieces of code (functions, components) work correctly in isolation. They're fast (run in milliseconds), easy to write, and catch regressions when code changes. Tools: Jest (most common), Vitest (fast, works with Vite). Unit tests are most valuable for pure functions — data transformations, validators, utility functions. 404Fault's regression test suite is a form of smoke testing.
اختبارات الوحدة تتحقق من أن قطع الكود الفردية (الدوال والمكونات) تعمل بشكل صحيح بشكل معزول. إنها سريعة (تعمل في ميلي ثوانٍ) وسهلة الكتابة وتكتشف الانتكاسات عند تغيير الكود. الأدوات: Jest (الأكثر شيوعًا) وVitest (سريع ويعمل مع Vite). اختبارات الوحدة أكثر قيمةً للدوال الخالصة — تحويلات البيانات والمُتحقِّقات والدوال المساعدة. مجموعة اختبارات الانتكاس في 404Fault هي شكل من اختبار الدخان.

Example Usage

test('slug builder', () => { expect(buildSlug('Next.js')).toBe('nextjs') })

test('باني slug', () => { expect(buildSlug('Next.js')).toBe('nextjs') })

Knowledge Graph

Avoid these mistakes when using Unit Test:

1

Testing implementation details instead of behavior (tests break when you refactor even if behavior is unchanged)

2

100% coverage target — forces useless tests on trivial code

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Unit Test.

Explain:
1. What is Unit Test and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Testing implementation details instead of behavior (tests break when you refactor even if behavior is unchanged), 100% coverage target — forces useless tests on trivial code
5. Best practices and production tips

Official Resources