This question is specifically about expo-secure-store
and jest.
I am using expo-secure-store
to store user's access_token when logging in. It works fine when running on device / simulator. But doesn't work at all in Jest tests; token comes back as undefined. I am able to call the functions like normal.
test('Test SecureStore wrt Jest', async () => {
try {
await SecureStore.setItemAsync(TOKEN, 'randomToken');
const token = await SecureStore.getItemAsync(TOKEN);
console.log(token);
expect(token).toBeDefined();
expect(token).toBe(randomToken);
} catch (error) {
console.log(error);
throw error;
}
}
Package.json
{
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react-native": "10.1.1",
"expo": "^44.0.0",
"jest": "28.1.2",
"jest-expo": "^45.0.1",
"react": "17.0.1",
"react-native": "0.64.3",
"ts-jest": "^28.0.0-next.3"
},
"devDependencies": {
"@types/jest": "^28.1.5",
"@types/react-test-renderer": "^18.0.0",
"jest-environment-jsdom": "^28.1.2",
"react-test-renderer": "17.0.1",
}
}
Here is mock file
jest.mock('expo-secure-store', () => {
const realConstants = jest.requireActual('expo-constants').default
const mockConstants = Object.create(realConstants)
const realExpoSecureStore = jest.requireActual(
'expo-secure-store/src/SecureStore'
)
const realExpoModulesCore = jest.requireActual(
'expo-modules-core/src/NativeModulesProxy'
)
return {
...realConstants,
...realExpoSecureStore,
...realExpoModulesCore,
mockConstants,
NativeModulesProxy: {
ExpoSecureStore: {
setValueWithKeyAsync: jest.fn(),
getValueWithKeyAsync: jest.fn(),
},
},
SecureStore: {
setItemAsync: jest.fn(),
getItemAsync: jest.fn(),
},
}
})