Angular 2 Karma Test 'component-name' is not a known element
Because in unit tests you want to test the component mostly isolated from other parts of your application, Angular won't add your module's dependencies like components, services, etc. by default. So you need to do that manually in your tests. Basically, you have two options here:A) Declare the original NavComponent in the test
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
NavComponent
]
}).compileComponents();
}));
B) Mock the NavComponent
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
MockNavComponent
]
}).compileComponents();
}));
// it(...) test cases
});
@Component({
selector: 'app-nav',
template: ''
})
class MockNavComponent {
}
You'll find more information in the official documentation. Angular 5 karma test showing 'app-addbook' is not a known element
Start by adding the AddbookComponent
in the declarations
section of the book.component spec file. See below.
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import {AddbookComponent} from './addbook.component';
import { BookComponent } from './book.component';
describe('BookComponent', () => {
let component: BookComponent;
let fixture: ComponentFixture<BookComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ BookComponent, AddbookComponent ]
})
.compileComponents();
}));
...
});
There is a good chance you will have to add other things to the spec as you troubleshoot this. You have changed things in the application, so the specs need to be updated as a result.Here is a good resource for Angular testing techniques.
Angular5/Karma 'selector' is not a known element
You need to stub child component when you are testing parent component, For example I have two components one is app
and another is dj-menu
then I should mock dj-menu
in app
test using code something like below
@Component({
selector: 'dj-menu',
template: '<h1> Some test </h1>'
})
class DjMenuComponentStub{
}
And my app component look like this (Which uses dj-menu
)@Component({
selector: 'app',
template: '<dj-menu></dj-menu>'
})
class AppComponent{
}
While testing app component, angular doesn't know about dj-menu
you have to tell angular to use stub component by declaring TestBed.configureTestingModule({
declarations: [ DjMenuComponentStub ]
})
Angular 2 + ng test: 'X' is not a known component
I was facing the same issue and it turn out that for tests you need to declare used components as well:
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
MyNewComponent
],
});
Angular Karma - nb-card-header' is not a known element
I have fixed the issue with adding 'NbCardModule'.
Element not known error in Karma when ran ng test
Angular developers often get confused by this. When you run ng test
, karma and jasmine runs angular modules defined within .spec.ts
files. It does not look at your normal code at all. So whatever you put in app.module.ts
has no effect whatsoever on your test module. There are two things you could do.
Add
CUSTOM_ELEMENTS_SCHEMA
to testing moduleWithin
app.component.spec.ts
do the following
TestBed.configureTestingModule({
declarations: [
AppComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
This would solve the error you are getting now. However, you'll probably encounter others as I've seen you inject some services to AppComponent
which brings us to next thing you could doImport
AppModule
within test module.You could just import
AppModule
withinTestBed
as follows. What this will do is to ensure that your tests always have what they need defined. If you add another component toAppModule
and use it inAppComponent
, it will be already available in test as well. Also, you won't need to addCUSTOM_ELEMENTS_SCHEMA
.However, you should be aware that this will import and create whatever 3rd party component/services you use within
app.component
. One would argue that it is against the nature of unit testing. You can still mock those services somehow but they will be rendered. Also, in angular applications, when testing a module that importsRouterModule
,RouterTestingModule
is used instead. UsingRouterModule
within test may mess your tests up as these tests run on a headless browser andRouterModule
may cause URL changes.
TestBed.configureTestingModule({
imports: [
AppModule
],
}).compileComponents();
Angular selector is not a known element: if it's an Angular component, then verify that it is part of this module
Maybe the module that you import doesn't have an export for component that you are looking for?
When you e.g have a component let's say ComponentA in ModuleA, to use ComponentA in other ModuleB, you need to have in Modul A
declarations: [ComponentA]
exports: [ComponentA]
Related Topics
How to Auto-Slide the Window Out from Behind Keyboard When Textinput Has Focus
Why Is 'This' Undefined Inside Class Method When Using Promises
Catching Errors in JavaScript Promises with a First Level Try ... Catch
JavaScript Summing Large Integers
Differencebetween Settimeout(Fn, 0) and Settimeout(Fn, 1)
Differencebetween Native Objects and Host Objects
What Exactly Can Cause an "Hierarchy_Request_Err: Dom Exception 3"-Error
Scope of Default Function Parameters in JavaScript
JavaScript Time Zone Is Wrong for Past Daylight Saving Time Transition Rules
Bootstrap: Open Another Modal in Modal
Understanding JavaScript Promise Object
JavaScript String Array to Object
Using Raw Image Data from Ajax Request for Data Uri
Check If File Exists But Prevent 404 Error in Console from Showing Up
How to Pass a Flag to Gulp to Have It Run Tasks in Different Ways