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

ReferenceError: Response is not defined #13

Closed
FredericRuaudel opened this issue Feb 22, 2017 · 10 comments
Closed

ReferenceError: Response is not defined #13

FredericRuaudel opened this issue Feb 22, 2017 · 10 comments

Comments

@FredericRuaudel
Copy link

FredericRuaudel commented Feb 22, 2017

Hi!

I'm experiencing a strange bug today. I was using your module successfully until this morning when suddenly all my test suites failed with error:

Test suite failed to run

    ReferenceError: Response is not defined

      at Object.<anonymous> (node_modules/jest-fetch-mock/src/index.js:11:24)
      at Object.<anonymous> (__tests__/setupJest.js:1:103)

The only thing I did was removing my node-modules folder and then npm install them again. I imagine that I had some modules (or version of modules) in cache that disappeared with the rm command and were replaced by a new one after that. 🤔
But it doesn't seems that any module associated with jest-fetch-mock has changed since yesterday.

I've attached a little project that should reproduce the bug. Just unzip it and run:

npm install
npm test

Thanks in advance for your help!

BugFetchJest.zip

@FredericRuaudel
Copy link
Author

ok, I've fixed my issue by adding import 'isomorphic-fetch' at the beginning of my setupJest.js file. But I still don't understand how it was working previously without it...

@sturdynut
Copy link

Same issue, but adding the import 'isomorphic-fetch' fixed it for me. Why???

@FredericRuaudel
Copy link
Author

I have absolutely no idea. The problem seemed to come out of nowhere and disappears with this fix while it was working perfectly without it, the day before...

@sturdynut
Copy link

Looking at isomorphic-fetch, it sets the global Response object, as well as fetch, Request and Headers (see below). So, that remedies the issue, but still just a band-aid.

@jefflau - Should we be setting these globals (Response, etc) in our jestSetup files or is there a better way?

"use strict";

var realFetch = require('node-fetch');
module.exports = function(url, options) {
	if (/^\/\//.test(url)) {
		url = 'https:' + url;
	}
	return realFetch.call(this, url, options);
};

if (!global.fetch) {
	global.fetch = module.exports;
	global.Response = realFetch.Response;
	global.Headers = realFetch.Headers;
	global.Request = realFetch.Request;
}

@xareelee
Copy link

Adding the import 'isomorphic-fetch' fixed it for me too.

@xareelee
Copy link

Furthermore, before I imported jest-fetch-mock, jest reports a warning:

Warning: flattenChildren(...): Encountered two children with the same key, `0`. Child keys must be unique; when two children share a key, only the first child will be used.

After adding jest-fetch-mock, the flattenChildren was fixed, but introducing a new error Response is not defined .

// Adding 'jest-fetch-mock' will suppress `flattenChildren(...)` error, 
// but introduce the `Response is not defined`  error.
global.fetch = require('jest-fetch-mock');

After adding import 'isomorphic-fetch' to fix the problem, the flattenChildren(...) error was back again.

// After adding `import 'isomorphic-fetch'`,
// the `Response is not defined`  error was gone,
// but the `flattenChildren(...)` error was back again.
import 'isomorphic-fetch'
global.fetch = require('jest-fetch-mock');

@wwwdata
Copy link
Contributor

wwwdata commented May 5, 2017

#8 did also fix it and I don't know why that was undone again

@wwwdata
Copy link
Contributor

wwwdata commented May 5, 2017

I'm doing it like this in my jest setup file now:

const { Response, Headers, Request } = require('whatwg-fetch');

global.Response = Response;
global.Headers = Headers;
global.Request = Request;

@britter
Copy link

britter commented Jul 12, 2017

#8 did also fix it and I don't know why that was undone again

The change in #8 broke stuff for other people. See #9. Maybe we can find a why to fix this for all?

@jefflau
Copy link
Owner

jefflau commented Nov 9, 2018

I've merged in a PR that now supports blobs and uses cross-fetch and adds the Response, Headers and Request as globals. This should resolve this issue as well. Closing this for now, but should be good now.

@jefflau jefflau closed this as completed Nov 9, 2018
tednguyendev added a commit to nimblehq/js-framework-benchmark that referenced this issue May 26, 2023
tednguyendev added a commit to nimblehq/js-framework-benchmark that referenced this issue May 26, 2023
tednguyendev added a commit to nimblehq/js-framework-benchmark that referenced this issue May 26, 2023
tednguyendev added a commit to nimblehq/js-framework-benchmark that referenced this issue May 28, 2023
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

6 participants