Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeolocationPositionError is not defined

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);
  }
}
like image 290
Jonathan Tuzman Avatar asked Oct 19 '25 02:10

Jonathan Tuzman


2 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")

like image 117
djthoms Avatar answered Oct 22 '25 05:10

djthoms


Try this:

if (error instanceof window.GeolocationPositionError)
like image 43
Amartya Mishra Avatar answered Oct 22 '25 03:10

Amartya Mishra