Skip to content

Bug: Mocks broken for plain JS projects #236

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

Closed
janhesters opened this issue Nov 5, 2019 · 11 comments
Closed

Bug: Mocks broken for plain JS projects #236

janhesters opened this issue Nov 5, 2019 · 11 comments

Comments

@janhesters
Copy link

janhesters commented Nov 5, 2019

Current behavior

When you follow the docs on how to integrate this module with Jest in a plain JS project, tests don't run. The reason is that the mock code references Flow types.

  ● Test suite failed to runmponent.test.js

    /Users/jan/dev/app/node_modules/@react-native-community/async-storage/jest/async-storagemock.js: 6
    type KeysType = Array<string>;
         ^^^^^^^^

    SyntaxError: Unexpected identifier

See also discussion in #39.

Expected behavior

The mock should work for projects that avoid static types.

Repro steps

  1. Create a project without static types (e.g. npx react-native init myproject) and install AsyncStorage
  2. In your project root directory, create __mocks__/@react-native-community directory.
  3. Inside that folder, create async-storage.js file.
  4. Inside that file, export Async Storage mock.
  5. Run a test that uses AsyncStorage
export default from '@react-native-community/async-storage/jest/async-storage-mock'

Environment

  • Async Storage version: 1.6.2
  • React-Native version: 0.61.3
  • Platform tested: None -> It's in Jest tests
  • Logs/Error that are relevant: see Actual behavior
@janhesters janhesters changed the title Mocks broken for plain JS projects Bug: Mocks broken for plain JS projects Nov 5, 2019
@krizzu krizzu added the LEGACY label Nov 6, 2019
@krizzu
Copy link
Member

krizzu commented Nov 6, 2019

@janhesters Whole project uses Flow, not only the Mock implementation. What preset do you use in your jest tests?

@janhesters
Copy link
Author

@krizzu Here is my Jest config:

  "jest": {
    "preset": "@testing-library/react-native",
    "setupFilesAfterEnv": [
      "@testing-library/react-native/cleanup-after-each"
    ],
    "moduleDirectories": [
      "node_modules",
      "utils"
    ]
  }

@krizzu
Copy link
Member

krizzu commented Nov 6, 2019

@janhesters What about babel.config.js? @testing-library/react-native uses react-native preset, which relies on babel module:metro-react-native-babel-preset preset to parse flow in RN app.

@krizzu
Copy link
Member

krizzu commented Nov 29, 2019

@janhesters any update for this?

@janhesters
Copy link
Author

@krizzu babel.config.js is:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
};

@krizzu
Copy link
Member

krizzu commented Nov 29, 2019

Have you tried the step from troubleshooting docs?

@iamolegga
Copy link

Same issue, with same babel config.

@krizzu there is another error text , and it did not help

@krizzu
Copy link
Member

krizzu commented Dec 16, 2019

@iamolegga what error text you see?

@iamolegga
Copy link

The same as topic starter

@krizzu
Copy link
Member

krizzu commented Dec 16, 2019

I'd need more context then, like your Jest setup. Example app is a good place to lookup the config, if you need it.

@krizzu krizzu removed the LEGACY label Apr 12, 2020
@krizzu
Copy link
Member

krizzu commented Oct 24, 2020

Addressed already, mocks are type-free now

@krizzu krizzu closed this as completed Oct 24, 2020
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

3 participants