Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

TypeError: Cannot read property 'RNEncryptedStorage' of undefined #67

Open
gwijaya94 opened this issue Dec 7, 2021 · 4 comments
Open

Comments

@gwijaya94
Copy link

Screen Shot 2021-12-07 at 18 16 25

Hi, i got this error when running yarn test
I just want to replace @react-native-async-storage/async-storage with this library
I'm using latest Ignite cli as RN Boilerplate, and not Expo

"jest": "^25.5.4",
"react-native-encrypted-storage": "^4.0.2",
"react": "17.0.1",
"react-native": "0.64.2",

here the code on storage.test.ts

import { EncryptedStorage as AsyncStorage } from "./async-storage"
import { load, loadString, save, saveString, clear, remove } from "./storage"

// fixtures
const VALUE_OBJECT = { x: 1 }
const VALUE_STRING = JSON.stringify(VALUE_OBJECT)

beforeEach(() => (AsyncStorage.getItem as jest.Mock).mockReturnValue(Promise.resolve(VALUE_STRING)))
afterEach(() => jest.clearAllMocks())

test("load", async () => {
  const value = await load("something")
  expect(value).toEqual(JSON.parse(VALUE_STRING))
})

test("loadString", async () => {
  const value = await loadString("something")
  expect(value).toEqual(VALUE_STRING)
})

test("save", async () => {
  await save("something", VALUE_OBJECT)
  expect(AsyncStorage.setItem).toHaveBeenCalledWith("something", VALUE_STRING)
})

test("saveString", async () => {
  await saveString("something", VALUE_STRING)
  expect(AsyncStorage.setItem).toHaveBeenCalledWith("something", VALUE_STRING)
})

test("remove", async () => {
  await remove("something")
  expect(AsyncStorage.removeItem).toHaveBeenCalledWith("something")
})

test("clear", async () => {
  await clear()
  expect(AsyncStorage.clear).toHaveBeenCalledWith()
})

or does mock file have affect too? on my last project, this doesnt affect me at all
Screen Shot 2021-12-07 at 18 19 03

Does it need additional configuration? Please let me know.
Thank you.

@Meknassih
Copy link

You might want to check #34, solved it for me.

@nacholopeztoral
Copy link

Posting this here in case anybody has this issue too.

I found 2 solutions that fixed this issue for me, although the first solution did affect other tests in the project:

  • Mocking the entire react-native NativeModules, again, if you have other tests in your project depending on other mocks this might affect them 🤦‍♂️.
// src/__mocks__/react-native.js
Or perhaps (depending on project config)
//tests/__mocks__/react-native.js

module.exports = {
    NativeModules: {
        RNEncryptedStorage: {
            setItem: jest.fn(() => Promise.resolve()),
            getItem: jest.fn(() => Promise.resolve('{ "foo": 1 }')),
            removeItem: jest.fn(() => Promise.resolve()),
            clear: jest.fn(() => Promise.resolve())
        }
    }
}
  • Mocking only the library as react-native-encrypted-storage
__mocks__/react-native-encrypted-storage.js

const RNEncryptedStorage = {
  setItem: jest.fn(() => Promise.resolve()),
  getItem: jest.fn(() => Promise.resolve('{ "foo": 1 }')),
  removeItem: jest.fn(() => Promise.resolve()),
  clear: jest.fn(() => Promise.resolve()),
};

export default RNEncryptedStorage;

@shahriar0247
Copy link

didnt work for me, tried everything.

@Myst6971
Copy link

Myst6971 commented May 31, 2023

Please give it a try with the following:

__mocks__/reactNativeEncryptedStorage.js

jest.mock('react-native-encrypted-storage', () => {
  return {
    setItem: jest.fn(() => Promise.resolve()),
    getItem: jest.fn(() => Promise.resolve('{ "foo": 1 }')),
    removeItem: jest.fn(() => Promise.resolve()),
    clear: jest.fn(() => Promise.resolve()),
  };
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants