react-native-get-random-values icon indicating copy to clipboard operation
react-native-get-random-values copied to clipboard

Error Cannot read property 'getRandomBase64' of undefined when running test file

Open gandarain opened this issue 4 years ago • 4 comments

I try use package uuid and react native get random values to create random uuid. Everything is fine and working. But when I try run my test file using jest, and I get an error Cannot read property 'getRandomBase64' of undefined. How to solve this error?

Thanks.

Screen Shot 2020-10-02 at 10 37 22

gandarain avatar Oct 02 '20 03:10 gandarain

i'm getting this too.

paulmars avatar Oct 05 '20 20:10 paulmars

I was having this problem as well while using jest to test my Entrypoint file.

My Entrypoint had the following for import Expo [email protected] to support calling [email protected]:

import 'react-native-get-random-values';

Because my Entrypoint file was trying to import the react-native-get-random-values for shallow rendering, it was throwing the Cannot read property 'getRandomBase64' of undefined error when getting tested since it wasn't being set up correctly during the test.

I ended up solving this by mocking out the function call for getRandomBase64 in my Entrypoint test file like this:

jest.mock('react-native-get-random-values', () => ({
  getRandomBase64: jest.fn(),
}));

aav7fl avatar Oct 14 '20 13:10 aav7fl

@aav7fl thanks, mocking helped

SnaiNeR avatar Oct 22 '20 01:10 SnaiNeR

I was having the same problem.

Mocked it a bit differently though.

jest.mock('react-native-get-random-values/getRandomBase64', (arr='bla') => (  
  Buffer.from(arr, 'base64').toString('ascii')
))

jpaulomotta avatar Oct 23 '20 13:10 jpaulomotta