react-native-async-storage / async-storage

An asynchronous, persistent, key-value storage system for React Native.
https://react-native-async-storage.github.io/async-storage/
MIT License
4.61k stars 458 forks source link

Jest Mocks Always Returning Undefined #1078

Closed bj97301 closed 3 weeks ago

bj97301 commented 3 months ago

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?

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
github-actions[bot] commented 1 month ago

This issue has been marked as stale due to inactivity. Please respond or otherwise resolve the issue within 7 days or it will be closed.