Jest: Since Jest update, Response is not defined for more than one file.

Created on 22 Apr 2016  路  9Comments  路  Source: facebook/jest

Hi,

I have migrated Jest from 0.8.2 to 0.10.2. I have fixed the small issues I had, except for one.

At this point, I have two API utils files (Flux architecture). To communicate with the REST API, I use the Fetch API, with whatwg-fetch as polyfill. So, to mock the request, I create a Response, as defined in the Fetch API.

Example:

fetch.mockReturnValueOnce(Promise.resolve(new Response(
    '',
    {
        status: 204
    }
)));

If I run the two files individually, they pass. If I run the two files one after the other, then the second one will throw an exception: - ReferenceError: Response is not defined, under Jest 0.10.2. However, under Jest 0.8.2, the tests passed.

NOTE
The project is under Windows and due to some issues with the multi threading, we have to use jest --runInBand. I have made a small project to reproduce, but I only had the issues with that config.

Thanks,
Pascale

Most helpful comment

I'm having this issue on Jest 15.1.1, both new Response and new Request give ReferenceError: Response/Request is not defined.

Any recommendations?

edit: I solved it by importing isomorphic-fetch on my test file.

All 9 comments

Would you mind re-trying this with Jest on master? I believe this bug was fixed recently.

Ok, I tried out your gist and can confirm that jest -i passes the tests on Jest 12.

I had to add this:

"unmockedModulePathPatterns": [
  "node_modules/babel-polyfill"
]

to the Jest config however. There might be a minor bug there, individual from your issue and I will try to repro that :) Thank you for your patience!

Oh yeah, that is indeed another bug I accidentally introduced. Fixed in #942 and will go out in 12.0.1. You won't need to add anything to the config then :)

Hi Christoph, thanks for the responses. I will try in few days. For now, I am out the office ;) Have a good day!

Everything is working. For history purpose, I had to change nothing, I did not add the babel-polyfill in the package.json and it is working.

I'm having this issue on Jest 15.1.1, both new Response and new Request give ReferenceError: Response/Request is not defined.

Any recommendations?

edit: I solved it by importing isomorphic-fetch on my test file.

We should re-open this issue.
I am using version 23.6.0 and I am still getting this error.
Is importing the isomorphic-fetch the only solution?
Here is my devDependencies in package.json

"devDependencies": {
    "@vue/cli-plugin-babel": "3.0.3",
    "@vue/cli-plugin-e2e-cypress": "3.0.3",
    "@vue/cli-plugin-eslint": "3.0.3",
    "@vue/cli-plugin-unit-jest": "3.0.3",
    "@vue/cli-service": "3.0.3",
    "@vue/test-utils": "1.0.0-beta.25",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "23.6.0",
    "less": "3.0.4",
    "less-loader": "4.1.0",
    "lodash": "4.17.10",
    "node-sass": "4.9.2",
    "sass-loader": "7.0.3",
    "vue-template-compiler": "2.5.16",
  }

This issue is more than 2 years old. If you have a similar one, please open up a new issue with reproduction steps

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kentor picture kentor  路  3Comments

kgowru picture kgowru  路  3Comments

samzhang111 picture samzhang111  路  3Comments

Secretmapper picture Secretmapper  路  3Comments

withinboredom picture withinboredom  路  3Comments