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

Jest Mocks Always Returning Undefined #1078

Open
1 of 5 tasks
bj97301 opened this issue Apr 2, 2024 · 0 comments
Open
1 of 5 tasks

Jest Mocks Always Returning Undefined #1078

bj97301 opened this issue Apr 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@bj97301
Copy link

bj97301 commented Apr 2, 2024

What happened?

i am using async storage and attempting to write some test. I setup the mocks:

  jest.mock('@react-native-async-storage/async-storage', () =>
    require('@react-native-async-storage/async-storage/jest/async-storage-mock'),
  )

but when i try to set then get an item i always get undefined:

 describe(loadSettingsFromLocalStorage.name, () => {
    it('can load settings from local storage', async () => {
      let responseFromSet = AsyncStorage.setItem('settings', 'test')
      console.log(responseFromSet) // undefined
      let responseFromGet = AsyncStorage.getItem('settings')
      console.log(responseFromGet) // undefined

Version

1.18.2

What platforms are you seeing this issue on?

  • Android
  • iOS
  • macOS
  • Windows
  • web

System Information

info Fetching system and libraries information...
System:
  OS: macOS 14.4.1
  CPU: (10) x64 Apple M1 Max
  Memory: 37.95 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 16.20.1
    path: ~/.nvm/versions/node/v16.20.1/bin/node
  Yarn:
    version: 1.22.19
    path: ~/.yarn/bin/yarn
  npm:
    version: 8.19.4
    path: ~/.nvm/versions/node/v16.20.1/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.12.1
    path: /Users/Bryan/.rvm/gems/ruby-2.7.6/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.4
      - iOS 17.4
      - macOS 14.4
      - tvOS 17.4
      - visionOS 1.1
      - watchOS 10.4
  Android SDK:
    API Levels:
      - "30"
      - "31"
      - "32"
      - "33"
      - "33"
      - "34"
    Build Tools:
      - 27.0.3
      - 28.0.3
      - 29.0.3
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 32.0.0
      - 33.0.0
      - 33.0.2
      - 34.0.0
    System Images:
      - android-33 | Google APIs ARM 64 v8a
      - android-34 | Google APIs ARM 64 v8a
      - android-UpsideDownCake-ext5 | Google APIs ARM 64 v8a
      - android-UpsideDownCake-ext5 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2022.3 AI-223.8836.35.2231.10811636
  Xcode:
    version: 15.3/15E204a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.6
    path: /usr/bin/javac
  Ruby:
    version: 2.7.6
    path: /Users/Bryan/.rvm/rubies/ruby-2.7.6/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.72.10
    wanted: 0.72.10
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Steps to Reproduce

put the mocks in the jest setup file then run this test:

 describe(loadSettingsFromLocalStorage.name, () => {
    it('can load settings from local storage', async () => {
      let responseFromSet = AsyncStorage.setItem('settings', 'test')
      console.log(responseFromSet) // undefined
      let responseFromGet = AsyncStorage.getItem('settings')
      console.log(responseFromGet) // undefined
@bj97301 bj97301 added the bug Something isn't working label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant