react-native-encrypted-storage
react-native-encrypted-storage copied to clipboard
TypeError: Cannot read property 'RNEncryptedStorage' of undefined

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
Does it need additional configuration? Please let me know. Thank you.
You might want to check #34, solved it for me.
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;
didnt work for me, tried everything.
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()),
};
});