I created a very basic component:
import { Component } from '@angular/core';
@Component({
selector: 'loading',
templateUrl: './loading.component.html',
styleUrls: ['./loading.component.scss']
})
export class LoadingComponent {
}
This component simply displays a static loading logo without any user interaction. I decided to write a test to check if it initializes correctly:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LoadingComponent } from './loading.component';
describe('LoadingComponent', () => {
let component: LoadingComponent;
let fixture: ComponentFixture<LoadingComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoadingComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoadingComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should exist', () => {
expect(component).toBeTruthy();
});
});
When running this test in Firefox, I encountered an error: j is undefined
, and in Chromium browsers, the error message was
Cannot read property 'startTime' of undefined
. However, the template used in this component contains only simple text:
Sample Text
There is no reference to 'startTime' in any of these files, so why are these errors occurring?
Note
In other components, I do use properties like startTime
, but I cannot understand why that would impact this particular test.
Edit
To resolve this error, I added the following line of code to every test in the project:
afterAll(() => {
TestBed.resetTestingModule();
});
The error disappeared after implementing this change. I am still unsure about:
- The reason behind this solution/the root cause of the issue
- Whether adding this line to just one test would have been sufficient