MapsApiLoader.load not recognised in karma test
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?
@abyrne85 MapsApiLoader is not the implementation class that is used internally. Generally I don't think it's a good idea to test this in an integration-style test. You could use NO_ERRORS_SCHEMA to disable the maps logic. I'm closing this as this is no bug or feature request. Feel free to comment below.
@abyrne85 did you fix the problem?
@SebastianM, how can I use NO_ERRORS_SCHEMA?
Got it!
beforeEach(() => {
TestBed.configureTestingModule({
...
schemas: [
NO_ERRORS_SCHEMA
]
});
});
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.
" I don't think it's a good idea to test this in an integration-style test"
I respectfully disagree. First, automated tests are actually a good idea to have, you should use them and recommend them to all your users. Second, the ticket specifically mentions karma for unit testing, and mocks are required for unit tests, this has nothing to do with integration tests. Third, NO_ERRORS_SCHEMA is a terrible idea and can cause many problems.
Seems odd to see a library that does not want its end users running automated tests. You could literally copy the 'MockMapsAPILoader ' mentioned above to solve this.
Anyway, good luck on the plugin!
I was able to resolve it like this. Import from core.umd:
import { AgmCoreModule, MapsAPILoader } from '@agm/core/core.umd';
Then provide MapsAPILoader like so:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ AgmCoreModule ],
providers: [
{
provide: MapsAPILoader,
useValue: {
load: jest.fn().mockReturnValue(new Promise(resolve => resolve(true)))
}
}
]
})
.compileComponents();
}));
this is with jest but you can just pass a mock function for load if you aren't using jest
Quick update, importing from '@agm/core/core.umd' caused an issue with the prod build (because you have to import it that way in the actual module) - I got the error
Function calls are not supported in decorators but AgmCoreModule was called
So it appears I have to choose between tests passing or prod build succeeding. Luckily I can just omit the module spec, and this solves my issue for now.
Hi guys,
Try:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [..., AgmCoreModule.forRoot()],
declarations: [..., AgmMarkerCluster]
})
.compileComponents();
}));
Works for me.
@jonhnanthan thank you, this solved this issue for me too.