Mocking of Ionic Native plugins for browser usage

Hi there,

I’m currently reading the ionic-native docs regarding the use of native-plugins in a browser. The mentioned solution here works fine in general. I was expecting that the mocks are only used if a certain functionality or plugin is not available for a specific platform. But it seems to me that always the mocks are used if they are defined like in the above mentioned example. Regardless of the platform you are on.

The described alternative / workaround here is not really an option because the emulator for example also serves the app via URL so I cannot use the real plugins there.

Is there another option or did I misunderstood something completly?

Help is appreciated. Thank you in advance.

Tim

Hi Tim,

I had the same usecase as you : I want the mock to be used in the browser but not when I run it on my device. I came up with a neat sollution to enable mocks anywhere and anyhow you wish :

Here my app.module.ts :

const isBrowser = document.URL.startsWith('http');

@NgModule({
[...]
  providers: [
    PlaceService,
    SplashScreen,
    (isBrowser ? {provide: Camera, useClass : CameraMock} : Camera),
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

So I declare the isBrowser variable, and according to it I will use an object with “useClass” which will mock, or the conventional plugin. The solution you linked is overkill.