Skip to content

Commit 3bf81dd

Browse files
chiptusclaude
andauthored
feat(ci): add unit tests (#24)
* test: add unit tests for voteConfig and stages (Phase 1) Add comprehensive unit tests for: - src/lib/voteConfig.ts: 20 tests covering VOTE_CONFIG structure, getVoteConfig, and getVoteValue functions - src/lib/constants/stages.ts: 6 tests for DEFAULT_STAGE_COLOR constant All tests passing (50/50). Phase 1 of the unit testing plan is now complete. * test: add unit tests for UI components (Phase 2) Set up React Testing Library and add comprehensive unit tests for: - Component tests (87 new tests): - GenreBadge.tsx: 9 tests with mocked useGenres hook - StageBadge.tsx: 11 tests for rendering and styling - StagePin.tsx: 9 tests with mocked useStageQuery hook - ui/badge.tsx: 10 tests for variants and props - ui/button.tsx: 20 tests for variants, sizes, and interactions - ui/card.tsx: 28 tests for all card components Infrastructure: - Added @testing-library/react, @testing-library/jest-dom, @testing-library/user-event - Created test setup file (src/test/setup.ts) for jest-dom matchers - Updated vite.config.test.ts to include setupFiles Total: 137 tests passing (Phase 1 + Phase 2) * test: add unit tests for custom hooks (Phase 3) Add comprehensive unit tests for custom hooks: - use-mobile.tsx: 10 tests for responsive breakpoint detection - useOnlineStatus.ts: 10 tests for online/offline status tracking - useScrollVisibility.ts: 14 tests for IntersectionObserver wrapper - use-toast.ts: 17 tests for toast state management with reducer - useCookieConsent.ts: 16 tests for GDPR consent management All hooks tests include: - State initialization - Event listener setup and cleanup - State updates and transitions - Edge cases and error handling - Mock implementations for browser APIs Total: 204 tests passing (Phases 1-3 combined) * test: add unit tests for core utilities (Phase 4) Add comprehensive unit tests for core utility functions: - utils.ts: 12 tests for cn() function (class name merging with Tailwind) - stageUtils.ts: 11 tests for sortStagesByOrder() sorting algorithm - markdown.ts: 21 tests for parseMarkdown() with GFM support - timeUtils.ts: 44 tests for time formatting and timezone conversion functions - formatTimeRange, formatDateTime, formatTimeOnly - toDatetimeLocal, toISOString - combineDateAndTime, convertLocalTimeToUTC All utilities tests include: - Edge cases and null/undefined handling - Format validation - Timezone conversions - Complex data scenarios Total: 292 tests passing (Phases 1-4 combined) * ci: add unit tests workflow Add GitHub Actions workflow to run Vitest unit tests: - Runs on push to main/develop and on PRs - Executes all 292 unit tests - Generates coverage report on PRs - Uploads coverage artifacts for review This provides fast feedback (~20s) compared to E2E tests. * fix: resolve vitest webidl-conversions errors Fix unhandled errors with webidl-conversions and whatwg-url: - Add pool: "forks" to vitest config for better process isolation - Add global polyfills for Set, Map, WeakMap, WeakSet in test setup This resolves the "Cannot read properties of undefined" errors that were occurring during module loading in the test environment. All 292 tests now pass successfully. * fix: add globalSetup to fix webidl-conversions errors Add globalSetup file to polyfill ArrayBuffer and SharedArrayBuffer properties before any test modules are loaded. This prevents the webidl-conversions package from throwing errors during module import. Changes: - Created vitest.global-setup.ts with early polyfills - Updated vite.config.test.ts to use globalSetup - Enhanced src/test/setup.ts with additional polyfills All 292 tests now pass without unhandled errors. * fix: resolve TypeScript and oxlint errors in test files - Fixed timeUtils test for cross-day time ranges by using times farther apart to ensure they span different days regardless of timezone conversion - Fixed unused variable in useScrollVisibility test - Replaced any types with proper TypeScript types using ReturnType utility - Added optional chaining for potentially undefined function calls in use-toast test - Initialized variables with proper types instead of undefined in use-toast test - Fixed update method call to include required id property in use-toast test - Added null coalescing for querySelector result in card test - Added type annotations and assertions for nullable stage_order values in stageUtils tests * fix: move webidl-conversions polyfill to config file top Apply ArrayBuffer/SharedArrayBuffer polyfills at the very top of the vite.config.test.ts file, before any imports. This ensures the polyfills are applied before webidl-conversions is loaded by any dependencies. The previous globalSetup approach didn't work in CI because globalSetup runs in a separate context after modules are already loaded. Moving the polyfill to the config file ensures it runs early enough to prevent 'Cannot read properties of undefined (reading get)' errors in Node 18. * ci: upgrade Node.js version from 18 to 22 Update CI workflows to use Node.js 22 to match local development environment. This provides: - Better compatibility with modern dependencies - Native support for newer JavaScript features - No need for webidl-conversions polyfills - Consistent behavior between local and CI environments * chore: add vite coverage * chore: upgrade vite deps * fix * move test files * chore: update vite deps * fix: remove ref to vite.config.test --------- Co-authored-by: Claude <[email protected]>
1 parent 1e628da commit 3bf81dd

26 files changed

+3231
-494
lines changed

.github/workflows/e2e-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424

2525
- uses: actions/setup-node@v4
2626
with:
27-
node-version: 18
27+
node-version: 22
2828
cache: "pnpm"
2929

3030
- name: Install dependencies
@@ -65,7 +65,7 @@ jobs:
6565

6666
- uses: actions/setup-node@v4
6767
with:
68-
node-version: 18
68+
node-version: 22
6969
cache: "pnpm"
7070

7171
- name: Install dependencies

.github/workflows/unit-tests.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
branches: [main, develop]
6+
pull_request:
7+
branches: [main, develop]
8+
9+
jobs:
10+
test:
11+
name: Run Unit Tests
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 10
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- uses: pnpm/action-setup@v4
19+
20+
- uses: actions/setup-node@v4
21+
with:
22+
node-version: 22
23+
cache: "pnpm"
24+
25+
- name: Install dependencies
26+
run: pnpm install --frozen-lockfile
27+
28+
- name: Run unit tests
29+
run: pnpm test
30+
31+
- name: Run unit tests with coverage
32+
run: pnpm run test:coverage
33+
if: github.event_name == 'pull_request'
34+
35+
- name: Upload coverage reports
36+
uses: actions/upload-artifact@v4
37+
if: github.event_name == 'pull_request'
38+
with:
39+
name: coverage-report
40+
path: coverage/
41+
retention-days: 7

package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"format": "prettier --write .",
1313
"format:check": "prettier --check .",
1414
"preview": "vite preview",
15-
"test": "vitest --config vite.config.test.ts",
16-
"test:ui": "vitest --ui --config vite.config.test.ts",
17-
"test:coverage": "vitest run --coverage --config vite.config.test.ts",
15+
"test": "vitest",
16+
"test:ui": "vitest --ui",
17+
"test:coverage": "vitest run --coverage",
1818
"test:e2e": "playwright test",
1919
"test:e2e:ui": "playwright test --ui",
2020
"test:e2e:headed": "playwright test --headed",
@@ -91,22 +91,27 @@
9191
"tailwind-merge": "^2.5.2",
9292
"tailwindcss-animate": "^1.0.7",
9393
"vaul": "^0.9.3",
94-
"vite-plugin-pwa": "^1.0.1",
9594
"workbox-background-sync": "^7.3.0",
9695
"workbox-precaching": "^7.3.0",
9796
"workbox-routing": "^7.3.0",
9897
"workbox-strategies": "^7.3.0",
9998
"zod": "^3.23.8"
10099
},
101100
"devDependencies": {
101+
"vite-plugin-pwa": "^1.2.0",
102102
"@playwright/test": "^1.54.1",
103103
"@tailwindcss/typography": "^0.5.15",
104+
"@testing-library/jest-dom": "^6.9.1",
105+
"@testing-library/react": "^16.3.0",
106+
"@testing-library/user-event": "^14.6.1",
104107
"@types/node": "^22.5.5",
105108
"@types/react": "^18.3.3",
106109
"@types/react-dom": "^18.3.0",
107-
"@vitejs/plugin-react-swc": "^3.5.0",
108-
"@vitest/ui": "^3.2.4",
110+
"@vitejs/plugin-react-swc": "^4.2.2",
111+
"@vitest/coverage-v8": "^4.0.15",
112+
"@vitest/ui": "^4.0.15",
109113
"autoprefixer": "^10.4.20",
114+
"baseline-browser-mapping": "^2.9.5",
110115
"globals": "^15.9.0",
111116
"husky": "^9.1.7",
112117
"jsdom": "^27.0.0",
@@ -118,8 +123,8 @@
118123
"tailwindcss": "^3.4.11",
119124
"tsx": "^4.20.3",
120125
"typescript": "^5.5.3",
121-
"vite": "^5.4.1",
122-
"vitest": "^3.2.4"
126+
"vite": "^7.2.7",
127+
"vitest": "^4.0.15"
123128
},
124129
"lint-staged": {
125130
"*.{js,jsx,ts,tsx}": [

0 commit comments

Comments
 (0)