test: use Bun's fake timers to speed up unit tests
- Dominant language
- TypeScript
- Stars
- 12
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
### Background
Bun v1.3.4 introduced [fake timers](https://bun.sh/docs/test/dates-times) for `bun:test`, enabling time manipulation without real delays. This is a great opportunity to speed up our unit tests.
### Current state
Our unit tests use manual `setTimeout` mocking to test timeout behavior. For example, in `test/unit/disposable/timeout.test.ts`:
```ts
const setTimeoutSpy = spyOn(globalThis, "setTimeout")
.mockImplementation((callback: any, delay: any) => {
timeoutCallback = callback;
capturedDelay = delay;
return 0;
});
// Later, manually invoke the callback
timeoutCallback();
```
This works but requires boilerplate and manual callback invocation. Bun's fake timers provide a cleaner API.
### Proposal
Refactor unit tests to use Bun's fake timers:
```ts
import { jest } from "bun:test";
test("disposal timeout", () => {
jest.useFakeTimers();
// ... set up test ...
// Advance time instead of manual callback
jest.advanceTimersByTime(100);
// Assertions...
jest.useRealTimers();
});
```
### Scope
**In scope (unit tests only):**
- `test/unit/disposable/timeout.test.ts` — Primary candidate, already mocks `setTimeout`
- `test/unit/disposable/concurrent.test.ts` — Uses `Bun.sleep()` for timing
**Out of scope:**
- Integration/contract/e2e tests — These test real backend behavior where actual time delays are necessary
### Implementation notes
- Bun's fake timers are Jest-compatible: `jest.useFakeTimers()`, `jest.advanceTimersByTime()`, `jest.useRealTimers()`
- Always call `useRealTimers()` in cleanup to avoid test pollution
- Check [Bun docs](https://bun.sh/docs/test/dates-times) for full API
- Requires Bun ≥1.3.4 (update `@types/bun` if needed)
### Getting started
1. Read the [Bun fake timers docs](https://bun.sh/docs/test/dates-times)
2. Start with `test/unit/disposable/timeout.test.ts`
3. Run `bun run test:unit` to verify changes
4. Open a PR with the refactored tests
### References
- [Bun v1.3.4 release notes](https://bun.com/blog/bun-v1.3.4)
- [Bun docs: Dates and times](https://bun.sh/docs/test/dates-times)
- [Jest Timer Mocks](https://jestjs.io/docs/timer-mocks) (API is compatible)
Contributor guide
Research direction
Read Bun's fake-timer documentation, then inspect test/unit/disposable/timeout.test.ts and test/unit/disposable/concurrent.test.ts. Run bun run test:unit before and after replacing the manual timer handling within the stated scope. Done means the unit tests use fake timers, preserve their timing assertions, clean up with useRealTimers(), and pass without real delays.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- bun, typescript
- Domain
- testing-qa
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100