Skip to content

fix(@angular/build): resolve unit-test include patterns from the project root - #34203

Closed
thekhegay wants to merge 1 commit into
angular:mainfrom
thekhegay:fix/unit-test-include-project-root
Closed

thekhegay wants to merge 1 commit into
angular:mainfrom
thekhegay:fix/unit-test-include-project-root

Conversation

@thekhegay

Copy link
Copy Markdown
Contributor

PR Checklist

  • The commit message follows our guidelines
  • Tests for the changes have been added
  • Docs have been added / updated

PR Type

  • Bugfix

What is the current behavior?

Issue Number: #33467

Schema says include is relative to project root. Code resolves it from source root. So for a library with secondary entry point next to src nothing reaches it - my-sub-package/**/*.spec.ts, ./my-sub-package/**/*.spec.ts, my-sub-package/my-sub.spec.ts, projects/my-lib/my-sub-package/**/*.spec.ts all find zero files and run still says success. Happens with no config too, ng generate library writes no include.

What is the new behavior?

Pattern that matched nothing under source root gets retried from project root. Pattern that already matched is not retried, so **/*.spec.ts stays where it is and does not start picking up specs outside src. Project rooted at workspace root is never retried, that would search every project.

Checked on generated workspace with library, secondary entry point and an e2e/ spec. Four spellings above fixed plus reporter's ["**/*.spec.ts", "my-sub-package/**/*.spec.ts"]. Six spellings that work today return same files. e2e/ spec not picked up before or after.

Cost is globbing per pattern instead of batching, needed to tell unmatched from matched. On library with 302 specs medians are 451ms before and 447ms after, spread between rounds is wider than that.

builders/karma/find-tests.ts untouched, it calls through without new argument so karma keeps current behavior.

Three of five new cases fail without the change. Other two pin what must not move.

Does this PR introduce a breaking change?

  • Yes
  • No

@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 updates the unit test builder to retry finding test files from the project root if they are not found under the project source root, aligning with the builder's schema. The findTests function is refactored to use a helper collectTests to perform this search. Feedback on the changes points out a redundant ternary check when calling glob, as dynamicPatterns is guaranteed to be non-empty at that point.

Comment thread packages/angular/build/src/builders/unit-test/test-discovery.ts Outdated
…ect root

The `include` option is documented by the builder schema as relative to the
project root, but patterns were only ever resolved from the project source
root. A library that keeps a secondary entry point beside `src` had no
supported spelling that reached its specs, and the run still reported success.

A pattern that matches nothing under the source root is now retried from the
project root. A pattern that already matches is not retried, so an existing
pattern keeps matching exactly what it matches today. A project rooted at the
workspace root is never retried, since that would search every project.

Closes angular#33467
@thekhegay
thekhegay force-pushed the fix/unit-test-include-project-root branch from 689f7b7 to 580323c Compare September 29, 2026 13:57
@clydin

clydin commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Thank you for the contribution.
However, in this case the schema documentation is actually incorrect. The project source root is the expected base path for include and exclude.

For existing library projects, using parent traversal is the recommended solution:

"include": ["**/*.spec.ts", "../my-sub-package/**/*.spec.ts"]

However, for the in-development new library build system. The path constraints for secondary entrypoints will no longer be present. This will allow all source files to reside within the actual project source root in the future.

@clydin clydin closed this Sep 29, 2026
@thekhegay

Copy link
Copy Markdown
Contributor Author

@clydin thanks, that is clear. Should have looked for that answer before writing code.

Want me to send small PR fixing two descriptions in unit-test/schema.json? Both include and exclude say "relative to the project root"

@clydin

clydin commented Sep 29, 2026

Copy link
Copy Markdown
Member

Already done and merged but thank you for the offer.

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.

2 participants