# test-doctor > Use this skill to diagnose and repair broken tests with a methodical, surgical approach. - Author: Mathildanesth Dev - Repository: vperreard/Mathildanesth - Version: 20260121200222 - Stars: 1 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/vperreard/Mathildanesth - Web: https://mule.run/skillshub/@@vperreard/Mathildanesth~test-doctor:20260121200222 --- --- name: test-doctor description: > Use this skill to diagnose and repair broken tests with a methodical, surgical approach. AUTO-ACTIVATE when user mentions (FR/EN): - test cassΓ©, broken test, failing test, test fail, test Γ©choue - erreur test, test error, assertion failed, timeout test - rΓ©parer test, fix test, corriger test, debug test - npm test fail, jest error, test suite failed AGENTS: Specialized agents (backend-specialist, frontend-specialist, database-specialist) MUST invoke this skill when asked to fix tests. Use: Skill("test-doctor") CRITICAL: NO mass corrections. ONE test at a time with validation. ALWAYS diagnose before fixing. ALWAYS consult DONT_DO.md first. Context: 1211 test files, 5561/6101 passing (540 failing). Stack: Jest, React Testing Library, Prisma, Next.js 15, TypeScript 5. --- # Test Doctor - Diagnostic & Repair Methodology **Mission**: Repair broken tests surgically, one at a time, with validation at each step. **Core Principle**: 🩺 **Diagnostic BEFORE Treatment** (never blind fixes) **Usage**: Can be invoked by User OR by specialized agents for guided workflow --- ## 🚨 Critical Rules (MUST Follow) 1. ❌ **NEVER** fix multiple tests in one go 2. ❌ **NEVER** fix without understanding root cause 3. ❌ **NEVER** skip validation step 4. βœ… **ALWAYS** consult DONT_DO.md first (anti-patterns) 5. βœ… **ALWAYS** run test in isolation before fixing 6. βœ… **ALWAYS** validate fix doesn't break other tests --- ## πŸ“‹ Diagnostic Workflow (Mandatory Steps) ### Step 1: ISOLATE & REPRODUCE ```bash # Run single test file in isolation npm test -- path/to/failing-test.test.ts # If still fails, run single test case npm test -- path/to/failing-test.test.ts -t "specific test name" ``` **Decision Point**: - βœ… Passes in isolation? β†’ **Problem: Test interdependency or setup order** - ❌ Fails in isolation? β†’ Continue to Step 2 --- ### Step 2: CLASSIFY ERROR TYPE Analyze error message and classify: **A) TypeScript Compilation Error** ``` Type 'X' is not assignable to type 'Y' Property 'foo' does not exist on type 'Bar' ``` β†’ **Root Cause**: Code source changed, test outdated β†’ **Fix**: Update test types OR fix code types β†’ **Consult**: `Skill("prisma-relations-mapping")` if Prisma/API related **B) Import/Dependency Error** ``` Cannot find module '../path/to/file' ReferenceError: X is not defined ``` β†’ **Root Cause**: File moved, renamed, or missing β†’ **Fix**: Update import paths OR restore missing file β†’ **Consult**: `Skill("architecture-lookup")` for current structure **C) Assertion/Logic Error** ``` Expected: X, Received: Y AssertionError: expect(received).toBe(expected) ``` β†’ **Root Cause**: Code behavior changed OR test expectation wrong β†’ **Fix**: Verify code logic FIRST, then update test if code is correct β†’ **Consult**: DONT_DO.md for known anti-patterns **D) Async/Timeout Error** ``` Timeout - Async callback was not invoked within the 5000 ms timeout ``` β†’ **Root Cause**: Missing await, improper mock, or cleanup issue β†’ **Fix**: Add proper async/await, fix mocks, add cleanup β†’ **Pattern**: See `resources/async-patterns.md` **E) Mock/Setup Error** ``` Cannot read property 'X' of undefined Mock function not called ``` β†’ **Root Cause**: Incorrect mock setup or missing beforeEach/afterEach β†’ **Fix**: Verify mock configuration, add proper setup/teardown β†’ **Pattern**: See `resources/mock-patterns.md` **F) Invalid Test Data / API Validation Error** ⭐ **MATHILDANESTH-SPECIFIC** ``` Expected: 201 (Created) Received: 422 (Validation error) Error: "DonnΓ©es de validation invalides" ``` β†’ **Root Cause**: Test data missing required fields (Prisma migrations, schema changes) β†’ **Fix**: Add required fields from Prisma schema (consult Skill("prisma-relations-mapping")) β†’ **Pattern**: See `resources/mathildanesth-specific-patterns.md` (Pattern MS-1, MS-6) β†’ **Frequency**: TRÈS HAUTE (~35-45% des tests cassΓ©s) --- ### Step 3: CONSULT DOCUMENTATION (Before Fixing!) **Mandatory checks in order**: 1. **`Skill("anti-patterns")`** (DONT_DO.md) - Check if error is a known anti-pattern - Example: "Never mock Prisma client directly in integration tests" 2. **Context-specific skills**: - API/Prisma error? β†’ `Skill("prisma-relations-mapping")` - UI component error? β†’ `Skill("frontend-patterns")` - Schema/model error? β†’ `Skill("architecture-lookup")` 3. **BREAKING_CHANGES.md** (if error appeared after git pull) - Check recent migrations (e.g., PascalCase migration Oct 26) - Check removed models (e.g., OnCall removal Oct 24) 4. **⭐ Mathildanesth-specific patterns** (CONSULT FIRST for high-frequency errors): - `resources/mathildanesth-specific-patterns.md` - 6 patterns couvrant 75-90% des tests cassΓ©s - Pattern MS-1: API Validation 422 (~30-40% des erreurs) - Pattern MS-6: Prisma Required Fields (~35-45% des erreurs) - Pattern MS-4: Global Mocks Removed (~20% des erreurs) - Pattern MS-5: Auth Mock Missing (~25% des erreurs) 5. **Test-specific resources** (generic patterns): - `resources/error-patterns.md` - Catalog of recurring errors - `resources/fix-strategies.md` - Proven fix strategies --- ### Step 4: FIX MINIMALLY (One Change Only) **Decision Tree**: ``` Error classified? ↓ Is CODE source wrong? YES β†’ Fix code source NO β†’ Continue ↓ Is TEST outdated/wrong? YES β†’ Update test expectations NO β†’ Continue ↓ Is SETUP/MOCK wrong? YES β†’ Fix setup/mock configuration NO β†’ Continue ↓ Is FILE STRUCTURE wrong? YES β†’ Update imports/paths NO β†’ Escalate (complex issue) ``` **Apply ONE fix only**: - If code is wrong β†’ Fix code (not test) - If test is wrong β†’ Fix test (not code) - If both unclear β†’ **ASK USER** with AskUserQuestion tool --- ### Step 5: VALIDATE (Mandatory) ```bash # 1. Run fixed test in isolation npm test -- path/to/test.test.ts # 2. If passes, run ALL tests in same file npm test -- path/to/test.test.ts # 3. If still passes, run related tests npm test -- path/to/related-module/ # 4. Only if ALL pass, consider success ``` **Validation Checklist**: - βœ… Test passes in isolation? - βœ… All tests in same file pass? - βœ… No new TypeScript errors? (`npm run typecheck:backend` or `typecheck:frontend`) - βœ… No new lint errors? (`npm run lint`) **If ANY validation fails**: 1. Revert the fix immediately 2. Return to Step 2 (reclassify error) 3. Consider alternative root cause --- ## 🎯 Common Patterns & Quick Fixes ### Pattern 1: Prisma PascalCase Migration (Oct 26, 2025) **Error**: ``` Property 'user' does not exist on type 'PrismaClient' ``` **Fix**: ```typescript // ❌ Old (snake_case) await prisma.user.findMany() // βœ… New (PascalCase) await prisma.user.findMany() // Already correct! Check import ``` **Consult**: `Skill("prisma-relations-mapping")` for correct model names --- ### Pattern 2: React Testing Library Async Updates **Error**: ``` Warning: An update to Component inside a test was not wrapped in act(...) ``` **Fix**: ```typescript // ❌ Wrong render() expect(screen.getByText('Loading')).toBeInTheDocument() // βœ… Correct render() await waitFor(() => { expect(screen.getByText('Loaded')).toBeInTheDocument() }) ``` **Consult**: `resources/async-patterns.md` --- ### Pattern 3: Mock Cleanup **Error**: ``` Test suite failed to run jest.mock is already defined ``` **Fix**: ```typescript // Add cleanup in afterEach afterEach(() => { jest.clearAllMocks() jest.resetModules() }) ``` **Consult**: `resources/mock-patterns.md` --- ## πŸ“Š Success Metrics (Track Progress) After each fix, report: - βœ… Test file: `path/to/test.test.ts` - βœ… Error type: (A/B/C/D/E from Step 2) - βœ… Root cause: Brief explanation - βœ… Fix applied: One-line summary - βœ… Validation: All checks passed? (Yes/No) - βœ… Tests now passing: X/Y in file **Example Report**: ``` βœ… Fixed: src/app/api/leaves/__tests__/route.test.ts Error Type: C (Assertion Error) Root Cause: API response schema changed (PascalCase migration) Fix: Updated test expectations to match new schema Validation: βœ… All checks passed Tests: 12/12 passing in file ``` --- ## πŸ”„ Escalation Protocol **When to escalate** (ask user): 1. Root cause unclear after Step 2 classification 2. Fix requires architectural decision (e.g., change API contract) 3. Fix breaks other tests (validation fails) 4. Error pattern not documented in resources **Use AskUserQuestion tool** with: - Clear description of the issue - Your diagnostic findings - 2-3 options with trade-offs - Your recommended approach --- ## πŸ“š Progressive Resources For detailed patterns and strategies: - **`resources/error-patterns.md`** - Catalog of 20+ recurring error patterns - **`resources/fix-strategies.md`** - Proven fix strategies by error type - **`resources/async-patterns.md`** - Async/await patterns for RTL - **`resources/mock-patterns.md`** - Mock setup patterns (Prisma, API, WebSocket) - **`resources/validation-checklist.md`** - Complete validation checklist --- ## πŸŽ“ Learning Loop After each successful fix: 1. Document pattern if new β†’ Add to `resources/error-patterns.md` 2. Update DONT_DO.md if anti-pattern discovered 3. Share fix strategy with team (commit message) --- **Version**: 1.0.0 (November 2025) **Maintainer**: Test Doctor Skill **Last Update**: Based on 1211 test files, 540 failing tests context