GeolocationPositionError is not defined

Viewed 219

I'm using GeolocationPositionError which should be built in, but es-lint is telling me it's not defined. Webstorm isn't helping me import it. Where do I import this from, or is there another issue here?

The code works:

} catch (error) {
  console.log(error);

  if (error instanceof GeolocationPositionError) {
    console.log("Location access was denied");
    return;
  } else {
    setTimeout(() => {
      console.log("retrying");
      dispatch(fetchUserLocation());
    }, 2000);
  }
}
1 Answers

I mocked it like this:

class GeolocationPositionError extends Error {
  readonly code: number;
  readonly message: string;
  readonly PERMISSION_DENIED: number;
  readonly POSITION_UNAVAILABLE: number;
  readonly TIMEOUT: number;

  constructor(msg?: string) {
    super(msg);
  }
}

global.GeolocationPositionError = GeolocationPositionError;

It's not a true "mock", but it allows jsdom to properly resolve the global. You can spy on it using jest.spyOn(global, "GeolocationPositionError")

Related