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.

@ViewChild returns undefined in unit tests with material components

See original GitHub issue

Bug:

When I try to access to a material component using @ViewChild from the unit tests I recieve undefined. If I use @ViewChild('element') element: ElementRef; all is fine, but if I use for instance @ViewChild('prompt') prompt: MdSlideToggle; the ‘prompt’ value is undefined. I need to use MdSlideToggle instead of ElementRef because I need to access to the specific properties of the component.

What is the expected behavior?

The component accessed through the @ViewChild decorator should be visible in the unit tests.

What is the current behavior?

The component accessed through the @ViewChild decorator is undefined in the unit tests.

What are the steps to reproduce?

Create the following:

  • html:

<md-slide-toggle #prompt></md-slide-toggle>

  • ts:

@ViewChild('prompt') prompt: MdSlideToggle;

Now create a simple unit test and try to access to the ‘prompt’ element. The value is undefined.

What is the use-case or motivation for changing an existing behavior?

Because the angular material components should be accessible from the unit tests like the common html components.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

  • Angular: 4.3.6
  • Material: 2.0.0-beta.10
  • Windows 10
  • TypeScript: 2.4.2
  • All browsers

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
abhinavbhardwaj2011commented, Dec 13, 2018

Hello Guys, I need bit help in unit testing. It would be nice if someone can help me to write the unit test for slider and few other components. I can pay a small amount for it… Thank you

5reactions
JSMikecommented, Dec 5, 2017

using @ViewChild from the unit tests I receive undefined

Just to leave a comment, in case others get here from a google search.

In unit tests if your test component has an incomplete template, or have an html syntax error, it will get past compiling the component (it will pass the truthy test) and any View/Conent child/ren variables will be undefined.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 unit testing - @ViewChild is undefined
I have a vague feeling like most of the upvoters have a different problem. Make sure that your child component is not hidden...
Read more >
cannot read properties of undefined angular testing - You.com
I am trying to unit test a component. export class ThinComponent implements OnInit { @Input() component!: BrComponent; componentModels: any; constructor() ...
Read more >
Testing Components with children - Testing Angular
A unit test of HomeComponent does not render these children. The host elements are rendered, but they remain empty. You might wonder, what...
Read more >
Angular Unit Testing @ViewChild - InDepth.Dev
This articles explains how to writ unit tests for components with @ViewChild decorators using a stub component.
Read more >
Angular Unit Testing @ViewChild - Morioh
Testing @ViewChild with a Stub Component ... Testing our ParentComponent by just including ChildComponent in the declarations isn't optimal. This is because ...
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