es-toolkit
es-toolkit copied to clipboard
test: Improve debounce/throttle test performance by replacing real timers with fake timers
Summary
Replaced real timer usage with fake timers in debounce and throttle tests to eliminate flaky test risks and significantly improve test execution time.
Problem
- Tests were using real timers with
delay()function, making them slow and potentially flaky - Real timer-based tests are susceptible to timing issues in CI environments
- Total test execution time was unnecessarily long due to actual time delays
Solution
- Targeted approach: Only replaced real timers with fake timers for debounce and throttle tests that were the primary cause of slow test execution
- Simple replacements: For straightforward cases, replaced
delay()calls withvi.advanceTimersByTime()for precise time control - Preserved complex tests: When tests required additional modifications beyond simple
delay()replacement, maintained original tests usingvi.useRealTimers()to avoid breaking existing functionality
Performance Improvements
Individual Test Files
| Test File | Before | After | Improvement |
|---|---|---|---|
| src/compat/function/debounce.spec.ts | 4.08s | 1.16s | 3.52x faster |
| src/compat/function/throttle.spec.ts | 5.08s | 2.02s | 2.52x faster |
| src/function/debounce.spec.ts | 903ms | 12ms | 75.25x faster |
| src/function/throttle.spec.ts | 1.45s | 15ms | 96.67x faster |
Overall Test Suite Impact
Test execution time: 19.64s → 11.33s (42.3% improvement)
Benefits
- Eliminated flaky test risks - No dependency on system timing
- Faster CI/CD pipeline - Reduced test execution time by 42%
- More reliable tests - Deterministic timing behavior