Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Mocking react-native-keychain, undefined is not a function

$
0
0

I'm trying to write a test for my helper Functions which uses the react-native-keychain library.

I accordingly to the documentation mocked the library.

I added a directory react-native-keychain to the __mocks__ directory and added the following code:

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().mockResolvedValue(),getGenericPassword: jest.fn().mockResolvedValue(),resetGenericPassword: jest.fn().mockResolvedValue(),};export default keychainMock;

My helper function just calls the getGenericPassword method.

{getGenericPassword().then(genericPassword => {  if (genericPassword) {    setUser({jwt: genericPassword.password, authorities: []});  }});}

And my test looks like this:

describe('AuthProvider', () => {test('Snapshot', () => {const component = renderer.create(<AuthProvider><MockComponent /></AuthProvider>,);expect(component.toJSON()).toMatchSnapshot();});

I get the following error:

TypeError: (0 , _reactNativeKeychain.getGenericPassword) is not a function

Package.json:

"react": "17.0.2","react-native": "0.68.1","react-native-keychain": "^8.0.0","jest": "26.6.3",

So the mocks seem to work since its not calling the real method, but the jest.fn() neither.

What am i missing?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>