Skip to content

fix(@angular/build): apply build conditions to the Vitest runner - #34842

Open
damienwebdev wants to merge 2 commits into
angular:mainfrom
damienwebdev:fix/unit-test-vitest-conditions
Open

damienwebdev wants to merge 2 commits into
angular:mainfrom
damienwebdev:fix/unit-test-vitest-conditions

Conversation

@damienwebdev

@damienwebdev damienwebdev commented Dec 4, 2026 •

Copy link
Copy Markdown

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

The Vitest runner sets resolve.conditions to ['es2015', 'es2020', 'module'], plus browser in browser mode. Packages that Vite resolves at test time ignore the build target's conditions, so tests can run different code than ng build.

Fixes: #34835

What is the new behavior?

The unit-test builder reads conditions from the build target. When set, they replace the default module condition in the runner's resolve.conditions, as they do in the build. Without conditions, the Vitest config is unchanged.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Reproduction: https://github.com/damienwebdev/angular-cli-conditions-repro/tree/vitest. With this change, the test in src/app/app.spec.ts passes.

#33246 forwarded custom conditions in a similar way but was closed after stalling.

The Vitest runner sets its own `resolve.conditions`, so packages resolved by
Vite at test time ignored the build target's `conditions`. Use them in place
of the default `module` condition, as the build does.

Fixes angular#34835

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces support for custom build conditions in the Vitest unit-test runner by extracting the conditions setting from the build target options and passing it to the Vitest configuration plugin. It also adds behavior tests to verify that packages are resolved correctly using these custom conditions. The reviewer suggested defensively filtering the conditions array to ensure it only contains string values instead of relying on a type assertion.

Comment on lines +60 to +62
const conditions = Array.isArray(buildTargetOptions?.conditions)
? (buildTargetOptions.conditions as string[])
: undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To ensure robustness and prevent potential runtime errors (e.g., if the configuration is loaded programmatically or schema validation is bypassed), we should defensively filter the conditions array to ensure it only contains string values instead of relying on a type assertion.

Suggested change
const conditions = Array.isArray(buildTargetOptions?.conditions)
? (buildTargetOptions.conditions as string[])
: undefined;
const conditions = Array.isArray(buildTargetOptions?.conditions)
? buildTargetOptions.conditions.filter((c): c is string => typeof c === 'string')
: undefined;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Applied in e73ffc5. The build target options here aren't schema-validated, so the type check matches how preserveSymlinks is read above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unit-test (vitest) resolves packages without the build target's conditions

1 participant