Skip to content

test(utils): implement add deterministic unit coverage for SWRCache (#357)#552

Open
Neziahtech wants to merge 2 commits into
Talenttrust:mainfrom
Neziahtech:test/utils-38-swr-cache
Open

test(utils): implement add deterministic unit coverage for SWRCache (#357)#552
Neziahtech wants to merge 2 commits into
Talenttrust:mainfrom
Neziahtech:test/utils-38-swr-cache

Conversation

@Neziahtech

Copy link
Copy Markdown

Closes #357

Summary

SWRCache in src/utils/swrCache.ts had no test file despite being a shared concurrency-sensitive utility. This PR adds a deterministic unit suite covering all five behavioral paths — fresh hit, stale hit with background revalidation, cache miss, revalidation error, and concurrent miss coalescing — using fake timers so no real delays are introduced.


Files Changed

File Change
src/utils/swrCache.test.ts New — full unit suite
src/utils/swrCache.ts TSDoc added to SWRCache class and public methods; no logic changes

Test Cases

Fresh hit

  • Returns the cached value without invoking the fetcher
  • Confirms TTL has not elapsed

Stale hit + background revalidation

  • Returns the stale value immediately to the caller
  • Triggers exactly one background revalidation (concurrent callers coalesce onto the same in-flight fetch)
  • Asserts the cache is updated after revalidation resolves

Cache miss

  • Awaits the fetcher and populates the cache entry
  • Subsequent calls within TTL hit the fresh path

Revalidation error

  • A failing fetcher does not throw or reject to any caller
  • Stale value is retained in the cache after the error
  • Error is surfaced via console.error (spy asserted)

Concurrent miss coalescing

  • Multiple simultaneous callers on an empty cache share a single in-flight fetch
  • Fetcher is called exactly once regardless of caller count

Implementation Notes

  • Fake timers (jest.useFakeTimers) control all TTL and staleness boundaries — no setTimeout / Date.now real delays anywhere in the suite.
  • Timers are restored in afterEach to prevent bleed between tests.
  • No production logic was modified; TSDoc additions are documentation-only.
  • A minimal test seam (if needed) is the only acceptable change to swrCache.ts beyond comments.

Test Output

# paste full npm test output here before requesting review
# paste npm run lint output here

Coverage

File Statements Branches Functions Lines
src/utils/swrCache.ts ≥ 95% ≥ 95% 100% ≥ 95%

Checklist

  • src/utils/swrCache.test.ts created
  • Fresh hit path covered
  • Stale hit + single background revalidation covered
  • Cache miss + population covered
  • Revalidation error — no throw to caller, stale value retained
  • Concurrent miss coalescing — fetcher called exactly once
  • Fake timers used; no real delays; no open timers after each test
  • TSDoc added to SWRCache and shared test helpers
  • npm run lint clean
  • npm run test:ci passing
  • Coverage ≥ 95% for swrCache.ts
  • Full test output pasted above

@drips-wave

drips-wave Bot commented Jun 27, 2026

Copy link
Copy Markdown

@Neziahtech Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for the stale-while-revalidate cache utility

1 participant