Skip to content

πŸ› fix: address Copilot review on #3966 β€” fix mock leaks and remove duplicate test#3971

Merged
clubanderson merged 1 commit intomainfrom
fix/test-mock-leaks
Mar 31, 2026
Merged

πŸ› fix: address Copilot review on #3966 β€” fix mock leaks and remove duplicate test#3971
clubanderson merged 1 commit intomainfrom
fix/test-mock-leaks

Conversation

@clubanderson
Copy link
Copy Markdown
Collaborator

Summary

  • Replace Object.defineProperty/global.fetch = with vi.stubGlobal() in all 3 test files so vitest properly tracks and restores global mocks
  • Add vi.unstubAllGlobals() to afterEach for proper cleanup between tests
  • Delete duplicate useClusterProgress.test.ts from hooks/ (canonical copy in hooks/__tests__/)

Addresses all 4 Copilot review comments from #3966.

Test plan

  • All 29 tests pass across the 3 affected test files
  • TypeScript compiles without errors

…plicate test

- Replace Object.defineProperty/global assignment with vi.stubGlobal()
  for WebSocket and fetch mocks so vitest tracks and restores them
- Add vi.unstubAllGlobals() to afterEach in all 3 test files
- Re-establish WebSocket mock in beforeEach (cleared by unstubAllGlobals)
- Delete duplicate useClusterProgress.test.ts from hooks/ (canonical
  copy lives in hooks/__tests__/)

Signed-off-by: Andrew Anderson <andy@clubanderson.com>
Copilot AI review requested due to automatic review settings March 31, 2026 13:37
@kubestellar-prow kubestellar-prow bot added the dco-signoff: yes Indicates the PR's author has signed the DCO. label Mar 31, 2026
@kubestellar-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign clubanderson for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@netlify
Copy link
Copy Markdown

netlify bot commented Mar 31, 2026

βœ… Deploy Preview for kubestellarconsole ready!

Name Link
πŸ”¨ Latest commit dabd495
πŸ” Latest deploy log https://app.netlify.com/projects/kubestellarconsole/deploys/69cbce259841de00098b9e7a
😎 Deploy Preview https://deploy-preview-3971.console-deploy-preview.kubestellar.io
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kubestellar-prow kubestellar-prow bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 31, 2026
@github-actions
Copy link
Copy Markdown
Contributor

πŸ‘‹ Hey @clubanderson β€” thanks for opening this PR!

πŸ€– This project is developed exclusively using AI coding assistants.

Please do not attempt to code anything for this project manually.
All contributions should be authored using an AI coding tool such as:

This ensures consistency in code style, architecture patterns, test coverage,
and commit quality across the entire codebase.


This is an automated message.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR tightens Vitest test isolation for several hooks by switching global mocking to vi.stubGlobal() with explicit cleanup, and removes a duplicate useClusterProgress test file so the canonical suite lives under src/hooks/__tests__/.

Changes:

  • Replace ad-hoc global mocking (Object.defineProperty, global.fetch = ...) with vi.stubGlobal() in the affected tests.
  • Add vi.unstubAllGlobals() to afterEach() to prevent global mock leakage across tests/files.
  • Delete the duplicate web/src/hooks/useClusterProgress.test.ts (keeping the __tests__ version).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
web/src/hooks/useClusterProgress.test.ts Removes duplicate test file to avoid duplicate execution.
web/src/hooks/tests/useUpdateProgress.test.ts Uses vi.stubGlobal('WebSocket', ...) + vi.unstubAllGlobals() for tracked, restorable global stubbing.
web/src/hooks/tests/useLocalAgent.test.ts Uses vi.stubGlobal('fetch', ...) + vi.unstubAllGlobals() to avoid global fetch mock leaks between tests.
web/src/hooks/tests/useClusterProgress.test.ts Uses vi.stubGlobal('WebSocket', ...) + vi.unstubAllGlobals() and keeps per-test state reset.

Comment on lines 61 to 71
// Assign mock to global before importing the hook
Object.defineProperty(globalThis, 'WebSocket', {
writable: true,
value: MockWebSocket,
})
vi.stubGlobal('WebSocket', MockWebSocket)

import { useClusterProgress } from '../useClusterProgress'

describe('useClusterProgress', () => {
beforeEach(() => {
vi.useFakeTimers()
wsInstances = []
vi.stubGlobal('WebSocket', MockWebSocket)
})
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSocket is stubbed twice: once at module scope (before the import) and again in beforeEach(). Since useClusterProgress only instantiates WebSocket inside useEffect (not at module-eval time), the module-scope vi.stubGlobal is unnecessary and can be removed to avoid double-stubbing and make global cleanup more predictable (keep the per-test stub + vi.unstubAllGlobals() in afterEach).

Copilot uses AI. Check for mistakes.
Comment on lines 66 to 76
// Assign mock to global before importing the hook
Object.defineProperty(globalThis, 'WebSocket', {
writable: true,
value: MockWebSocket,
})
vi.stubGlobal('WebSocket', MockWebSocket)

import { useUpdateProgress } from '../useUpdateProgress'

describe('useUpdateProgress', () => {
beforeEach(() => {
vi.useFakeTimers()
wsInstances = []
vi.stubGlobal('WebSocket', MockWebSocket)
})
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSocket is stubbed both at module scope and again in beforeEach(). Because useUpdateProgress only calls new WebSocket(...) inside useEffect (not during module import), the module-scope vi.stubGlobal('WebSocket', ...) isn’t needed and leads to double-stubbing; consider removing the module-scope stub and relying on the per-test stub + vi.unstubAllGlobals() cleanup.

Copilot uses AI. Check for mistakes.
@clubanderson
Copy link
Copy Markdown
Collaborator Author

πŸ”„ Auto-Applying Copilot Code Review

Copilot code review found 0 code suggestion(s) and 2 general comment(s).

Also address these general comments:

  • web/src/hooks/__tests__/useClusterProgress.test.ts (line 71): WebSocket is stubbed twice: once at module scope (before the import) and again in beforeEach(). Since `useClusterPro
  • web/src/hooks/__tests__/useUpdateProgress.test.ts (line 76): WebSocket is stubbed both at module scope and again in beforeEach(). Because useUpdateProgress only calls `new Web

Push all fixes in a single commit. Run cd web && npm run build && npm run lint before committing.


Auto-generated by copilot-review-apply workflow.

@clubanderson clubanderson merged commit 2d33562 into main Mar 31, 2026
28 of 33 checks passed
@kubestellar-prow kubestellar-prow bot deleted the fix/test-mock-leaks branch March 31, 2026 13:50
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your contribution! Your PR has been merged.

Check out what's new:

Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey

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

Labels

dco-signoff: yes Indicates the PR's author has signed the DCO. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants