pryanik
pryanik

Reputation: 73

angular 2 component testing errors due to custom elements in html in Jasmine

I am trying to test a component in Angular using Jasmine. the test is simple, it just needs to verify that the component is created.

it('AppComponent should be created', () => { expect(component).toBeTruthy(); });

It is a default spec which is created automatically inside spec file if you use angular cli. My problem is that the html file of the component has some elements that are not regular tags, that is custom.

<app-header></app-header> 

The question is how can I let Jasmine know about them, where they came from, or how to add links etc, so that errors like

Failed: Template parse errors: 'app-header' is not a known element: 1. If 'app-header' is an Angular component, then verify that it is part of this module.

won't show up in Karma debug page. How to verify?

Upvotes: 4

Views: 2229

Answers (2)

vijaya bharathi
vijaya bharathi

Reputation: 61

It might work if you add NO_ERRORS_SCHEMA :

 import { NO_ERRORS_SCHEMA } from '@angular/core';

which would ignore those component errors

Upvotes: 2

Frontend Friedrich
Frontend Friedrich

Reputation: 151

You are not setting up your TestBed.configureTestingModule({} block correctly. It is structured like a module in angular. You need to import and inject your modules/components/services and so on here. Or mock them. Please read a tutorial on this (https://angular.io/guide/testing#test-a-component). As this is very basic and couldn't be explained in a single answer here.

Upvotes: 1

Related Questions