Here is my oninit class, which I am not supposed to change.
ngOnInit(): void {
this.setCustomizedValues();
this.sub = PubSub.subscribe('highlightEntity', (subId, entityIdentifier: string) => {
document.querySelector(entityIdentifier).classList.add('highlight');
setTimeout(() => {
document.querySelector(entityIdentifier).classList.remove('highlight');
}, 2000);
});
}
and here is minimal test setup
describe('aComponent', () => {
let httpCallerServiceStub = jasmine.createSpyObj('httpCallerServiceStub', ['get']);
let fixture: ComponentFixture<aComponent>;
let componentInstance: aComponent;
localStorage.clear();
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [aComponent],
providers: [
{
provide: HttpCallerService,
useValue: httpCallerServiceStub
}],
imports: [CommonModule, CmcSpinnerModule, NgbModule],
}).compileComponents();
fixture = TestBed.createComponent(aComponent);
componentInstance = fixture.componentRef.instance;
fixture.autoDetectChanges();
});
describe('ngOnInit method', () => {
beforeEach(() => {
fixture.componentInstance.customized = {};
});
it('should initialize with minimum input', () => {
// Triggers ngOnInit
fixture.detectChanges();
expect(fixture.componentInstance.customized).toEqual({});
});
});
The issue is the tests are failing randomly with the error that classlist not found of null
"message": "An error was thrown in afterAll\nTypeError: Cannot read property 'classList' of null\n at <Jasmine>\n
at webpack:///components/path/aComponent.ts:35:53
this line no 35 refers to this lines in oninit function
setTimeout(() => {
document.querySelector(entityIdentifier).classList.remove('highlight');
}, 2000);
So few questions here
I have tried putting tick(2000) and fake asysnc in a simple test of ngonit but still it is giving me the same random errors (as suggested by this answer )
Is my testbed configuration is missing something? because even when I comment out the oninit test and just check some random function even without fixture.detectChanges(), the same random error is happening.
if I run tests using fdescibe, all the tests get passed every time. This issue only happens when I removed f from fdescribe and run all the tests of other components as well.
Please guide me, I am stuck here since last 4 days.