Skip to content

Conversation

@clydin
Copy link
Member

@clydin clydin commented Nov 7, 2025

This change enhances the Vitest unit test builder to intelligently select the default DOM test environment (jsdom or happy-dom) based on which package is installed in the user's project.

Previously, the builder strictly defaulted to jsdom. With this update:

  • The validateDependencies function now checks for the presence of either jsdom or happy-dom when browser-based tests are not configured, providing a more flexible dependency requirement.
  • A new findTestEnvironment helper function is introduced to detect the available DOM environment (happy-dom is preferred if installed, otherwise jsdom).
  • The createVitestConfigPlugin now uses this helper to dynamically set the environment in the Vitest configuration, ensuring a smart default if the user has not explicitly specified one.

This improves the out-of-the-box experience by adapting to common project setups and offering more choice in DOM emulation.

NOTE: For advanced scenarios, a custom runner config can be used to customize the environment configuration.

@clydin clydin added the target: rc This PR is targeted for the next release-candidate label Nov 7, 2025
This change enhances the Vitest unit test builder to intelligently select the default DOM test environment (`jsdom` or `happy-dom`) based on which package is installed in the user's project.

Previously, the builder strictly defaulted to `jsdom`. With this update:
- The `validateDependencies` function now checks for the presence of either `jsdom` or `happy-dom` when browser-based tests are not configured, providing a more flexible dependency requirement.
- A new `findTestEnvironment` helper function is introduced to detect the available DOM environment (`happy-dom` is preferred if installed, otherwise `jsdom`).
- The `createVitestConfigPlugin` now uses this helper to dynamically set the `environment` in the Vitest configuration, ensuring a smart default if the user has not explicitly specified one.

This improves the out-of-the-box experience by adapting to common project setups and offering more choice in DOM emulation.
@clydin clydin force-pushed the unit-test/vitest-environment-auto branch from 0bba993 to b1ce21d Compare November 8, 2025 01:50
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Nov 8, 2025
@alan-agius4 alan-agius4 linked an issue Nov 8, 2025 that may be closed by this pull request
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 8, 2025
@alan-agius4 alan-agius4 merged commit 53bdbf1 into angular:main Nov 8, 2025
33 checks passed
@alan-agius4
Copy link
Collaborator

This PR was merged into the repository. The changes were merged into the following branches:

@clydin clydin deleted the unit-test/vitest-environment-auto branch November 8, 2025 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: rc This PR is targeted for the next release-candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add happy-dom option to unit-test runner when using Vitest

2 participants