Create a test utility that doesn't call the Geocodio API
Closed this issue · 1 comments
Testing is a bit difficult currently because I need to mock the geocoder. It would be nice to have a test utility version of this library that has all the same behaviour as new Geocodio()
, but doesn't make API calls so that it doesn't count against API usage and it's more stable.
The input values could be hashed to generate fake, yet consistent, output. They could also have a behaviour like if an address starts with [INVALID]
then it'll fail in the same way the real API call would fail. Also could provide fake constants to use in order to simulate valid and invalid API keys.
Here's what I imagine using it might look like:
import { GeocodioTestClient, VALID_TEST_API_KEY, INVALID_TEST_API_KEY, InvalidApiKeyError } from 'geocodio-test-util';
it('should resolve', () => {
const mockGeocoder = new GeocodioTestClient(VALID_TEST_API_KEY);
expect(async () => await mockGeocoder.geocode(['123 Main Street'])).not.to.throw();
})
it('should resolve', () => {
const mockGeocoderInvalid = new GeocodioTestClient(INVALID_TEST_API_KEY);
expect(async () => await mockGeocoderInvalid.geocode(['123 Main Street'])).to.throw(new InvalidApiKeyError());
})
it('should resolve', async () => {
const mockGeocoder = new GeocodioTestClient(VALID_TEST_API_KEY);
const result = await mockGeocoder.geocode(['123 Main Street...', '456 Pleasant St'])
expect(result.results).to.have.length(2);
})
Hi @davidleger95,
Since the client library has very limited logic we have decided to not mock the JSON API response.
A simple way to test the API without incurring usage, would be to use the built-in, fixed DEMO
api key.
Which allows you to make test requests without an account.
Mathias