Open itsramiel opened 3 months ago
Is there any reason why const module = require('@react-native-async-storage/async-storage')?.default as AsyncStorageStatic;
is not imported as an esm import import AsyncStorage from 'react-native-async-storage/async-storage'
and then assigning AsyncStorage to module?
Hi @itsramiel, Thanks for raising the issue, this is a duplicate of #12829 Could you try adding a mock as mentioned on the linked issue
@itsramiel, wanted to follow up and see if you had a chance to try adding a mock as mentioned in the issue referenced in @ashwinkumar6's comment. Let us know if you're still having this issue and if we can help, thanks!
Yeah I already use the following mock:
jest.mock('@aws-amplify/react-native', () => {
const original = jest.requireActual('@aws-amplify/react-native');
return {
...original,
loadAsyncStorage: jest.fn(),
loadNetInfo: jest.fn(() => ({
addEventListener: jest.fn(),
})),
};
});
but I was wondering if there is a plan to handle that internally
Before opening, please confirm:
JavaScript Framework
React Native
Amplify APIs
DataStore
Amplify Version
v6
Amplify Categories
Not applicable
Backend
None
Environment information
Describe the bug
When testing a react native app with jest, I get the following error message:
Ensure
@react-native-async-storage/async-storageis installed and linked.
with the source being
at loadAsyncStorage (node_modules/@aws-amplify/react-native/src/moduleLoaders/loadAsyncStorage.ts:22:15)
The issue is that when loading the module, the module is loaded with
require().default
: https://github.com/aws-amplify/amplify-js/blob/2b71e29d50204f037b1dae2007291c54d1ec8c19/packages/react-native/src/moduleLoaders/loadAsyncStorage.ts#L10-L11But the mock that is shipped with
react-native-async-storage/async-storage
is a common js object with no default property, sorequire('@react-native-async-storage/async-storage')?.default
is always undefind in jest, butrequire('@react-native-async-storage/async-storage')
is definedExpected behavior
I expect to not get the error
Ensure
@react-native-async-storage/async-storageis installed and linked.
since it is installed and mocked properly according to the library docs.Reproduction steps
yarn
yarn test
and check the errorCode Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response