Jasmine is the framework we are going to use to create our tests. It has a bunch of functionalities to allow us the write different kinds of tests.
karma is a task runner for our tests. It uses a configuration file in order to set the startup file, the reporters, the testing framework, the browser among other things.
To run the test you only need to run the command “ng test”.
Karma Config
Karma.config.js
Test entry file
The angular-cli configuration of karma uses the file “test.ts” as the entry point of the tests for the application.
Unit test component
describe('MyPopoverComponent', () => {
let component: MyPopoverComponent;
let fixture: ComponentFixture<MyPopoverComponent>;
let popoverController;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyPopoverComponent],
imports: [
IonicModule.forRoot(),
RouterTestingModule,
TranslateModule.forRoot(),
],
providers: [
{
provide: PopoverController,
useClass: class {
dismiss = jasmine.createSpy('dismiss');
},
},
],
}).compileComponents();
fixture = TestBed.createComponent(MyPopoverComponent);
component = fixture.componentInstance;
fixture.detectChanges();
popoverController = TestBed.get(PopoverController);
}));
it('should create', () => {
expect(component).toBeTruthy();
});
it('should create close', () => {
component.close();
expect(component.close).toBeDefined();
});
});