Repository navigation
fix(@angular/build): apply build conditions to the Vitest runner - #34842
damienwebdev wants to merge 2 commits into
Conversation
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
There was a problem hiding this comment.
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.
| const conditions = Array.isArray(buildTargetOptions?.conditions) | ||
| ? (buildTargetOptions.conditions as string[]) | ||
| : undefined; |
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
Applied in e73ffc5. The build target options here aren't schema-validated, so the type check matches how preserveSymlinks is read above.
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
The Vitest runner sets
resolve.conditionsto['es2015', 'es2020', 'module'], plusbrowserin browser mode. Packages that Vite resolves at test time ignore the build target'sconditions, so tests can run different code thanng build.Fixes: #34835
What is the new behavior?
The unit-test builder reads
conditionsfrom the build target. When set, they replace the defaultmodulecondition in the runner'sresolve.conditions, as they do in the build. Withoutconditions, the Vitest config is unchanged.Does this PR introduce a breaking change?
Other information
Reproduction: https://github.com/damienwebdev/angular-cli-conditions-repro/tree/vitest. With this change, the test in
src/app/app.spec.tspasses.#33246 forwarded custom conditions in a similar way but was closed after stalling.