question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit test should fail if an element is not known

See original GitHub issue

🚀 Unit test should fail if an element is not known

Relevant Package

This feature request is for @angular/core. And I think this is a kinda a regression since this behavior changed with Ivy.

Description

When executing unit tests with Karma and my test module has not been setup correctly, to contain all the relevant components, I get a warning like this:

WARN: ''app-my-element' is not a known element:
1. If 'app-my-element' is an Angular component, then verify that it is part of this module.
2. If 'app-my-element' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'

The test is still successful in that case. In my opinion it should fail, since the test module clearly is missing this component.

I saw that there have been efforts to make it appear as an error: 00f3c58. This has been reverted in this commit: 00f3c58.

But that wouldn’t change the fact, that the test still passes.

Describe the solution you’d like

I would like one of these options:

  • tests with unknown elements fail (as they did pre Ivy)
  • an option to enable this behavior to make the tests fail
    • i.e. a CLI argument or a property in angular.json

If the default is “fail”, then a user could add CUSTOM_ELEMENTS_SCHEMA or NO_ERRORS_SCHEMA to their test module if they want to suppress the errors.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:57
  • Comments:27 (8 by maintainers)

github_iconTop GitHub Comments

19reactions
cexbrayatcommented, May 4, 2022

We landed two PRs that introduce new options for the TestBed in Angular v14 and that should fix this issue.

You can now enable errorOnUnknownElements and/or errorOnUnknownProperties when setting up the TestBed globally or in a specific test to throw an error on unknown elements or unknown properties encountered in templates (instead of just logging the NG303/NG304 error).

These new options are opt-in for now.

If you’re using the CLI, you can enable them globally in test.ts

getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(),
  { 
    errorOnUnknownElements: true,
    errorOnUnknownProperties: true
  }
);

Or you can enable/disable them in a specific test:

TestBed.configureTestingModule({
  declarations: [AppComponent],
  errorOnUnknownElements: true, 
  errorOnUnknownProperties: false
})

See https://next.angular.io/api/core/testing/TestModuleMetadata

18reactions
eegeeZAcommented, Jul 6, 2021

For a workaround, I have added the code below to the end of our src/test.ts file.

// https://github.com/angular/angular/issues/36430
// eslint-disable-next-line @typescript-eslint/no-explicit-any
console.error = (data: any) => fail(data);
Read more comments on GitHub >

github_iconTop Results From Across the Web

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...
Read more >
Unit test error: 'X' is not a known element - Stack Overflow
For a quick unblock, you can try adding schemas: [NO_ERRORS_SCHEMA] to the unit test. This will treat all components that cannot be rendered ......
Read more >
Component testing scenarios - Angular
The Angular testing environment does not know that the test changed the component's title . The ComponentFixtureAutoDetect service responds to asynchronous ...
Read more >
React Testing Library Tutorial – How to Write Unit Tests for ...
It is a very popular React testing library for writing unit tests. ... So if you don't want your test to fail if...
Read more >
Angular unit testing tutorial with examples - LogRocket Blog
Unit testing is the process of testing small, isolated pieces of code. Also known as isolated testing, unit tests do not use external ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found