benjreinhart / react-native-aws3

Pure JavaScript React Native library for uploading to AWS S3
MIT License
398 stars 151 forks source link

Jest Test fail, ReferenceError: FormData is not defined #77

Closed pacozaa closed 4 years ago

pacozaa commented 6 years ago

I run yarn test and found this error

ReferenceError: FormData is not defined

      at Object.<anonymous> (node_modules/react-native-aws3/lib/Request.js:148:20)
      at Object.<anonymous> (node_modules/react-native-aws3/lib/RNS3.js:16:16)
      at Object.<anonymous> (node_modules/react-native-aws3/index.js:1:1)

and also exclude package as follow

"jest": {
    "preset": "react-native",
    "verbose": true,
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native-aws3)/)"
    ]
  }

Any idea what's happening?

Best, Sarin

woraponprathuangthip commented 6 years ago

same error +1

abhishek-here commented 6 years ago

i also have the same error

abhishek-here commented 6 years ago

@pacozaa try by moving last slash outside the bracket- "jest": { "preset": "react-native", "verbose": true, "transformIgnorePatterns": [ "node_modules/(?!(react-native-aws3))/" ] }

may it help..

reaganmcf commented 5 years ago

I ended up fixing this issue by cloning the repo and editing the files myself since this repository is no longer maintained (As evident by the 9 PR's). But if you still need a fix you can do the following.

Change

Request.FormData = FormData
Request.XMLHttpRequest = XMLHttpRequest

to

Request.FormData = require('FormData')
Request.XMLHttpRequest = require('XMLHttpRequest')

Then, you have to go into the Jest setup file in node_modules/react-native/jest and change

BlobModule: {
    BLOB_URI_SCHEME: 'content',
    BLOB_URI_HOST: null,
    enableBlobSupport: jest.fn(),
    disableBlobSupport: jest.fn(),
    createFromParts: jest.fn(),
    sendBlob: jest.fn(),
    release: jest.fn(),
}

to

BlobModule: {
    BLOB_URI_SCHEME: 'content',
    BLOB_URI_HOST: null,
    enableBlobSupport: jest.fn(),
    disableBlobSupport: jest.fn(),
    createFromParts: jest.fn(),
    sendBlob: jest.fn(),
    release: jest.fn(),
}

This GitHub issue is what I used to fix the second half of the issue regarding the addNetworkingHandler is not a function error

pacozaa commented 4 years ago

fixed by mock it.

Try this if you come across

jest.mock('react-native-aws3', () => ({
  put: jest.fn(),
}));