While working on writing test cases for a modified version of the Angular.io Heroes tutorial, I encountered an interesting issue with one of the components. No matter what I tried, I couldn't get this particular test to fail.
Here's an example of the code:
describe('OrgDetailComponent', () => {
let comp: OrgDetailComponent;
let fixture: ComponentFixture<OrgDetailComponent>;
let de: DebugElement;
let el: HTMLElement;
let org1: Org = new Org({ ... fills Org ... });
let dialogService: DialogService = null;
let globalsService: GlobalsService = null;
let orgService: OrgService = null;
let routeStub: any = { data: Observable.of( { org: org1 } ) } ;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ FormsModule, RouterTestingModule ],
providers : [
{ provide: DialogService, useClass: MockDialogService },
{ provide: GlobalsService, useClass: MockGlobalsService },
{ provide: OrgService, useClass: MockOrgService },
{ provide: ActivatedRoute, useValue: routeStub }
],
declarations: [ OrgDetailComponent ],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(OrgDetailComponent);
dialogService = fixture.debugElement.injector.get(DialogService);
globalsService = fixture.debugElement.injector.get(GlobalsService);
orgService = fixture.debugElement.injector.get(OrgService);
});
it('should always fail', () => {
fixture.detectChanges();
fixture.whenStable().then(() => {
fail('forced fail');
});
});
});
Running this test did not yield any failures. Even after wrapping the fail statement in a try-catch block, the test still passed without triggering the catch block. The Karma testing framework consistently reported success. Why is that?
This issue is quite important as it prevents me from ensuring the accuracy of my tests. Despite knowing there were errors in the code, fixing just one issue made all error messages disappear. Without being able to clearly identify failures, the validity of the entire test suite comes into question.
I would appreciate any insights or solutions. Thank you, Jerome.