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

node_modules/react-native-aws3-cognito ReferenceError: FormData is not defined #9564

Closed
bryanboyko opened this issue Feb 12, 2020 · 4 comments

Comments

@bryanboyko
Copy link

bryanboyko commented Feb 12, 2020

πŸ› Bug Report

 FAIL  ./App.test.js
  ● Test suite failed to run

    ReferenceError: FormData is not defined

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

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.619s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ test: `jest`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @ test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

To Reproduce

Follow setup instructions here

https://docs.expo.io/versions/latest/guides/testing-with-jest/

Install react-native-aws3-cognito in your project

Execute npm run test

Attempt to include react-native-aws3-cognito in package.json transformIgnorePatterns like so

  "jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|@sentry/.*|sentry-expo|native-base|react-native-aws3-cognito/.*)"
    ]
  },

Note that error still occurs

Expected behavior

Tests would run without error

envinfo

  System:
    OS: macOS Mojave 10.14.4
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 12.12.0 - /usr/local/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.11.3 - /usr/local/bin/npm
@thymikee
Copy link
Collaborator

That's likely not an issue with transformation. The error is ReferenceError: FormData is not defined which means there's no FormData available for some reason. You'll need to dig in into node_modules to see why.

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. You'll find more on Jest docs help page. Thank you :)

@SimenB
Copy link
Member

SimenB commented Feb 12, 2020

FormData is not a global in Node, so if your testEnvironment is node (or a derivation of it) it won't be available. You can probably polyfill it in a setupFile though.

@bryanboyko
Copy link
Author

bryanboyko commented Feb 12, 2020

Thanks guys!

Was able to resolve by adding a setupTests.js file that looks like

function FormDataMock() {
    this.append = jest.fn();
}
global.FormData = FormDataMock

And then configuring jest to use the setupTests.js file by adding setupFilesAfterEnv to my package.json

  "jest": {
    "preset": "jest-expo",
    ...
    "setupFilesAfterEnv": [
      "<rootDir>/setupTests.js"
    ]
  }

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants