0

I have a mind-boggling issue. I have some code I am testing in two different projects, both in angular. In the first project, the tests work, in the second I get the following error.

TypeError: Cannot read property 'assertPresent' of undefined

I saw this question about the issue, but all solutions they discuss importing zone.js and the order etc. However, in neither my working test project nor my non-working one, I do not explicitly import zone.js.

The testing code tests http with a delay, so it needs fakeAsync to use tick. This is the test that tests the codes in both projects:

it('should throw error if action failed`, fakeAsync(()=>{
    const mockGetActionResponse = {'action' : {'id' : mockActionId, state: 'Failed' }};
    
    service.doAction(mockContent).subscribe(
        () => {},
        err => expect(err).toEqual('Action Failed');
    );

    const postCall = httpTestingController.expectOne(url1);
    expect(postCall.request.method).toEqual('POST');
    expect(postCall.request.body).toEqual(mockContent);

    postCall.flush(mockPostActionResponse);

    tick(config.retryTime);

    const getCall = httpTestingController.expectOne(url2/mockActionId);
    expect(getCall .request.method).toEqual('Get');

    getCall.flush(mockGetActionResponse );
}));

Now in project 1, it works perfectly, and in project 2 I get the aforementioned error. I have the same imports in both:

import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {fakeAsync, TestBed, tick} from '@angular/core/testing';

in my package.json devDependencies I have the following similarities: (removed quotes here for ease)

jasmine-core: 3.6.0,
jasmine-spec-reporter: 6.0.0,
karma: 5.2.3 (p1) / ^5.2.3 (p2)
karma-jasmine: 4.1.0,
karma-jasmine-html-reporter: 1.5.4

Now, in p2 I updated karma-coverage to no longer use instabul, and p1 uses electron where as p2 does not, so now I have the following differences too:

karma-coverage-instabul-reporter: 3.0.3, // P1 only
karma-coverage: ^2.0.3, // p2 only
karma-electron: 6.3.1, // p1 only
karma-chrome-launcher: ~3.1.0 // p2 only 
karma-junit-reporter: ^2.0.1 //p2 only

similar regular dependency: zone.js: 0.11.1 (p1) / ^0.11.2 (p2)

The other major difference perhaps worth noting is that p1 is angular 10, where as p2 is angular 11. Again, the code is the same, as is the test, so I am having a really hard time understanding why in p2 I get the aforementioned error.

Any leads would be greatly apprectiated!

PMO1948
  • 2,210
  • 11
  • 34

1 Answers1

2

Thanks to this comment on a github post, I discovered that P1 had an import p2 lacked.

In test.ts, the file that is required by karma.conf.js and is responsible for recursively loading all the spec files, there was an import missing. Adding it to p2's test.ts solved the issue.

import 'zone.js/dist/zone-testing';

PMO1948
  • 2,210
  • 11
  • 34