I’m following these instructions on how to mock native-plugins. Trying this leads to the error in the title.
I am using Ionic 5. Has the way to mock native plugins changed dramatically?
I’m following these instructions on how to mock native-plugins. Trying this leads to the error in the title.
I am using Ionic 5. Has the way to mock native plugins changed dramatically?
Mmm, probably, I haven’t used native-mocks to be honest, what have you tried so far?
Can you share some code please.
Honestly? Not much.
I’m using the mock to test with Jasmine, instancing the mock class directly instead of providing, during the tests.
Instead of extending from Health, I CAN extend HealthOriginal. That works and gives me the desired workflow, but it fails when I start the tests, and I’m guessing this relates to how it is imported.
I’ve also looked at the mock-classes you’ve provided, but I fail to see how the results would be much different.
As an emergency, I can just leave the mock as empty, but having a connection between the mock class and real class is a pretty big advantage.
This does not work:
import { HealthData, HealthQueryOptions, HealthDataType, Health } from '@ionic-native/health';
import { LocalHealthData } from '../../../health-data/services/health-data-services';
import { Injectable } from '@angular/core';
@Injectable()
export class HealthMock extends Health {
data: HealthData[];
authorized = false;
available = true;
readAuthorizations: string[];
constructor(data: LocalHealthData[]) {
super();
this.setData(data);
this.readAuthorizations = [];
}
Leading to the following error: Type 'HealthOriginal' is not a constructor function type.
This does work when developing:
import { HealthData, HealthQueryOptions, HealthDataType, Health, HealthOriginal } from '@ionic-native/health';
import { LocalHealthData } from '../../../health-data/services/health-data-services';
import { Injectable } from '@angular/core';
@Injectable()
export class HealthMock extends HealthOriginal {
data: HealthData[];
authorized = false;
available = true;
readAuthorizations: string[];
But leads to the following problem when trying to test:
ERROR in ./src/app/native/health-data-provider/__tests__/HealthMock.ts 4:42-56
"export 'HealthOriginal' was not found in '@ionic-native/health'
Maybe an /ngx
on the end of an import might help?
Unfortunately not. Didn’t seem to have an effect.
How about ditching inheritance all together? As long as (enough of) the external API surface of the Health
class is present (i.e. methods and properties), I would expect things to work out.
Yes, that is a solution, though I lose the handy connection I would’ve liked to have with the base class, in terms of auto-complete and the likes. But yes, it is possible, if there’s no other way