react-native-keychain icon indicating copy to clipboard operation
react-native-keychain copied to clipboard

jest mock keychain does not work

Open ray0925 opened this issue 3 years ago • 5 comments

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 avatar May 05 '21 11:05 ray0925

@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 avatar May 18 '21 18:05 chaawlaapooja

@chaawlaapooja Not working me as well. Did you that working? Can you please elaborate a bit?

fahry-mohammed avatar Jul 13 '21 10:07 fahry-mohammed

@fahry-mohammed did you make it work?

jalmonacidOA avatar Aug 03 '21 21:08 jalmonacidOA

@ray0925 did you succed in the end?

SasClaudiuCristian avatar Sep 13 '22 22:09 SasClaudiuCristian

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);

owencraston avatar Feb 28 '23 06:02 owencraston