1

I have one failure when I run 'ng test' on my Angular 6 app:

Failed: Cannot read property 'textContent' of null


Please see sample app... SampleApp


The problem appears to be with the app.component.spec.ts file. See error message below:

AppComponentTest > -> should render title in a h1 tag
Failed: Cannot read property 'textContent' of null
    at <Jasmine>
    at UserContext.eval (./src/app/app.component.spec.ts?:31:44)
    at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26)
    at AsyncTestZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:712:39)
    at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:284:39)
    at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:386:32)
    at Zone.runGuarded (./node_modules/zone.js/dist/zone.js?:150:47)
    at runInTestZone (./node_modules/zone.js/dist/zone-testing.js?:840:29)
    at UserContext.eval (./node_modules/zone.js/dist/zone-testing.js?:774:17)
    at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26)
    at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:287:39)

2
  • 1
    There's no h1 in the template for AppComponent. Move test to the component with the h1 tag in the template. Commented Oct 3, 2018 at 13:35
  • That worked, thanks! Commented Oct 3, 2018 at 13:52

1 Answer 1

2

The setup for the component under test includes CUSTOM_ELEMENTS_SCHEMA, which allows the use of custom tag elements in the template without complicating the test by instantiating the associated component. As a result, testing for an element in a child component from the parent fails. The solution is to move the failing test spec into proper child component.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.