3

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);
  }
}
Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
  • 2
    I couldn't find any [predefined environment](https://raw.githubusercontent.com/sindresorhus/globals/main/globals.json) that contains `GeolocationPositionError`, although it seems to exist in all browsers, so probably you'll just have to define it as a global in your ESLint config. – GOTO 0 Jan 19 '22 at 04:53

2 Answers2

0

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

djthoms
  • 3,026
  • 2
  • 31
  • 56
  • for all readonly properties above I'm getting error, e.g. `Property 'code' has no initializer and is not definitely assigned in the constructor.ts(2564)` – sKopheK Nov 06 '22 at 11:38
0

Try this:

if (error instanceof window.GeolocationPositionError)
Amartya Mishra
  • 145
  • 3
  • 7