mlegenhausen / fetch-intercept

Interceptor library for the native fetch command inspired by angular http intercepts.
MIT License
405 stars 52 forks source link

No fetch avaibale. Unable to register fetch-intercept #44

Open alexey2baranov opened 4 years ago

alexey2baranov commented 4 years ago

in Jest test

import fetch from 'node-fetch'
global.fetch= fetch
import fetchIntercept from "fetch-intercept";

throws

● Test suite failed to run

No fetch avaibale. Unable to register fetch-intercept

  10 | 
  11 | // fetchIntercept.register(errorInterceptor)
> 12 | // fetchIntercept.register(dataInterceptor)
     |                                              ^
  13 | // fetchIntercept.register(cookieInterceptor)
  14 | // fetchIntercept.register(headersInterceptor)
  15 | 

  at attach (node_modules/fetch-intercept/lib/webpack:/src/attach.js?1269:38:13)
  at Object.<anonymous> (node_modules/fetch-intercept/lib/webpack:/src/node.js:3:18)
  at Object.<anonymous> (node_modules/fetch-intercept/lib/node.js:53:31)
  at __webpack_require__ (node_modules/fetch-intercept/lib/webpack:/webpack/bootstrap 288f28a76d94cd56de0b?2753:19:1)
  at node_modules/fetch-intercept/lib/webpack:/webpack/bootstrap 288f28a76d94cd56de0b?2753:39:1
  at Object.<anonymous> (node_modules/fetch-intercept/lib/node.js:44:10)
  at Object.<anonymous> (src/api/index.js:12:46)
  at Object.<anonymous> (src/plugins/bottle.js:24:35)
  at Object.<anonymous> (src/models/AbstractSync.js:25:15)
  at Object.<anonymous> (src/models/index.js:27:44)
  at Object.<anonymous> (src/plugins/className.js:5:38)
  at Object.<anonymous> (tests/test-setup.js:30:1)
Shishir-Tiwari commented 4 years ago

While running in node environment, fetch-intercept uses whatwg-fetch pollyfill but since it is not included in dependencies, it is not available and hence generates this error.

https://github.com/werk85/fetch-intercept/blob/develop/src/attach.js#L36

As a workaround, adding whatwg-fetch@3.2.0 as a dev dependency in the main project worked for me.