Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

382
Vistas
Manually mock Google Maps in Jest

I want to mock the google maps API that I am using with the @googlemaps/js-api-loader package.

For now, I need to ensure that the geocoder was called and I am trying to do so by:

const mockGeocoder = jest.fn();

jest.mock('google.maps.Geocoder', () => () => ({
  geocoder: mockGeocoder,
}));

But I get

Cannot find module 'google.maps.Geocoder' from 'tests/unit/usecases/maps/getPlaceByAddress.spec.ts'

  3 | const mockGeocoder = jest.fn();
  4 |
> 5 | jest.mock('google.maps.Geocoder', () => () => ({

Not sure how to proceed. Can anyone help me with this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Check the package https://www.npmjs.com/package/@googlemaps/jest-mocks for mocks and examples. I'm not sure if Geocoder has a mock, but it should be possible to follow the pattern there (and send a pr).

about 4 years ago · Juan Pablo Isaza Denunciar

0

I actually found it on Stackoverflow. Dont know why yesterday Google didnt recommend this to me.

This is where I found the answer

So I did the following:

const setupGoogleMock = () => {
  global.window.google = {
    maps: {
      Geocoder: jest.fn(() => ({
        geocode: mockGeocoder,
      })),
      GeocoderStatus: {
        ERROR: 'ERROR',
        INVALID_REQUEST: 'INVALID_REQUEST',
        OK: 'OK',
        OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
        REQUEST_DENIED: 'REQUEST_DENIED',
        UNKNOWN_ERROR: 'UNKNOWN_ERROR',
        ZERO_RESULTS: 'ZERO_RESULTS',
      },
    } as any,
  };
};

and then beforeAll(() => { setupGoogleMock(); });

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda