Angular logs the “not a known element” error as a warning

In Angular 9 and 10 we can notice that the “my-element is not a known element” error is missing when our tests don’t have all required stubs. Make sure to check debug messages when running tests and add all absent stubs. Otherwise, you will have to update your test suite when the Angular team fixes this bug.

I noticed the issue that I describe here when working with the following Angular version:

Consequences of missing error when the nested element is not known

It seems that since version 9 Angular handles unknown elements in unit tests differently than the previous releases. If we forget to stub an element that we used inside a component under test, the test will pass. Furthermore, the message about an unknown element will be displayed only in the debug window.

The guest-layout.component.spec file is an example test file:

As we can see, I created stubs for the GuestNavComponent, PageContentComponent and GuestFooterComponent and declared them in the TestBed configuration. If I run the $ ng test command now, all tests pass.

If I was to remove the PageContentComponent stub declaration from the test and run it again, the outcome would still be positive. We won’t see any errors in the test result. Instead, we have to debug to find the app-page-content' is not a known element – the error is missing from the summary, as we can see on the screenshot below:

element is not a known element error missing from test output, visible only in debug

Consequently, you don’t know for sure whether you stubbed all nested components or not until you check debug messages.

There are multiple issues on GitHub that address this problem. One of them has been recently labeled as High priority issue that should be resolved as soon as possible. However, for now, we have to manually verify debug messages to find missing nested component declarations. If you commit code without fixing the warnings, you risk a lot of broken tests once you update your project to the Angular version that reintroduces the error.

How to disable “not a known element error” permanently

On the other hand, if you added the NO_ERRORS_SCHEMA to the TestBed.schemas, Angular compiler already ignores unrecognized elements and attributes. This means that you don’t have to stub elements. In this case, you won’t get any hints if you forget to declare a nested component even after the Angular team fixes the bug. Nevertheless, don’t forget about consequences of this approach:

The NO_ERRORS_SCHEMA also prevents the compiler from telling you about the missing components and attributes that you omitted inadvertently or misspelled. You could waste hours chasing phantom bugs that the compiler would have caught in an instant.

https://angular.io/guide/testing-components-scenarios#use-both-techniques-together

More on testing components with nested elements in Angular

Photo by Ba Phi from Pexels

Leave a Reply

Your email address will not be published.