Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

enter image description here

like image 994
Ganda Rain Panjaitan Avatar asked Jan 20 '26 00:01

Ganda Rain Panjaitan


1 Answers

react-native-get-random-values is a native module, so you need to mock it when running unit tests.

One way to do it is as follow: Go to your __mocks__ folder (or create it in the root of the project if it's not there) and place a file named react-native-get-random-values.js (the name is important) with the following content:

export default {
  getRandomBase64: jest.fn().mockImplementation(() => {
    console.log("getRandomBase64 mock called");
    return "mockedBase64";
  })
};

To learn more about mocking an entire module, read the Jest docs

like image 58
martom Avatar answered Jan 22 '26 18:01

martom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!