Repository navigation
Description
🚀 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.
Activity
[-]Unit test should have option to fail if an element is not known[/-][+]Unit test should fail if an element is not known[/+]x87 commentedon Apr 6, 2020
This is probably related to #36171
danmana commentedon Apr 9, 2020
This has been driving me nuts for a few weeks.
On my local machine, tests with unknown elements pass without even showing a warning.
On the build server, the tests fail.
Other types of failing tests behave the same on both machines (wrong expect statements fail on both)
Test command:
npm run test -- --watch false --progress falseMy machine:
Windows 10 Pro, version 10.0.18363 Build 18363
Build server:
Gitlab-CI using image
circleci/node:12-browsersI even tried running the tests locally in the same docker container that the build server uses, but to no avail, it still passes locally but fails once I push it to gitlab.
Locally it's behaving like I have some global setting of
schemas: [NO_ERRORS_SCHEMA]...sebastianhaeni commentedon Apr 22, 2020
In case anybody is interested, I have the following workaround in place until this is fixed:
Executing the following function before creating the component with
TestBed.createComponent()will ensure tests fail if there are unknown elements.Since we use a util function in every component test to setup our test bed, it was easy to add there.
20 remaining items
distante commentedon Sep 1, 2021
Monkey patching
console.errordoes not work always. No idea why.gagle commentedon Oct 5, 2021
This is really an inconvenient. Having errors in console with unit tests passing is not good.
I use this snippet:
Stored in
/setup-jest.tsand loaded from jest config usingsetupFilesproperty.KissBalazs commentedon Oct 21, 2021
It would be nice if this feature could be a flag option to fail the test run upon any
console.error. This way compatibility wouldn't be hurt.petebacondarwin commentedon Oct 22, 2021
We think that this could be changed to throw an error now, but it would be a breaking change that could land in 14.0.0 at the earliest. For now we will put it in the backlog, and run a test in Google to see how breaking it would be. If it turns out this is a big effort to merge into Google, when we will need to prioritize it as a project.
kevinmerckx commentedon Jan 4, 2022
Isn't it a bug that is a breaking change introduced recently? Therefore, fixing this issue shouldn't be considered a breaking change, but rather a fixing change.
fix(core): throw an "unknown element" error instead of logging a cons…
cexbrayat commentedon May 4, 2022
We landed two PRs that introduce new options for the
TestBedin Angular v14 and that should fix this issue.You can now enable
errorOnUnknownElementsand/orerrorOnUnknownPropertieswhen setting up theTestBedglobally 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.tsOr you can enable/disable them in a specific test:
See https://next.angular.io/api/core/testing/TestModuleMetadata
angular-automatic-lock-bot commentedon Jun 4, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.