Reputation: 47
I'm having trouble with code coverage and can't figure it out. I am using google geocoding API to query coordinates where response is returned inside callback function. Jest is used for testing.
This is the testable call with callback:
const geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, (results, status) => {
// want to get coverage in this block
// expected test results are OK and logging shows right results
});
This is the test. Because google.maps is not available by default when testing, I found such solution:
it('test', () => {
const constructorSpy = spyOn(google.maps, 'Geocoder');
const geocoder = createSpyObj('Geocoder', ['geocode']);
constructorSpy.and.returnValue(geocoder);
geocoder.geocode = jest.fn((adr, callback) => callback(response, 'OK'));
// expected results that are all OK
});
createSpyObj https://stackoverflow.com/a/45319913/1756136:
const createSpyObj = (baseName, methodNames): { [key: string]: Mock<any> } => {
let obj: any = {};
for (let i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = jest.fn();
}
return obj;
};
And google.maps is defined in setupTests.js. When not testing, google.maps is available when react loads google map
window.google = {
maps: {
Geocoder: {},
GeocoderStatus: {
OK: 'OK'
}
}
};
Any ideas what I could try or look into? Only coverage is the problem, expected results are fine.
Upvotes: 2
Views: 1608
Reputation: 47
Code coverage is actually already working correctly with this implementation. Problem was that I wasn't accessing 'else' statements in 'if' clauses where there was only 'if' clause.
Also it's possible not to spy on, but define as a property and run your test:
it('test', () => {
Object.defineProperty(google, 'maps', {
value: {
Geocoder: function () {
return {
geocode: jest.fn((adr, callback) => callback(response, 'OK'))
}
}
}
});
// expect..
});
Upvotes: 1