Skip to content

[Test Improver] Add unit tests for useInterval composable - #113

Draft
github-actions[bot] wants to merge 1 commit into
masterfrom
test-assist/use-interval-composable-525f282ed64790cc
Draft

[Test Improver] Add unit tests for useInterval composable#113
github-actions[bot] wants to merge 1 commit into
masterfrom
test-assist/use-interval-composable-525f282ed64790cc

Conversation

@github-actions

Copy link
Copy Markdown

🤖 This PR was created by Test Improver, an automated AI assistant focused on improving test coverage.

Goal and Rationale

shell/composables/useInterval.ts had no test coverage. It registers a setInterval on onMounted and clears it on onBeforeUnmount — a lifecycle-driven side effect that is easy to misuse (e.g. timer leaks, interval not cleared on unmount). Testing this verifies correct lifecycle integration and that the cleanup guard works properly.

Approach

Each test mounts/unmounts a minimal defineComponent harness that calls useInterval, then advances fake timers to observe call counts. This isolates the composable without requiring a real component.

Tests (8 total):

# Test What it verifies
1 does not call fn before the component is mounted fn is only invoked via setInterval, not eagerly
2 calls fn after the specified delay once mounted interval starts on onMounted
3 calls fn repeatedly on each interval tick setInterval (not setTimeout) behavior
4 does not call fn before the delay elapses off-by-one boundary at delay - 1 ms
5 clears the interval on unmount so fn is not called afterwards clearInterval guard in onBeforeUnmount
6 stops calling fn immediately when unmounted mid-interval cleanup works even partway through a tick
7–8 table-driven: short (100 ms / 350 ms → 3 calls) and long (2000 ms / 5000 ms → 2 calls) delays delay parameter is respected accurately

Coverage Impact

useInterval.ts moves from 0 % → ~100 % statement/branch coverage (all branches: mounted callback, unmount guard, if (interval.value) check).

Trade-offs

  • Uses jest.useFakeTimers() per-test to avoid wall-clock delays.
  • defineComponent harness keeps tests self-contained and avoids mocking Vue internals.

Reproducibility

YARN_IGNORE_ENGINES=true NODE_OPTIONS=--max_old_space_size=8192 yarn test:ci --testPathPattern=shell/composables/useInterval

Test Status

✅ All 8 tests pass. ESLint reports 0 warnings/errors.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • download.cypress.io

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "download.cypress.io"

See Network Configuration for more information.

Generated by Daily Test Improver · ● 5.4M ·

Test the setInterval lifecycle composable that registers/clears
an interval on component mount/unmount.

- fn is not invoked before mount
- fn fires after the specified delay once mounted
- fn fires repeatedly on each tick
- fn is not called before the delay elapses
- interval is cleared on unmount (no further calls)
- unmounting mid-interval prevents any call
- table-driven cases validate count for short/long delays

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants