Search code examples
react-nativeunit-testingjestjsexporeact-native-testing-library

Expo secure-store unavailable in jest tests


This question is specifically about expo-secure-store and jest.

Currently, I am using expo-secure-store to store my JWT when logging in. It works fine when running on emulator however, it doesn't work at all in Jest tests; token comes back as undefined. I am able to call the functions like normal.

Excuse any typos I might have made refactoring this. Calling from tests:

    test('when logging in, given correct credentials, gets response token.', async () => {

        try {
            var token = await SecureStore.getItemAsync("token");
            await SecureStore.setItemAsync('token', 'test');
            token = await SecureStore.getItemAsync('token');
            console.log(token);
            expect(token).toBeDefined();
            expect(token).toBe("test");
        } catch (err) {
            console.log(err);
            throw err;
        }
   }

Question: Does expo-secure-store not load/work without an actual device/emulator?

There are no documentation at all about testing with secure-store and from what I can tell I might have to mock this module.


Solution

  • you have to mock any native module that you use because their implementations live primarily in native code that is intended to be run on ios and android devices. so the same applies here - you should mock expo-secure-store.