isomorphic-fetch
isomorphic-fetch copied to clipboard
Getting referenceerror: self is not defined when running mocha tests in command line as soon as I attempt to use isomorphic-fetch
So I wanted to use this, and I have a SPA client only app using webpack, npm, es6. The moment I add this line in:
import fetch from 'isomorphic-fetch';
The moment I have this line, my mocha tests crash with -- referenceerror: self is not defined -- error. Mocha browser runs fine, and I can perform a fetch correctly.
I can't get command line mocha test fixed. How do I fix this? I don't have this issue when I import any other libraries, including React-DOM which is definitely dependent on a DOM.
This issue also causes React Native to crash. The problem is if GitHub's library is loaded without a browser environment, self isn't defined. I believe React Native's packager will include the script even if it is not required.
My workaround right now is to use webpack's providerPlugin to load fetch for dev, deploy, but not test (separate webpack.config file for each env). That means of course that I cannot call actual fetch at all, but have to stub it via a mocking framework. This is all right for unit testing, but won't work for integration testing. Also disallows me from using sinon's fake server functionality (since that would require an actual fetch call).
Having this same issue... Should we exclude something from the build (webpack externals config) when building for Node to prevent this?
I got this fixed within GitHub's code (https://github.com/github/fetch/pull/253)
But my PR on this repo also needs to be merged to resolve this entirely: #63
Much obliged @billinghamj ! Just wait for the merge I guess
Yeah, I'm waiting for the merge as well. :+1:
Since it is a quick fix I made the changes directly in the file on node_modules just to keep developing.
What is the status of this merge?
@matthew-andrews seems to be uninterested in resolving this. As such, @lucasfeliciano created "fetch-everywhere" instead which is designed to work in every circumstance - https://github.com/lucasfeliciano/fetch-everywhere
@billinghamj Yes, just testing it :) Thanks :D
Same self is not defined when trying to use command line mocha test and import fetch from 'isomorphic-fetch';
@matthew-andrews: Also experiencing this, is there no solid solution yet, or roadmap for this to be merged. The community seems to be drifting away from this lib in favour of fetch-everywhere simply due to this issue.
@andrewmclagan Indeed :)
I'm still getting this issue
my solution: https://github.com/lucasfeliciano/fetch-everywhere/issues/7
i had this error working with chai/mocha test.
fixed by this workaround:
add fix-global-self.js with:
global.self = global;
module.exports = global;
then import it at the top of my test:
import "./fix-global-self"
works for me.
Another solution is to make sure node has a self property, via: https://www.npmjs.com/package/node-self.