Skip to content

Remove ViewChild from metadata#8771

Open
c-harding wants to merge 4 commits into
help-me-mom:mainfrom
c-harding:fix-GH-8634
Open

Remove ViewChild from metadata#8771
c-harding wants to merge 4 commits into
help-me-mom:mainfrom
c-harding:fix-GH-8634

Conversation

@c-harding
Copy link
Copy Markdown
Contributor

ViewChild will always be null, and so there is no point in mocking it.
viewChild.required throws an error in mocked components without this
change.

Fixes GH-8634

ViewChild will always be null, and so there is no point in mocking it.
viewChild.required throws an error in mocked components without this
change.

Fixes help-me-momGH-8634
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2024

Codecov Report

❌ Patch coverage is 57.14286% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.93%. Comparing base (0c3837a) to head (995a2ca).
⚠️ Report is 5866 commits behind head on main.

Files with missing lines Patch % Lines
...src/lib/mock-component/render/generate-template.ts 33.33% 1 Missing and 1 partial ⚠️
libs/ng-mocks/src/lib/common/decorate.queries.ts 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main    #8771      +/-   ##
===========================================
- Coverage   100.00%   99.93%   -0.07%     
===========================================
  Files          227      227              
  Lines         4935     4938       +3     
  Branches      1147     1149       +2     
===========================================
  Hits          4935     4935              
- Misses           0        1       +1     
- Partials         0        2       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@satanTime satanTime left a comment

Choose a reason for hiding this comment

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

Hi there.

Thank you for contribution, but it's a breaking change which affects other angular versions. Should be fixed without breaking anything.

@c-harding
Copy link
Copy Markdown
Contributor Author

Hi @satanTime, what’s the effect for other versions? Only __prop_metadata__ is affected, which is not a documented property. Additionally, viewChild would always be falsy for mocked components anyway, because mocked components do not have any of their own elements as children.

@satanTime
Copy link
Copy Markdown
Member

ng-mocks uses many undocumented and monkey patched things.

For a proper PR, you need to provide a proper test which demonstrates the issue and its fix.

Here, I see that mocks which provide own empty views now will return nulls instead of objects, so tests which relied on these empty views will fail despite no change in the code, only due to the update of ng-mocks.

That's what should be avoided, so what worked before should still work afterwards.

@dmitry-stepanenko
Copy link
Copy Markdown

dmitry-stepanenko commented May 24, 2024

@satanTime signal-based queries have isSignal: true property set. WDYT if we would modify the condition as follows

diff --git a/libs/ng-mocks/src/lib/common/decorate.queries.ts b/libs/ng-mocks/src/lib/common/decorate.queries.ts
index 860983934..f69eb1423 100644
--- a/libs/ng-mocks/src/lib/common/decorate.queries.ts
+++ b/libs/ng-mocks/src/lib/common/decorate.queries.ts
@@ -26,8 +26,11 @@ const generateFinalQueries = (queries: {
   const scanKeys: string[] = [];
 
   for (const key of Object.keys(queries)) {
-    const query: Query & { ngMetadataName?: string } = queries[key];
-    final.push([key, query]);
+    const query: Query & { ngMetadataName?: string; isSignal?: boolean } = queries[key];
+    const isSignalBasedQuery = query.isViewQuery && query.isSignal;
+    if (!isSignalBasedQuery) {
+      final.push([key, query]);
+    }
 
     if (!query.isViewQuery && !isInternalKey(key)) {
       scanKeys.push(key);

That way it should not be considered as a regression for any existing cases while still allowing us to move forward

@c-harding
Copy link
Copy Markdown
Contributor Author

@dmitry-stepanenko that sounds promising! I’ve given you write access to this repo, so you’re welcome to edit the PR directly

@dmitry-stepanenko
Copy link
Copy Markdown

awesome, thanks @c-harding. I'll update the branch shortly

@c-harding
Copy link
Copy Markdown
Contributor Author

@dmitry-stepanenko Thanks for fixing this! It looks like the coverage is still a little low (I can’t tell why, you have written a test that should cover this).

@kheos31
Copy link
Copy Markdown

kheos31 commented Feb 6, 2025

Hello! @satanTime does the fix provided by @dmitry-stepanenko in this PR and as he explained in this post #8771 (comment) is what you expected as a good fix?
Otherwise, how could we help to do this PR "acceptable"?
Thanks in advance

@gestj
Copy link
Copy Markdown

gestj commented Feb 20, 2025

Bump ... Sad this is still pending... known issue since now almost a year :/

@satanTime satanTime force-pushed the master branch 7 times, most recently from 6824c13 to 6f17ef1 Compare May 5, 2025 07:19
@Francesco-Borzi
Copy link
Copy Markdown

any news?

@Watercycle
Copy link
Copy Markdown
Contributor

For those who really want to use signals right now or are facing the struggles of mixed code, here's a not-fully-vetted patch containing this branch and #11909:

"ng-mocks": "https://github.com/Watercycle/ng-mocks/releases/download/v14.14.0/ng-mocks-0.0.1.tgz",

I think the next steps for this branch are... re-merging master? It seems like the tests didn't run last time (CircleCI). I'd be surprised if anything significant came up, though.

@Watercycle
Copy link
Copy Markdown
Contributor

I've duplicated this PR over to #12875 with some minor changes to (artificially) make CI happy. I think ideally tests-e2e would also report coverage and be merged with the existing tests coverage to simplify things for future contributions; but, that's a separate adventure and kinda pending the question of whether it's worth the effort of having every release maintain compatibility across every Angular version.

@satanTime satanTime changed the base branch from master to main March 6, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Incompatibility of MockComponent with new viewChild signal function

7 participants