2

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();
    });
});
Atif Majeed
  • 1,021
  • 17
  • 14
  • In my company we're facing this exact issue for almost a year now. The timeouts are completely random and when we exclude the failing tests some other tests will fail with a timeout. We have invested an enormous mount of time to find the root cause but have not yet found what really causes the issue. – omnibrain Dec 02 '20 at 08:05

1 Answers1

0

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();
});
Josef
  • 2,869
  • 2
  • 22
  • 23
Star
  • 1
  • 1