DebugElement.componentInstance returning the wrong component
See original GitHub issueπ bug report
Affected Package
N/A
Is this a regression?
N/A
Description
DebugElement.componentInstance
is returning the wrong component.
π¬ Minimal Reproduction
Create a new app.
ng new test-app --defaults
Create two components.
ng g c pet-list
ng g c pet-display
In src/app/pet-list/pet-list.component.html
use pet display component.
<app-pet-display></app-pet-display>
In src/app/app.component.html
use pet list component.
<app-pet-list></app-pet-list>
In src/app/pet-list/pet-list.component.spec.ts
add a test spec.
it('Should lookup child', () => {
let pd = fixture.debugElement.query(By.css("app-pet-display"))
expect(pd.componentInstance.constructor.name).toBe("PetDisplayComponent")
})
Run test.
npm test
The test case above fails:
Expected 'PetListComponent' to be 'PetDisplayComponent'.
π₯ Exception or Error
N/A
π Your Environment
Angular Version:
Angular CLI: 10.0.7
Node: 10.14.2
OS: darwin x64
Angular: 10.0.12
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.1000.7
@angular-devkit/build-angular 0.1000.7
@angular-devkit/build-optimizer 0.1000.7
@angular-devkit/build-webpack 0.1000.7
@angular-devkit/core 10.0.7
@angular-devkit/schematics 10.0.7
@angular/cli 10.0.7
@ngtools/webpack 10.0.7
@schematics/angular 10.0.7
@schematics/update 0.1000.7
rxjs 6.5.5
typescript 3.9.7
webpack 4.43.0
Anything else relevant?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
angular 2 unit tests not able to find debugElement
debugElement to confirm the tests are working. However this is always returning as NULL. Tests import { TestBed, async, ComponentFixture } fromΒ ...
Read more >Getting to know fixture.debugElement : Angular Unit Testing
DebugElement is an Angular class that contains all kinds of references and methods relevant to investigate an element as well as component fixture....
Read more >Testing Components β Testing Angular
Introduction to testing Angular Components with Angular's ... The return value of query is a DebugElement again, ... What is wrong here?
Read more >Basics of testing components - Angular
The nativeElement property unwraps the DebugElement and returns the platform-specific element object. Because the sample tests for this guide are designed toΒ ...
Read more >Angular Testing you don't scare me - Blexin
We can see below the code of the app.component.spec.ts file, ... spy and change the behavior of the getList method, returning an error....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, yes. That was my mistake. Adding
PetDisplayComponent
to declarations fixed the issue.OK, so itβs exactly as I suspected. Read the error messages in your terminal. It says:
And you get that error because you havenβt added PetDisplayComponent to the
declarations
of your testing module, insideconfigureTestingModule
. So, as the eror message explains, Angular doesnβt know what the<app-pet-display>
element is: itβs not a standard HTML element, and itβs not, inside this test, known as an angular component either.