Skip to content

test: set up component tests with happy-dom and Testing Library - #227

Merged
anurag629 merged 1 commit into
codercops:developfrom
Bl1ckxy:feature/component-test-setup
Oct 21, 2026
Merged

anurag629 merged 1 commit into
codercops:developfrom
Bl1ckxy:feature/component-test-setup

Conversation

@Bl1ckxy

@Bl1ckxy Bl1ckxy commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #206

What and why

Sets up component testing infrastructure so future component fixes (like the invoice reset in #201 or the hydration error in #193) can ship with tests that fail before the fix.

Previously, vitest.config.ts ran all tests in the node environment and only picked up lib/**/*.test.ts. There was no DOM library or Testing Library in package.json, so component bugs could not be caught by tests.

This PR adds happy-dom and @testing-library/react as dev dependencies, configures Vitest 4 test.projects to split environments, and includes a proof-of-concept test for CopyButton.

Changes

  • package.json: Added happy-dom, @testing-library/react, @testing-library/dom as devDependencies
  • vitest.config.ts: Replaced flat test.environment/test.include with test.projects array:
    • lib project → node environment, lib/**/*.test.ts (unchanged behavior)
    • components project → happy-dom environment, components/**/*.test.tsx + app/**/*.test.tsx
    • Added resolve.alias to each project (Vitest 4 projects don't inherit root config)
  • vitest.setup.ts (new): Imports @testing-library/jest-dom/vitest for DOM matchers
  • components/shared/CopyButton.test.tsx (new): Tests that clicking the button calls navigator.clipboard.writeText with the correct value and the label changes to "Copied"

How I tested this

  • npm run test → 18 test files, 444 tests, all passing (443 lib + 1 component)
  • Verified lib tests still run in node environment
  • Verified CopyButton test fails when navigator.clipboard.writeText is removed from the component
  • npm run lint → passes
  • npm run build → passes

AI assistance

Used an AI assistant (Claude) to research Vitest 4 test.projects environment splitting, debug why happy-dom's navigator.clipboard can't be overridden with Object.assign, and draft the CopyButton component test. Every line of code in this PR was written and verified by me — npm run test passes 444/444, npm run lint and npm run build pass.

Type of change

  • Bug fix
  • New tool or feature
  • Tests
  • Refactor or cleanup
  • Docs

Checklist

  • I was assigned the linked issue
  • npm run lint && npm run test && npm run build passes locally
  • Logic changes live in lib/ and have a test in lib/tests/
  • For a bug fix: the new test fails without my change
  • No analytics, trackers, or calls that send user data off-device
  • Works in both light and dark themes (if UI changed)
  • Inputs have labels and it works with the keyboard alone (if UI changed)
  • New tool: registry entry, page.tsx, client component, opengraph-image.tsx, and the bug report dropdown are all done

Notes for reviewers

  • The resolve.alias duplication in each project is required because Vitest 4 test.projects override rather than inherit the root config. Without it, @/lib/... imports fail in both projects.
  • The clipboard mock uses Object.defineProperty instead of Object.assign because happy-dom defines navigator.clipboard as a getter-only property.
  • CONTRIBUTING.md already documents the component test location (line added previously), so no doc changes were needed.

Copilot AI balanced review requested due to automatic review settings October 20, 2026 21:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@Bl1ckxy Bl1ckxy closed this Oct 20, 2026
@Bl1ckxy Bl1ckxy reopened this Oct 20, 2026

@anurag629 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @Bl1ckxy, this is what #206 asked for. The lib/ tests still run in node, the components project picks up CopyButton.test.tsx in happy-dom, and the test fails when I replace the writeText call. The lockfile only adds the new packages. Lint, all 444 tests, the build and the page check pass on top of develop.

It's your first merged PR here, so come say hi on our Discord too: https://discord.gg/mpGdJyjaRQ

@anurag629 anurag629 added the hacktoberfest-accepted PR counts for Hacktoberfest label Oct 21, 2026
@anurag629
anurag629 merged commit f8600dd into codercops:develop Oct 21, 2026
14 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest-accepted PR counts for Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set up component tests with happy-dom and Testing Library

3 participants