Lusito / mockzilla

A mocking toolkit leveraging the power of TypeScript to enhance your jest experience.
https://lusito.github.io/mockzilla/
zlib License
14 stars 2 forks source link

Example of deepMock: TypeError: Cannot read property 'webRequest' of undefined #10

Closed kabeleced77 closed 3 years ago

kabeleced77 commented 3 years ago

Hi, I really like to get mockzilla running for testing my web-extension. First of all I tried the given example here. I followed the instruction here.

But I get

TypeError: Cannot read property 'webRequest' of undefined

   6 |
   7 | export function onBeforeRedirect(callback: () => void) {
>  8 |   browser.webRequest.onBeforeRedirect.addListener(callback, { urls: [""] });
     |           ^
   9 |   return () => browser.webRequest.onBeforeRedirect.removeListener(callback);
  10 | }
  11 |

  at Object.onBeforeRedirect (src/my-web-extension.ts:8:11)
  at Object.<anonymous> (test/my-web-extension.spec.ts:37:30)

Could you please help? I don't know what to do else.

Thank you!

Lusito commented 3 years ago

Mockzilla itself does not mock browser by default. In the example you linked, the top-section does a jest.mock for webextension-polyfill-ts.

Mockzilla (this repository) does not by itself do anything for webextensions. If you have not seen it yet, mockzilla-webextension is a fully-functioning solution for testing webextensions.

If you don't want mockzilla-webextension (for example if you are not using the webextension-polyfill), you can use mockzilla, but need to mock the browser instance yourself.

kabeleced77 commented 3 years ago

Hi, thanks for answering (and understaning) my question. I think I got it now. In fact I started with mockzilla-webextension as I am also using webextension-polyfill-ts. My "problem" was my own misunderstanding of mocking and I wanted to start with just mockzilla.

Now I am using mockzilla-webextension based on your example . For my purpose I "only" needed a mock of browser.storage.local, e.g. mockBrowser.storage.local.set.expect({ key: value }).andResolve(). Now I can also test my code using mockzilla-webextension.

Thanks for your work :).