Repository navigation
fix(@angular/build): prevent missing NgModule scope errors in unit tests - #34287
alan-agius4 wants to merge 2 commits into
Conversation
Previously, `rootFiles` was used to restrict TypeScript compilation root names to only the test files to be executed. However, when an application `tsconfig.spec.json` includes application source files (standard for NgModule-based applications), non-standalone components rely on `AppModule` being present in the TypeScript program's root names for the Angular compiler to determine their compilation scope. Restricting root names caused template diagnostic errors such as `NG8001: '<element>' is not a known element`. This change replaces the root file allowlisting in the unit-test runner with an `excludeRootFiles` option that excludes unselected test files from the compilation roots. This ensures application files and NgModule definitions remain in the program while still ignoring unselected test files with compilation errors. Closes angular#34231
There was a problem hiding this comment.
Code Review
This pull request replaces the rootFiles option with excludeRootFiles across the Angular build pipeline to exclude unselected test files from the TypeScript compilation root names rather than restricting them. It also adds unit tests to verify that NgModule declared components compile correctly when using include or when tsConfig includes application files. Feedback suggests optimizing getVitestBuildOptions by avoiding a redundant and potentially expensive second glob scan of the workspace when running the full test suite with default test patterns and no exclusions.
| } | ||
| } | ||
|
|
||
| const allTestFiles = await findTests(DEFAULT_TEST_PATTERNS, [], workspaceRoot, projectSourceRoot); |
There was a problem hiding this comment.
When running the full test suite with the default test patterns and no exclusions, testFiles already contains all test files. We can optimize this by avoiding a redundant and potentially expensive second glob scan of the workspace.
const isDefaultInclude =
include.length === DEFAULT_TEST_PATTERNS.length &&
include.every((val) => DEFAULT_TEST_PATTERNS.includes(val)) &&
exclude.length === 0;
const allTestFiles = isDefaultInclude
? testFiles
: await findTests(DEFAULT_TEST_PATTERNS, [], workspaceRoot, projectSourceRoot);
Previously,
rootFileswas used to restrict TypeScript compilation root names to only the test files to be executed. However, when an applicationtsconfig.spec.jsonincludes application source files (standard for NgModule-based applications), non-standalone components rely onAppModulebeing present in the TypeScript program's root names for the Angular compiler to determine their compilation scope. Restricting root names caused template diagnostic errors such asNG8001: '<element>' is not a known element.This change replaces the root file allowlisting in the unit-test runner with an
excludeRootFilesoption that excludes unselected test files from the compilation roots. This ensures application files and NgModule definitions remain in the program while still ignoring unselected test files with compilation errors.Closes #34231