-
Notifications
You must be signed in to change notification settings - Fork 48
Set up Vitest Testing Infrastructure and Core Utilities Tests (vertex app) #3692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c15a3d8
Set up Vitest and Testing Library
Codebmk 96035e6
Add tests for core utils; update Vitest setup
Codebmk 131dc71
Add testing guide and npm test scripts
Codebmk 8baa34d
Add Type Check & Coverage to CI and Vitest
Codebmk 59538a8
fix version number
Codebmk 55b2e8a
Use window.localStorage and robust fakes in tests
Codebmk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| # Testing Guide | ||
|
|
||
| This guide defines the testing conventions for Vertex. Follow it when adding or updating automated tests. | ||
|
|
||
| ## Test stack | ||
|
|
||
| - Vitest is the test runner. | ||
| - React Testing Library is used for React components and hooks. | ||
| - `@testing-library/jest-dom` matchers are loaded globally from `vitest.setup.ts`. | ||
| - The default test environment is `jsdom`. | ||
| - Do not add Jest. | ||
|
|
||
| ## Commands | ||
|
|
||
| Run the full unit/component suite once: | ||
|
|
||
| ```bash | ||
| npm run test | ||
| ``` | ||
|
|
||
| Run tests in watch mode while developing: | ||
|
|
||
| ```bash | ||
| npm run test:watch | ||
| ``` | ||
|
|
||
| Generate coverage: | ||
|
|
||
| ```bash | ||
| npm run test:coverage | ||
| ``` | ||
|
|
||
| Run TypeScript validation: | ||
|
|
||
| ```bash | ||
| npx tsc --noEmit | ||
| ``` | ||
|
|
||
| Before opening a PR that adds or changes tests, run: | ||
|
|
||
| ```bash | ||
| npm run test | ||
| npx tsc --noEmit | ||
| ``` | ||
|
|
||
| ## File organization | ||
|
|
||
| Use co-located test files for unit, hook, and component tests. | ||
|
|
||
| ```txt | ||
| core/utils/status.ts | ||
| core/utils/status.test.ts | ||
|
|
||
| core/hooks/useClipboard.ts | ||
| core/hooks/useClipboard.test.tsx | ||
|
|
||
| components/ui/Button.tsx | ||
| components/ui/Button.test.tsx | ||
| ``` | ||
|
|
||
| Use: | ||
|
|
||
| - `*.test.ts` for TypeScript utilities and non-JSX logic. | ||
| - `*.test.tsx` for React components, hooks, providers, and tests that render JSX. | ||
|
|
||
| Do not create `tests` folders under every feature or utility folder for unit tests. Co-location keeps tests close to the behavior they protect and makes future service/module migrations safer because source files and tests move together. | ||
|
|
||
| Use a shared root-level `test/` folder only for reusable test infrastructure when needed: | ||
|
|
||
| ```txt | ||
| test/ | ||
| factories/ | ||
| mocks/ | ||
| utils/ | ||
| ``` | ||
|
|
||
| End-to-end tests should live separately under an `e2e/` folder when Playwright is introduced. | ||
|
|
||
| ## Writing good tests | ||
|
|
||
| Test public behavior, not private implementation details. | ||
|
|
||
| Prefer assertions that describe what a user, caller, or consuming module observes: | ||
|
|
||
| - returned values | ||
| - rendered accessible UI | ||
| - emitted events | ||
| - storage updates | ||
| - error messages | ||
| - callback behavior | ||
|
|
||
| Avoid assertions that depend on: | ||
|
|
||
| - private helper functions | ||
| - internal state shape unless it is the public contract | ||
| - exact CSS classes, except where the class string is the actual utility output | ||
| - snapshots for utility tests | ||
|
|
||
| Use table-driven tests when a function has repeated input/output cases. | ||
|
|
||
| ## Utility tests | ||
|
|
||
| Utility tests should be small, deterministic, and focused. | ||
|
|
||
| Cover: | ||
|
|
||
| - happy paths | ||
| - boundary values | ||
| - empty, nullish, or malformed inputs where supported | ||
| - error branches | ||
| - real regression cases | ||
|
|
||
| For date or time logic: | ||
|
|
||
| ```ts | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date("2026-06-28T12:00:00.000Z")); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
| ``` | ||
|
|
||
| For browser storage logic: | ||
|
|
||
| ```ts | ||
| beforeEach(() => { | ||
| localStorage.clear(); | ||
| sessionStorage.clear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| localStorage.clear(); | ||
| sessionStorage.clear(); | ||
| }); | ||
| ``` | ||
|
|
||
| Use real `jsdom` storage or the shared setup fallback. Do not manually mock storage inside each test file unless a specific failure mode must be simulated. | ||
|
|
||
| ## Hook and component tests | ||
|
|
||
| Use React Testing Library and test behavior through rendered output or hook results. | ||
|
|
||
| Prefer: | ||
|
|
||
| - `screen.getByRole` | ||
| - `screen.getByLabelText` | ||
| - `screen.getByText` | ||
| - `userEvent` for interactions | ||
|
|
||
| Avoid: | ||
|
|
||
| - querying by implementation-specific selectors | ||
| - testing React internals | ||
| - asserting on state setters directly | ||
|
|
||
| When hooks/components need providers, use shared render helpers rather than duplicating wrapper setup in every file. | ||
|
|
||
| Recommended future helpers: | ||
|
|
||
| ```txt | ||
| test/utils/renderWithProviders.tsx | ||
| test/utils/renderHookWithProviders.tsx | ||
| test/factories/ | ||
| test/mocks/ | ||
| ``` | ||
|
|
||
| ## Mocking guidance | ||
|
|
||
| Mock at boundaries, not everywhere. | ||
|
|
||
| Good mock boundaries include: | ||
|
|
||
| - network clients | ||
| - NextAuth session state | ||
| - Next.js router/navigation APIs | ||
| - timers | ||
| - browser APIs not implemented by jsdom | ||
|
|
||
| Avoid mocking the function under test or its private helpers. | ||
|
|
||
| Keep mocks local to the test file unless they are reused across multiple domains. Shared mocks should live under `test/mocks/`. | ||
|
|
||
| ## Coverage expectations | ||
|
|
||
| Coverage should be used as a quality signal, not as a substitute for useful assertions. | ||
|
|
||
| Prioritize meaningful coverage for: | ||
|
|
||
| - core utilities | ||
| - hooks | ||
| - shared UI components | ||
| - service/API boundary logic | ||
| - permission and access-control logic | ||
|
|
||
| Do not add low-value tests just to increase coverage numbers. | ||
|
|
||
| ## Deferred test areas | ||
|
|
||
| Proxy and network-heavy utilities should be tested in a later integration-style pass with explicit mocks for axios, NextAuth, environment config, and Next.js request/response objects. | ||
|
|
||
| Examples: | ||
|
|
||
| - `core/utils/proxyClient.ts` | ||
| - `core/utils/secureApiProxyClient.ts` | ||
| - `core/utils/platform.ts` | ||
|
|
||
| ## PR checklist | ||
|
|
||
| When adding or changing tests, confirm: | ||
|
|
||
| - Tests are co-located with the source file. | ||
| - Test names describe behavior. | ||
| - Tests are deterministic and do not leak timers or storage state. | ||
| - No snapshots were added for utilities. | ||
| - `npm run test` passes. | ||
| - `npx tsc --noEmit` passes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { | ||
| runClientCacheMaintenance, | ||
| shouldEnablePersistentClientCache, | ||
| } from "./clientCache"; | ||
|
|
||
| const VERSION = "2026-04-30.1"; | ||
| const VERSION_KEY = "airqo:vertex:client-cache:version"; | ||
| const QUERY_CACHE_PREFIX = "airqo:vertex:react-query:"; | ||
| const MISMATCH_LOG_KEY = "airqo:vertex:cache-version-mismatch:last"; | ||
|
|
||
| describe("clientCache utilities", () => { | ||
| beforeEach(() => { | ||
| window.localStorage.clear(); | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date("2026-06-28T12:00:00.000Z")); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.restoreAllMocks(); | ||
| window.localStorage.clear(); | ||
| }); | ||
|
|
||
| it("reports that persisted client cache is enabled", () => { | ||
| expect(shouldEnablePersistentClientCache()).toBe(true); | ||
| }); | ||
|
|
||
| it("does nothing when the cache version already matches", () => { | ||
| window.localStorage.setItem(VERSION_KEY, VERSION); | ||
| window.localStorage.setItem(`${QUERY_CACHE_PREFIX}devices`, "cached"); | ||
|
|
||
| runClientCacheMaintenance(); | ||
|
|
||
| expect(window.localStorage.getItem(`${QUERY_CACHE_PREFIX}devices`)).toBe( | ||
| "cached" | ||
| ); | ||
| expect(window.localStorage.getItem(MISMATCH_LOG_KEY)).toBeNull(); | ||
| }); | ||
|
|
||
| it("removes persisted react-query cache entries when the version changes", () => { | ||
| window.localStorage.setItem(VERSION_KEY, "old-version"); | ||
| window.localStorage.setItem(`${QUERY_CACHE_PREFIX}devices`, "remove-me"); | ||
| window.localStorage.setItem(`${QUERY_CACHE_PREFIX}sites`, "remove-me"); | ||
| window.localStorage.setItem("unrelated", "keep-me"); | ||
|
|
||
| runClientCacheMaintenance(); | ||
|
|
||
| expect(window.localStorage.getItem(`${QUERY_CACHE_PREFIX}devices`)).toBeNull(); | ||
| expect(window.localStorage.getItem(`${QUERY_CACHE_PREFIX}sites`)).toBeNull(); | ||
| expect(window.localStorage.getItem("unrelated")).toBe("keep-me"); | ||
| expect(window.localStorage.getItem(VERSION_KEY)).toBe(VERSION); | ||
| expect(window.localStorage.getItem(MISMATCH_LOG_KEY)).toBe( | ||
| "2026-06-28T12:00:00.000Z" | ||
| ); | ||
| }); | ||
|
|
||
| it("sets the cache version on first maintenance run", () => { | ||
| runClientCacheMaintenance(); | ||
|
|
||
| expect(window.localStorage.getItem(VERSION_KEY)).toBe(VERSION); | ||
| }); | ||
|
|
||
| it("tolerates localStorage remove failures while continuing maintenance", () => { | ||
| const fakeLocalStorage = { | ||
| getItem: (key: string) => { | ||
| if (key === VERSION_KEY) return "old-version"; | ||
| return null; | ||
| }, | ||
| setItem: vi.fn(), | ||
| removeItem: vi.fn(() => { | ||
| throw new Error("storage unavailable"); | ||
| }), | ||
| length: 1, | ||
| key: (i: number) => (i === 0 ? `${QUERY_CACHE_PREFIX}devices` : null), | ||
| }; | ||
|
|
||
| const originalLocalStorage = window.localStorage; | ||
| Object.defineProperty(window, "localStorage", { | ||
| value: fakeLocalStorage, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(() => runClientCacheMaintenance()).not.toThrow(); | ||
| expect(fakeLocalStorage.removeItem).toHaveBeenCalledWith( | ||
| `${QUERY_CACHE_PREFIX}devices` | ||
| ); | ||
|
|
||
| Object.defineProperty(window, "localStorage", { | ||
| value: originalLocalStorage, | ||
| configurable: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("tolerates localStorage write failures", () => { | ||
| const fakeLocalStorage = { | ||
| getItem: () => null, | ||
| setItem: vi.fn(() => { | ||
| throw new Error("storage unavailable"); | ||
| }), | ||
| removeItem: vi.fn(), | ||
| length: 0, | ||
| key: () => null, | ||
| }; | ||
|
|
||
| const originalLocalStorage = window.localStorage; | ||
| Object.defineProperty(window, "localStorage", { | ||
| value: fakeLocalStorage, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(() => runClientCacheMaintenance()).not.toThrow(); | ||
| expect(fakeLocalStorage.setItem).toHaveBeenCalledWith(VERSION_KEY, VERSION); | ||
|
|
||
| Object.defineProperty(window, "localStorage", { | ||
| value: originalLocalStorage, | ||
| configurable: true, | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.