Skip to content

test(ui): consolidate console test infrastructure - #6887

Merged
otavio merged 7 commits into
masterfrom
test/ui/cleanup-console-tests
Aug 11, 2026
Merged

test(ui): consolidate console test infrastructure#6887
otavio merged 7 commits into
masterfrom
test/ui/cleanup-console-tests

Conversation

@luizhf42

Copy link
Copy Markdown
Member

What

Unified scattered test utilities (src/test/, src/test-utils/, src/__mocks__/) into a single src/tests/ directory, replaced ~44 inline component mocks with 4 shared mocks + 1 auto-mock, and removed per-file afterEach(cleanup) and HTMLDialogElement stubs that setup.ts already provides globally.

Why

Test infrastructure was duplicated across three directories and dozens of files. The same Drawer, ConfirmDialog, CopyButton, and BaseDialog mocks were copy-pasted into every consumer, drifting slightly between copies. afterEach(cleanup) appeared in 38 files despite being in the global setup. This made adding new tests slower and existing tests harder to maintain.

Changes

  • src/tests/: new canonical home for test utilities — moved setup.ts, decodeB64url.ts, renderHookWithRouter.tsx, and their test from the old scattered locations. Deleted src/test/createAxiosError.ts (zero consumers).
  • src/tests/mocks.tsx: single source for MockDrawer, MockConfirmDialog, MockCopyButton, and MockBaseDialog — minimal DOM structure exposing role="dialog", ARIA attributes, and interactive elements that consumer tests query against.
  • src/hooks/__mocks__/useFocusTrap.ts: Vitest auto-mock (sibling __mocks__/ convention), activated globally via vi.mock("@/hooks/useFocusTrap") in setup.ts. Test files for the real hook and Drawer opt out with vi.unmock().
  • setup.ts: added global useFocusTrap mock alongside the existing @/env mock.
  • Per-file cleanup: removed redundant afterEach(cleanup) (38 files), HTMLDialogElement.prototype.showModal/.close stubs (2 files), and unused imports left behind.
  • useDeviceChooser.test.ts: fixed ../../client/@/client/ import (pre-existing convention violation caught during this cleanup).

Testing

All 211 test files (3024 tests) pass. Key areas to verify: files that vi.unmock("@/hooks/useFocusTrap") (useFocusTrap.test.tsx, Drawer.test.tsx) — these test the real implementation and must not pick up the global mock.

Merge `src/test/` and `src/test-utils/` into a single `src/tests/`
directory and delete the unused `createAxiosError` helper.

`src/__mocks__/env.ts` stays in place because Vitest's manual mock
resolution requires the `__mocks__/` directory to be a sibling of
the module it replaces.
The hook is a jsdom workaround needed by every dialog/drawer test.
A global `vi.mock` in `setup.ts` paired with
`hooks/__mocks__/useFocusTrap.ts` replaces 12 identical inline
mocks. The two files that test the real hook (`useFocusTrap.test`,
`Drawer.test`) opt out with `vi.unmock`.
Promote the Drawer mock from `admin/users/__tests__/mocks.tsx` to
`src/tests/mocks.tsx` and replace 9 inline copies. The shared mock
renders `role="dialog"` with `aria-label={title}`, which required
tightening two `/private key/i` label queries in the vault
`KeyDrawer` tests to `^private key$` so they don't collide with
the drawer title.
Add `MockConfirmDialog` to `src/tests/mocks.tsx` and replace 8
identical inline ConfirmDialog mocks.
Add `MockCopyButton` to `src/tests/mocks.tsx` and replace 10
inline CopyButton mocks.
Add `MockBaseDialog` to `src/tests/mocks.tsx` and replace 5 inline
BaseDialog mocks.
`setup.ts` already calls `afterEach(cleanup)` and stubs
`HTMLDialogElement.prototype.showModal`/`.close` globally, so
the per-file copies were redundant.
@luizhf42
luizhf42 requested a review from a team as a code owner August 11, 2026 19:05
@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown

Claude finished @luizhf42's task in 2m 3s —— View job


Code Review Complete

Reviewed 81 files across code quality, security, testing, language patterns, and architecture — no new issues found. The code looks good as-is.

I verified the higher-risk areas of this refactor directly:

  • Auto-mock wiring — the real useFocusTrap is a named-only export, and src/hooks/__mocks__/useFocusTrap.ts mirrors it. Only BaseDialog.tsx and Drawer.tsx consume it; the two test files that exercise the real implementation correctly opt out via vi.unmock("@/hooks/useFocusTrap") (hoisted above imports, so placement is fine).
  • Net-positive diffs (useDevices, encoding, NamespaceGuard, SignUpGuard, useAdminUsers, useBackdropClose, useFocusTrap.test, …) are Prettier re-wrapping + afterEach(cleanup) removal + @/test → @/tests import updates — no behavioral changes.
  • No dangling referencessrc/test/, src/test-utils/, and the deleted per-file mocks.tsx have zero remaining importers; createAxiosError has no consumers; __mocks__/env.ts is preserved for vi.mock("@/env").
  • Configvite.config.ts setupFiles and the global mocks in setup.ts are consistent with the new layout.

If you push additional changes and want a new review, tag @shellhub-io/admin and a team member can trigger it.
· test/ui/cleanup-console-tests

@otavio
otavio merged commit 6261654 into master Aug 11, 2026
41 of 42 checks passed
@otavio
otavio deleted the test/ui/cleanup-console-tests branch August 11, 2026 21:17
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.

2 participants