|
| 1 | +# Test Setup Analysis and Improvement Plan |
| 2 | + |
| 3 | +## Current Test Architecture Analysis |
| 4 | + |
| 5 | +The browser extension uses a comprehensive but complex test setup with several interesting patterns: |
| 6 | + |
| 7 | +### 1. **Vitest Configuration Analysis** |
| 8 | +- Uses `vitest` with WxtVitest plugin for browser extension testing |
| 9 | +- Node environment with thread pool |
| 10 | +- Global test utilities enabled |
| 11 | +- Custom setup file for DOM mocking |
| 12 | + |
| 13 | +### 2. **DOM Mocking Strategy (linkedom)** |
| 14 | +- Uses linkedom instead of jsdom/happy-dom |
| 15 | +- Creates complete HTML document structure |
| 16 | +- Global DOM object mocking approach |
| 17 | +- Manual assignment to globalThis |
| 18 | + |
| 19 | +### 3. **HAR-based Testing System** |
| 20 | +- Sophisticated HAR recording/playback for GitHub pages |
| 21 | +- Express server for viewing HAR content with Gitcasso integration |
| 22 | +- URL patching and webextension-polyfill mocking |
| 23 | +- Sanitization and redaction for privacy |
| 24 | + |
| 25 | +### 4. **Overtype Module Mocking** |
| 26 | +- Comprehensive mock of OverType editor |
| 27 | +- Constructor pattern mocking with array return |
| 28 | +- Static method mocking (setCodeHighlighter) |
| 29 | + |
| 30 | +## Issues Identified |
| 31 | + |
| 32 | +### **Critical Issues:** |
| 33 | +1. **Global DOM pollution** - Shared DOM state between tests |
| 34 | +2. **No test isolation** - DOM modifications persist across tests |
| 35 | +3. **Memory leak potential** - No cleanup of linkedom instances |
| 36 | +4. **Incomplete mock validation** - Mocks don't match real API contracts |
| 37 | + |
| 38 | +### **Performance Issues:** |
| 39 | +5. **HAR file loading** - Synchronous file operations in tests |
| 40 | +6. **Large fixture files** - 600KB+ HAR files loaded repeatedly |
| 41 | +7. **Express server overhead** - Unnecessary for unit tests |
| 42 | + |
| 43 | +### **Maintainability Issues:** |
| 44 | +8. **Complex mock setup** - Hard to understand/maintain mocks |
| 45 | +9. **No mock type safety** - Mocks lack proper TypeScript types |
| 46 | +10. **Test-specific globals** - Makes tests harder to reason about |
| 47 | + |
| 48 | +## Improvement Plan |
| 49 | + |
| 50 | +### **Phase 1: Test Isolation & Cleanup** |
| 51 | +1. Implement proper test setup/teardown with fresh DOM per test |
| 52 | +2. Add memory cleanup for linkedom instances |
| 53 | +3. Create isolated test contexts |
| 54 | +4. Add test environment validation |
| 55 | + |
| 56 | +### **Phase 2: Mock Strategy Improvements** |
| 57 | +1. Create typed mock factories for OverType |
| 58 | +2. Implement mock validation against real interfaces |
| 59 | +3. Add mock behavior verification |
| 60 | +4. Separate unit vs integration test mocking |
| 61 | + |
| 62 | +### **Phase 3: HAR Testing Optimization** |
| 63 | +1. Create lightweight test fixtures |
| 64 | +2. Implement HAR caching strategy |
| 65 | +3. Add lazy loading for test data |
| 66 | +4. Separate HAR viewer from test infrastructure |
| 67 | + |
| 68 | +### **Phase 4: Test Architecture Enhancement** |
| 69 | +1. Add test utilities for common patterns |
| 70 | +2. Implement custom matchers for DOM assertions |
| 71 | +3. Add performance benchmarks for tests |
| 72 | +4. Create test documentation and examples |
| 73 | + |
| 74 | +## Specific Code Improvements |
| 75 | + |
| 76 | +### setup.ts improvements: |
| 77 | +- Add `beforeEach`/`afterEach` hooks to reset DOM state |
| 78 | +- Create a factory function for DOM creation |
| 79 | +- Add proper TypeScript types for global extensions |
| 80 | +- Implement DOM cleanup utilities |
| 81 | + |
| 82 | +### vitest.config.ts improvements: |
| 83 | +- Consider adding coverage configuration |
| 84 | +- Add test timeout configurations |
| 85 | +- Configure proper test reporters |
| 86 | +- Add test isolation settings |
| 87 | + |
| 88 | +### github.test.ts improvements: |
| 89 | +- Extract HAR loading into a test utility |
| 90 | +- Add proper async/await patterns |
| 91 | +- Implement test data builders |
| 92 | +- Add more comprehensive assertions |
| 93 | +- Clean up DOM state after each test |
| 94 | +- Type the `enhanced` variable properly |
| 95 | +- Add error case testing |
0 commit comments