react-native-keychain
react-native-keychain copied to clipboard
jest mock keychain does not work
Hi, I am new to react native, I tried to mock the keychain with jest for testing with your example, but it does not work properly. The value can't store and it returns undefined... Here are my code:
expect.extend({
async toVerifyEncrypt(received) {
// const set = await KeychainHelper.setToKeychain(KeychainHelper.PREVIOUS_USERS, JSON.stringify(received));
// console.log("setting: " + set);
// const pw = await KeychainHelper.getFromKeychain(KeychainHelper.PREVIOUS_USERS);
// console.log(pw);
const username = 'zuck';
const password = 'poniesRgr8';
// Store the credentials
const test = await Keychain.setGenericPassword(username, password);
console.log(test);
try {
// Retrieve the credentials
const credentials = await Keychain.getGenericPassword();
console.log(credentials)
if (credentials) {
console.log(
'Credentials successfully loaded for user ' + credentials.password
);
} else {
console.log('No credentials stored ' + credentials);
}
} catch (error) {
console.log("Keychain couldn't be accessed!", error);
}
await Keychain.resetGenericPassword();
if (true) {
return {
message: () => 'hi',
pass: true
}
}
}
});
mock inside the jest set up file:
jest.mock('react-native-keychain', () => {
return {
setGenericPassword: jest.fn(),
getGenericPassword: jest.fn(),
resetGenericPassword: jest.fn(),
}
});
is there any missing for the set up? Many thanks.
@ray0925 in your jest config file, mock react-native-keychain as follows
jest.mock('react-native-keychain', () => ({ SECURITY_LEVEL_ANY: 'MOCK_SECURITY_LEVEL_ANY', SECURITY_LEVEL_SECURE_SOFTWARE: 'MOCK_SECURITY_LEVEL_SECURE_SOFTWARE', SECURITY_LEVEL_SECURE_HARDWARE: 'MOCK_SECURITY_LEVEL_SECURE_HARDWARE', setGenericPassword: jest.fn().mockResolvedValue(), getGenericPassword: jest.fn().mockResolvedValue(), resetGenericPassword: jest.fn().mockResolvedValue(), getInternetCredentials: jest.fn().mockResolvedValue(true), getSupportedBiometryType: jest.fn().mockResolvedValue(true), setInternetCredentials: jest.fn().mockResolvedValue(true), }))
and you can then mock return values in your test file as follows :
jest.mock('react-native-keychain', () => ({ setInternetCredentials: jest.fn().mockReturnValue(true), getSupportedBiometryType: jest.fn().mockReturnValue(true), ACCESS_CONTROL: { BIOMETRY_CURRENT_SET_OR_DEVICE_PASSCODE: 'mockedpwd' }, ACCESSIBLE: { WHEN_PASSCODE_SET_THIS_DEVICE_ONLY: true }, }))
@chaawlaapooja Not working me as well. Did you that working? Can you please elaborate a bit?
@fahry-mohammed did you make it work?
@ray0925 did you succed in the end?
My mocks are also not working and I get the following error when my test method calls setInternetCredentials
:
TypeError: Cannot read property 'setInternetCredentialsForServer' of undefined
Here is my mock inside my testSetup.js
file...
const keychainMock = {
SECURITY_LEVEL_ANY: 'MOCK_SECURITY_LEVEL_ANY',
SECURITY_LEVEL_SECURE_SOFTWARE: 'MOCK_SECURITY_LEVEL_SECURE_SOFTWARE',
SECURITY_LEVEL_SECURE_HARDWARE: 'MOCK_SECURITY_LEVEL_SECURE_HARDWARE',
setGenericPassword: jest.fn(),
getGenericPassword: jest.fn(),
resetGenericPassword: jest.fn(),
BIOMETRY_TYPE: {
TOUCH_ID: 'TouchID',
FACE_ID: 'FaceID',
FINGERPRINT: 'Fingerprint',
FACE: 'Face',
IRIS: 'Iris',
},
getSupportedBiometryType: jest.fn().mockReturnValue('FaceID'),
setInternetCredentials: jest
.fn(('server', 'username', 'password'))
.mockResolvedValue({ service: 'metamask', storage: 'storage' }),
};
jest.mock('react-native-keychain', () => keychainMock);