fetch-intercept
fetch-intercept copied to clipboard
No fetch avaibale. Unable to register fetch-intercept
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)
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 [email protected] as a dev dependency in the main project worked for me.