React-native: `fetch` undefined when running tests with jest

Created on 19 Dec 2016  ยท  8Comments  ยท  Source: facebook/react-native

Description

In trying to run tests with jest I am finding that fetch isn't available when running the tests. This appears to have broken when the configuration for react-native was moved from the jest repo to the react-native repo, which I think is because the whatwg-based fix which was added to jesthasn't been copied across when it was removed from jest.

Reproduction

Any test that uses fetch fails.

Solution

Add back fetch into jest/setup.js

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

Additional Information

  • React Native version: 0.39.0
  • Jest version: 18.0.0
  • Platform: to specific to iOS or Android
  • Operating System: MacOS
Locked

Most helpful comment

Since I posted my comment I was able to make it work.
Production code doesn't define fetch, because otherwise the app would crash. So in order for fetch to be available in jest tests I added import "isomorphic-fetch" in test files and it started working.

All 8 comments

I am seeing the same issue.

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

Having the same issue with

react-native-cli: 2.0.1
react-native: 0.48.1
jest: 20.0.4

The project is written in typescript. App works but jest tests fail due to missing fetch. When I import fetch from isomorphic-fetch tests work but app is not.

@hramos this issue definitely should be fixed rather that closed :-(
RN exposed fetch API, it is clearly stated in docs: http://facebook.github.io/react-native/docs/network.html#content
therefore react-native jest preset (hosted in RN repository now) should also provide the API!

Since I posted my comment I was able to make it work.
Production code doesn't define fetch, because otherwise the app would crash. So in order for fetch to be available in jest tests I added import "isomorphic-fetch" in test files and it started working.

Yes, now I added to my jest-setup.js this:

const fetchPolifill = require('whatwg-fetch')
global.fetch = fetchPolifill.fetch
global.Request = fetchPolifill.Request
global.Headers = fetchPolifill.Headers
global.Response = fetchPolifill.Response

But I need specifically support objects Request, Headers, Response... Of course tests do not actually send any requests.

Adding these in my setup file doesn't fix the issue

// global-mocks.js
const fetchPolifill = require('whatwg-fetch')

global.fetch = fetchPolifill.fetch
global.Request = fetchPolifill.Request
global.Headers = fetchPolifill.Headers
global.Response = fetchPolifill.Respons
// package.json
...
  "jest": {
    "preset": "jest-expo",
    "verbose": true,
    "rootDir": ".",
    "setupFiles": [
      "<rootDir>/global-mocks.js"
    ],
    "globals": {
      "__DEV__": true
    },
    "transform": {
      "^.+\\.js$": "babel-jest"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|react-navigation|native-base)/"
    ]
  }
...



md5-9b59bd6a4d78e143368cb7cf31df32e6



yarn run v1.3.2
$ node node_modules/jest/bin/jest.js
 FAIL  ./App.test.js
  โ— Test suite failed to run

    TypeError: Cannot read property 'fetch' of undefined

      at node_modules/whatwg-fetch/fetch.js:4:9
      at Object.<anonymous> (node_modules/whatwg-fetch/fetch.js:466:3)
      at Object.<anonymous> (node_modules/jest-expo/src/setup.js:101:348)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.115s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@pendar747 Note, that you use another jest preset! You use jest-expo. According to jest-expo/src/setup.js jest already have fetch defined in globals, so you better to remove this from global-mocks.js. And according to stack-trace, your error originated from jest-expo module, so your better to report it to them ;-)

Was this page helpful?
0 / 5 - 0 ratings