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.

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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bibhas2commented, Aug 26, 2020

Ah, yes. That was my mistake. Adding PetDisplayComponent to declarations fixed the issue.

beforeEach(async(() => {
  TestBed.configureTestingModule({
    declarations: [ 
      PetDisplayComponent,
      PetListComponent ]
  })
  .compileComponents();
}));
1reaction
jnizetcommented, Aug 26, 2020

OK, so it’s exactly as I suspected. Read the error messages in your terminal. It says:

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

And you get that error because you haven’t added PetDisplayComponent to the declarations of your testing module, inside configureTestingModule. 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.

Read more comments on GitHub >

github_iconTop 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 >

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