Visitar URL original
fix(@angular/build): prevent missing NgModule scope errors in unit tests by alan-agius4 · Pull Request #34287 · angular/angular-cli · GitHub
Skip to content

fix(@angular/build): prevent missing NgModule scope errors in unit tests - #34287

Open
alan-agius4 wants to merge 2 commits into
angular:mainfrom
alan-agius4:fix-unit-test-ngmodule-scope
Open

alan-agius4 wants to merge 2 commits into
angular:mainfrom
alan-agius4:fix-unit-test-ngmodule-scope

Conversation

@alan-agius4

Copy link
Copy Markdown
Collaborator

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 #34231

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
@alan-agius4
alan-agius4 requested a review from clydin October 8, 2026 11:11
@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Oct 8, 2026

@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 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);

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

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);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 4155071.

@alan-agius4 alan-agius4 added target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/build target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ng test fails with NG8001 in a new NgModule app on 22.2.0

1 participant