Skip to content

Add comprehensive unit tests with Vitest and HappyDom#502

Draft
Copilot wants to merge 6 commits intomasterfrom
copilot/fix-501
Draft

Add comprehensive unit tests with Vitest and HappyDom#502
Copilot wants to merge 6 commits intomasterfrom
copilot/fix-501

Conversation

Copy link
Copy Markdown

Copilot AI commented Jul 21, 2025

This PR implements comprehensive unit testing for the flood-it game using Vitest and HappyDom as requested in the issue.

Changes Made

🧪 Test Infrastructure

  • Added Vitest: Modern test runner with excellent TypeScript and ES modules support
  • Added HappyDom: Fast DOM environment for component testing
  • Added Testing Library: @testing-library/preact for component testing utilities
  • Configured Vitest: Updated vite.config.ts with test configuration and setup files

🔧 Code Refactoring for Testability

  • Extracted utility functions from src/app.tsx to src/utils.ts:
    • createSeed(width, height) - generates random game seeds
    • parseSeed(seed) - parses and validates/fixes seed strings
    • flood(x, y, target, replacement, grid) - flood fill algorithm
    • colors - game color constants
  • Updated imports in src/app.tsx to use the new utils module

📋 Test Coverage (23 tests total)

src/utils.test.ts (15 tests)

  • ✅ Color array validation
  • ✅ Seed generation with different grid sizes
  • ✅ Seed parsing with valid/invalid formats
  • ✅ Seed auto-fixing for too short/long sequences
  • ✅ Flood fill algorithm with various scenarios:
    • Simple 2x2 grids
    • Connected vs disconnected regions
    • Edge cases (out of bounds, empty grids)
    • Target equals replacement color

src/main.test.tsx (2 tests)

  • ✅ Module import validation
  • ✅ Mock dependency verification

src/app.test.tsx (6 tests)

  • ✅ Component rendering (title, buttons, moves counter)
  • ✅ Game state display (seed, grid cells)
  • ✅ Initial button states (restart disabled)

🛠️ Updated Scripts

  • Updated package.json test script from placeholder to vitest run
  • All existing build and lint processes remain functional

Verification

npm test        # ✅ 23 tests pass
npm run build   # ✅ Builds successfully  
npm run lint    # ✅ Passes TypeScript and oxlint checks

The implementation provides a solid foundation for testing game logic and component behavior while maintaining the existing functionality unchanged.

Fixes #501.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits July 21, 2025 07:49
Co-authored-by: Shuunen <439158+Shuunen@users.noreply.github.com>
Co-authored-by: Shuunen <439158+Shuunen@users.noreply.github.com>
Copilot AI changed the title [WIP] Cover source files with unit tests Add comprehensive unit tests with Vitest and HappyDom Jul 21, 2025
Copilot AI requested a review from Shuunen July 21, 2025 08:07
Comment thread package.json Outdated
Comment thread src/main.test.tsx Outdated
Comment thread src/utils.test.ts Outdated
Comment thread vite.config.ts
Copilot AI and others added 2 commits July 21, 2025 11:48
Co-authored-by: Shuunen <439158+Shuunen@users.noreply.github.com>
Co-authored-by: Shuunen <439158+Shuunen@users.noreply.github.com>
Copilot AI requested a review from Shuunen July 21, 2025 11:58
Copy link
Copy Markdown
Owner

@Shuunen Shuunen left a comment

Choose a reason for hiding this comment

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

please ignore this dep "@vitest/coverage-v8" in knip config

Co-authored-by: Shuunen <439158+Shuunen@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Jul 21, 2025

please ignore this dep "@vitest/coverage-v8" in knip config

Added @vitest/coverage-v8 to knip ignored dependencies in a2d4cd6

Copilot AI requested a review from Shuunen July 21, 2025 12:10
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.

Cover source files with unit tests

2 participants