isomorphic-fetch icon indicating copy to clipboard operation
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

Open ericlau-solid opened this issue 9 years ago • 15 comments

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.

ericlau-solid avatar Dec 29 '15 20:12 ericlau-solid

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.

billinghamj avatar Jan 05 '16 11:01 billinghamj

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).

ericlau-solid avatar Jan 05 '16 22:01 ericlau-solid

Having this same issue... Should we exclude something from the build (webpack externals config) when building for Node to prevent this?

Download avatar Jan 30 '16 01:01 Download

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

billinghamj avatar Jan 30 '16 22:01 billinghamj

Much obliged @billinghamj ! Just wait for the merge I guess

Download avatar Jan 30 '16 22:01 Download

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.

lucasfeliciano avatar Feb 02 '16 21:02 lucasfeliciano

What is the status of this merge?

montogeek avatar Jun 15 '16 15:06 montogeek

@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 avatar Jun 15 '16 15:06 billinghamj

@billinghamj Yes, just testing it :) Thanks :D

montogeek avatar Jun 15 '16 16:06 montogeek

Same self is not defined when trying to use command line mocha test and import fetch from 'isomorphic-fetch';

ivnmaksimovic avatar Jul 05 '16 07:07 ivnmaksimovic

@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 avatar Nov 17 '16 01:11 andrewmclagan

@andrewmclagan Indeed :)

montogeek avatar Nov 17 '16 01:11 montogeek

I'm still getting this issue

my solution: https://github.com/lucasfeliciano/fetch-everywhere/issues/7

marktrobinson avatar Apr 10 '17 05:04 marktrobinson

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.

noamshay avatar Jun 11 '19 15:06 noamshay

Another solution is to make sure node has a self property, via: https://www.npmjs.com/package/node-self.

LucasUnplugged avatar Sep 17 '21 20:09 LucasUnplugged