First I have created a new project using the angular cli using the following command
ng new my-project
Then I installed bootstrap 4 using the instructions from the angular-cli readme
After that I installed NG Bootstrap
I then created a new component using the following command
ng g component my-carousel
I then used the following markup to render a carousel in my-carousel/my-carousel.component.html
<ngb-carousel class="app-my-carousel">
<template ngbSlide>
<img src="http://lorempixel.com/900/500?r=1" alt="First">
<div class="carousel-caption">
<h3>First</h3>
<p>First description</p>
</div>
</template>
<template ngbSlide>
<img src="http://lorempixel.com/900/500?r=2" alt="Second">
<div class="carousel-caption">
<h3>Second</h3>
<p>Second description</p>
</div>
</template>
</ngb-carousel>
I am able to view the carousel in a browser but when I execute the tests with the following command
ng test --single-run
I get the following error
'ngb-carousel' is not a known element:
1. If 'ngb-carousel' is an Angular component, then verify that it is part of this module.
2. If 'ngb-carousel' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component t o suppress this message.
Here is the code for the test my-carousel.component.spec.ts
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { MyCarouselComponent } from './my-carousel.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
describe('MyCarouselComponent', () => {
let component: MyCarouselComponent;
let fixture: ComponentFixture<MyCarouselComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyCarouselComponent ],
imports: [ NgbModule.forRoot() ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyCarouselComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should render the bootstrap carousel', async(() => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('ngb-carousel')).not.toBeNull();
}));
});
I searched around a little and found the following questions which are similar but not the same as my problem
Template parse error in Jasmine test but not actual app
Angular2 Cli Test (Webpack) Erros: “Error: Template parse errors”
app.component.html
andapp.component.spec.ts
? Maybe your error is coming fromapp.component.spec.ts
– yurzui