Reputation: 1071
I recently upgraded an Angular 8 application to use newer version of Karma and related packages (upgraded from "karma": "~4.0.0" to "karma": "~4.4.1"). Many unit tests that worked fine before started to fail intermittently after the upgrade with the below error. Failures were mostly in spec files that had multiple describe blocks that used same instance of the component being tested. I made code changes so that instances are not shared across describe suites and added setting for jasmine.DEFAULT_TIMEOUT_INTERVAL in beforeEach functions. These changes greatly reduced the failures. However, one or two tests may still fail intermittently in the Jenkins pipeline. Running locally does not result in timeouts. Error and sample test follows. As you can see the value of jasmine.DEFAULT_TIMEOUT_INTERVAL is set in the test but still the error reports undefinedms
Upgraded packages
"karma": "~4.4.1",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~2.1.1",
"karma-jasmine": "~3.1.1",
"karma-jasmine-html-reporter": "^1.5.2",
Run tests
ng "test" "test-lib" "--browsers=ChromeHeadlessNoSandbox"
Error:
Error: Timeout - Async function did not complete within undefinedms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL) at <Jasmine>
Sample Test:
import { async, TestBed } from '@angular/core/testing';
import { MyTestModule } from './my-test.module';
describe('MyTestModule', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MyTestModule]
}).compileComponents();
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
}));
it('should create', () => {
expect(MyTestModule).toBeDefined();
});
});
Upvotes: 2
Views: 3761
Reputation: 21
Try to use await
in your beforeEach
to wait for TestBed
to successfully compile before a test is executed:
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MyTestModule],
}).compileComponents();
});
Upvotes: 0
Reputation: 1
You need to call done(); function after all expects; Like:
it('getHeroes should return value from array', (done: DoneFn) =>{
let heroes: Hero[] = [
{ id: 11, name: 'Dr Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
expect(service.getHeroes()).toEqual(heroes);
done();
});
Upvotes: 0