You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dear Ionic Team, could you please make an example mocking plugins (or parts of the module) from @capacitor/core
After several hours of trying, I wasn't able to mock this plugin, mostly running into ReferenceError: fetch is not defined which I believe suggests that CapacitorHttp.get() or CapacitorHttp.reuqest() calls were NOT mocked.
Using @capacitor/core": "^4.6.1", @angular/core": "14.2.2", "@ionic/angular": "^6.0.1", "jest": "28.1.1",
The text was updated successfully, but these errors were encountered:
So finally I was able to make it work by creating a __mocks__/@capacitor/core.ts file with a content:
export*from'@capacitor/core';// this is very IMPORTANT!import{CapacitorHttpPlugin,HttpResponse}from'@capacitor/core/types/core-plugins';exportconstmockCapacitorHttpResponse: HttpResponse={status: 200,headers: {},url: 'mockedResponseUrl',data: {},};exportconstCapacitorHttp: CapacitorHttpPlugin={request: jest.fn(()=>Promise.resolve(mockCapacitorHttpResponse)),get: jest.fn(()=>Promise.resolve(mockCapacitorHttpResponse)),post: jest.fn(()=>Promise.resolve(mockCapacitorHttpResponse)),put: jest.fn(()=>Promise.resolve(mockCapacitorHttpResponse)),patch: jest.fn(()=>Promise.resolve(mockCapacitorHttpResponse)),delete: jest.fn(()=>Promise.resolve(mockCapacitorHttpResponse)),};
Re- exporting all the other stuff (that is not mocked) from the core module was important.
Then it can be used as usual - that is modifying the method implementation in a specific test it('some test',() => { //...test}) or in the beforeEach() method:
Dear Ionic Team, could you please make an example mocking plugins (or parts of the module) from
@capacitor/core
After several hours of trying, I wasn't able to mock this plugin, mostly running into
ReferenceError: fetch is not defined
which I believe suggests thatCapacitorHttp.get()
orCapacitorHttp.reuqest()
calls were NOT mocked.Using
@capacitor/core": "^4.6.1"
,@angular/core": "14.2.2"
,"@ionic/angular": "^6.0.1"
,"jest": "28.1.1"
,The text was updated successfully, but these errors were encountered: