-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest cannot read property setItem #124
Comments
I had the same issue, i made a PR, which is already merged. Should be fixed in version 5.2.6. Could you check your version? |
Ok I was on 5.2.4, I upgrade this dependency but that change nothing. Did I miss something ? |
I'm still having this problem with the version "5.3.0" |
Same issue version 5.4.0, trying to make a mock.
|
Similar issue with 5.4.1
|
I am also having this issue when attempting:
TypeError: _reactNativeSensitiveInfo.default.getItem is not a function |
Well in my case
this works for me... |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I'm still having this issue with 6.0.0-alpha.6 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
You can create a mock for it. I created a simple one that satisfies my needs without needing to mock every function. Got some inspiration in @react-native-async-storage/async-storage mock. __mocks__/react-native-sensitive-info.js: const asMock = {
__INTERNAL_MOCK_STORAGE__: {},
getItem: jest.fn(async (key, options) => {
const storeName = _getStoreName(options);
const result = asMock.__INTERNAL_MOCK_STORAGE__[storeName]
? asMock.__INTERNAL_MOCK_STORAGE__[storeName][key]
: undefined;
return result;
}),
setItem: jest.fn(async (key, value, options) => {
const storeName = _getStoreName(options);
if (asMock.__INTERNAL_MOCK_STORAGE__[storeName] === undefined) {
asMock.__INTERNAL_MOCK_STORAGE__[storeName] = {};
}
asMock.__INTERNAL_MOCK_STORAGE__[storeName][key] = value;
return null;
}),
};
const _getStoreName = options =>
options.sharedPreferencesName || options.keychainService || 'default';
module.exports = asMock; And when I need to mock a specific result in a test: it('should mock', () => {
const sensitiveInfoMock = jest.spyOn(RNSInfo, 'getItem');
sensitiveInfoMock.mockImplementation(async () => 'specific value');
// ... test
sensitiveInfoMock.mockRestore();
}); If someone can mock all functions using the "options" object, I bet a PR would be welcome. PS: The code above won't distinguish iOS and Android for |
Blind code so take it for what it is... // __mocks__/react-native-sensitive-info.js
class RNSInfo {
static stores = new Map()
static getServiceName(o) {
return o.sharedPreferencesName
|| o.keychainService
|| 'default'
}
static validateString(s){
if (typeof s !== 'string') throw new Error('Invalid string:', s)
}
static getItem = jest.fn(async (k, o) => {
RNSInfo.validateString(k)
const serviceName = RNSInfo.getServiceName(o)
const service = RNSInfo.stores.get(serviceName)
if(service) return service.get(k) || null
})
static getAllItems = jest.fn(async (o) => {
const serviceName = RNSInfo.getServiceName(o)
const service = RNSInfo.stores.get(serviceName)
const mappedValues = []
if(service?.size){
for(const [k, v] of service.entries()){
mappedValues.push({key: k, value: v, service: serviceName})
}
}
return mappedValues
})
static setItem = jest.fn(async (k, v, o) => {
RNSInfo.validateString(k)
RNSInfo.validateString(v)
const serviceName = getServiceName(o)
let service = RNSInfo.stores.get(serviceName)
if (!service){
RNSInfo.stores.set(serviceName, new Map())
service = RNSInfo.stores.get(serviceName)
}
service.set(k, v)
return null
})
static deleteItem = jest.fn(async (k, o) => {
RNSInfo.validateString(k)
const serviceName = RNSInfo.getServiceName(o)
const service = RNSInfo.stores.get(serviceName)
if (service) service.delete(k)
return null
})
static hasEnrolledFingerprints = jest.fn(async () => true)
static setInvalidatedByBiometricEnrollment = jest.fn()
// "Touch ID" | "Face ID" | false
static isSensorAvailable = jest.fn(async () => 'Face ID')
}
module.exports = RNSInfo |
For some reason writing the mocks as So I added it inside jest.mock('react-native-sensitive-info', () => {
// your mock here
}); |
Had to change the contents of
The import from the corresponding |
I have a little problem with Jest. When I try to test a function using SInfo jest crash and says :
Do you have any hint ?
The text was updated successfully, but these errors were encountered: