question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MapsApiLoader.load not recognised in karma test

See original GitHub issue

I’m trying to mock the call to the maps api loader but karma keeps failing saying that load() is not recognised

Test:

describe('CommuteDetailsModalComponent', () => {
  let component: CommuteDetailsModalComponent;
  let fixture: ComponentFixture<CommuteDetailsModalComponent>;
  let mapsApiLoader;
  
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [CommuteDetailsModalComponent],
      providers: [
        MapsAPILoader
      ]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(CommuteDetailsModalComponent);
    component = fixture.componentInstance;
    mapsApiLoader = fixture.debugElement.injector.get(MapsAPILoader);
    spyOn(mapsApiLoader, 'load').and.callFake(() => {
      return true;
    });
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Result :

Error: <spyOn> : load() method does not exist
Usage: spyOn(<object>, <methodName>)

Any ideas what I’m missing?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
liamleppardcommented, Apr 9, 2018

To add to this, you should be able to mock the MapsAPILoader with a mock such as:

export class MockMapsAPILoader {
  public load(): Promise<boolean> {
    return new Promise(() => {
      return true;
    });
  }
}

You shouldn’t have to use NO_ERRORS_SCHEMA as a result.

3reactions
jonhnanthancommented, Apr 3, 2019

Hi guys,

Try:

beforeEach(async(() => { TestBed.configureTestingModule({ imports: [..., AgmCoreModule.forRoot()], declarations: [..., AgmMarkerCluster] }) .compileComponents(); }));

Works for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MapsApiLoader.load not recognised in karma test · Issue #1295
I'm trying to mock the call to the maps api loader but karma keeps failing saying that load() is not recognised Test: ...
Read more >
Developers - MapsApiLoader.load not recognised in karma test -
Coming soon: A brand new website interface for an even better experience!
Read more >
Google maps won't load in angular project because of Error
I have tried to provide MapsApiLoader in admin.module. But this does not work because a typescript error is thrown.
Read more >
Overview | Maps JavaScript API - Google Developers
Get started with the Google Maps JavaScript API. View a simple example, learn the concepts, and create custom maps for your site.
Read more >
nullinjectorerror: no provider for mapsapiloader! angular 2
It is working fine natively on my phone but not in any browser in PRODUCTION. I also could add the InAppBrowser to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found