Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting referenceerror: self is not defined when running mocha tests in command line as soon as I attempt to use isomorphic-fetch #59

Open
ericlau-solid opened this issue Dec 29, 2015 · 15 comments

Comments

@ericlau-solid
Copy link

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.

@billinghamj
Copy link

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.

@ericlau-solid
Copy link
Author

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

@Download
Copy link

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

@billinghamj
Copy link

I got this fixed within GitHub's code (JakeChampion/fetch#253)

But my PR on this repo also needs to be merged to resolve this entirely: #63

@Download
Copy link

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

@lucasfeliciano
Copy link

Yeah, I'm waiting for the merge as well. 👍

Since it is a quick fix I made the changes directly in the file on node_modules just to keep developing.

@montogeek
Copy link

What is the status of this merge?

@billinghamj
Copy link

@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

@montogeek
Copy link

montogeek commented Jun 15, 2016

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

@ivnmaksimovic
Copy link

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

@andrewmclagan
Copy link

andrewmclagan commented Nov 17, 2016

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

@montogeek
Copy link

@andrewmclagan Indeed :)

@marktrobinson
Copy link

marktrobinson commented Apr 10, 2017

I'm still getting this issue

my solution: lucasfeliciano/fetch-everywhere#7

@noamshay
Copy link

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.

@LucasUnplugged
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants