2

In my enzyme/jest tests on my React component, I get a warning

(node:8898) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined

that appears randomly in the logs of my tested components.

None of the tested components calls for a fetch directly.

I read about to implement libraries like node-fetch from here

ReferenceError: fetch is not defined

but as I import them, they are actually not being called.

I would like to get rid of those warnings, does anyone have an idea on how to resolve the warnings?

Maxeezy
  • 39
  • 1
  • 6
  • yes the problem here is that fetch is a method on window, so basically in your test you need to mock this fuction, so it can run it in your test, don't forget to return a promise – aleEspinosaM Sep 25 '20 at 14:35
  • Real requests aren't supposed to be performed in tests, so `fetch` polyfill is not a good idea. It should be mocked when it's expected. That you don't know which unit calls `fetch` isn't a good thing. You can trace the call by adding `process.on('unhandledRejection', console.error)` to test or setup file. – Estus Flask Sep 25 '20 at 19:04
  • Thx guys! It's end of a project and we will continue writing tests next week! Much appreciated. – Maxeezy Sep 27 '20 at 19:54

0 Answers0