- Introduction
- Quickstart
- Installation and usage
- API documentation
- Troubleshooting
- Examples
Troubleshooting
fetch is assigned to a local variable, not a global
First of all, consider whether you could just use fetch as a global. Here are 3 reasons why this is a good idea:
- The
fetchstandard defines it as a global (and in some cases it won’t work unless bound towindow), so to write isomorphic code it’s probably best to stick to this pattern isomorphic-fetchtakes care of installing it as a global in nodejs or the browser, so there’s no effort on your part to do so.fetch-mockis primarily designed to work withfetchas a global and your experience of using it will be far more straightforward if you follow this pattern
Still not convinced?
In that case fetchMock.sandbox() can be used to generate a function which you can pass in to a mock loading library such as mockery instead of fetch
fetch doesn’t seem to be getting mocked?
- If using a mock loading library such as
mockery, are you requiring the module you’re testing after registeringfetch-mockwith the mock loader? You probably should be (Example incorrect usage). If you’re using ES6importit may not be possible to do this without reverting to usingrequire()sometimes. - If using
isomorphic-fetchin your source, are you assigning it to afetchvariable? You shouldn’t be i.e.import 'isomorphic-fetch', notimport fetch from 'isomorphic-fetch'require('isomorphic-fetch'), notconst fetch = require('isomorphic-fetch')
Environment doesn’t support requiring fetch-mock?
- If your client-side code or tests do not use a loader that respects the browser field of package.json use
require('fetch-mock/es5/client'). - If you need to use fetch-mock without commonjs, you can include the precompiled
node_modules/fetch-mock/es5/client-browserified.jsin a script tag. This loads fetch-mock into thefetchMockglobal variable. - For server side tests running in nodejs 0.12 or lower use
require('fetch-mock/es5/server')
Matching Request objects in node fails
In node, if using npm at a version less than 2 the Request constructor used by fetch-mock won’t necessarily be the same as the one used by node-fetch. To fix this either:
- upgrade to [email protected]
- use
fetchMock.config.Request = myRequest, wheremyRequestis a reference to the Request constructor used in your application code.