Closed
Description
What happened?
I'm using the library mock to unit test some logic I've written that uses mergeItem
. My function and test are:
const mergeProperties = async (properties: { [a: string]: any }) => {
const propsStr = JSON.stringify(properties)
await AsyncStorage.mergeItem(MY_KEY_CONST, propsStr)
}
it('should initialise the property correctly', async () => {
const properties = { property: 'hopefully set' }
await mergeProperties(properties)
const savedValue = await AsyncStorage.getItem(MY_KEY_CONST)
expect(savedValue).to.eql('{"property":"hopefully set"}')
})
This tests the case where MY_KEY_CONST
has not yet been set.
When I test my code on the simulator, it seems to initialise the value correctly, so I assume I don't need to first initialise with setItem like in the docs example.
However, the unit test crashes with
SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
The line that crashes in the mock is this one, because asMock.__INTERNAL_MOCK_STORAGE__[key]
is undefined.
Is the mock incorrect? Should line 100 look like const oldValue = JSON.parse(asMock.__INTERNAL_MOCK_STORAGE__[key] || '{}');
? Or is there a problem with what I'm trying to do?
Version
1.15.9
What platforms are you seeing this issue on?
- Android
- iOS
- macOS
- Windows
- web
System Information
System:
OS: macOS 11.6
CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
Memory: 22.23 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.24.1 - /usr/local/Cellar/nvm/0.30.1/versions/node/v10.24.1/bin/node
Yarn: 1.17.3 - ~/.yarn/bin/yarn
npm: 6.14.12 - /usr/local/Cellar/nvm/0.30.1/versions/node/v10.24.1/bin/npm
Watchman: Not Found
Managers:
CocoaPods: 1.11.2 - /Users/me/.rvm/rubies/ruby-2.6.3/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4
Android SDK:
API Levels: 23, 27, 28, 29, 30
Build Tools: 28.0.3, 29.0.0, 29.0.2, 30.0.2
System Images: android-28 | Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-R | Google APIs Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5791312
Xcode: 12.5.1/12E507 - /usr/bin/xcodebuild
Languages:
Java: 10.0.1 - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.13.1 => 16.13.1
react-native: 0.63.4 => 0.63.4
react-native-macos: Not Found
npmGlobalPackages:
*react-native*: Not Found
Steps to Reproduce
Use the mock function and unit test above