1

There is a npm package we made for our team usage and inside that we are using this uuidjs. Now I have installed this custom npm package on my Create-React-App which am testing with testing-library.

When I test the component which imports the files from this custom package which has uuidjs I get following error:

crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported

I know there are answers to this questions is install react-native-get-random-values. But my confusion is can I install this in CRA which uses React.js? Is react-native has anything to do with it or its independent?

Nikita Kumari
  • 309
  • 3
  • 7
  • 1
    Does this answer your question? [How to use Jest to test functions using crypto or window.msCrypto](https://stackoverflow.com/questions/52612122/how-to-use-jest-to-test-functions-using-crypto-or-window-mscrypto) – Wing Sep 09 '21 at 12:59
  • If you update jest to v29 (https://jestjs.io/docs/upgrading-to-jest29#jsdom-upgrade) that issue is resolved. We don't need to mock crypto anymore – Nacho Zullo Jun 15 '23 at 12:40

1 Answers1

3

Problem

When you run your test, I suspect you are running it in a test environment running in Node.js. crypto.getRandomValues is only available from web APIs. It does not exist in Node under crypto, see Node's documentation on crypto. Node does provide a web crypto API which has getRandomValues but the uuid library won't be aware of it.

Solution

You could mock or polyfill crypto.getRandomValues yourself, see: How to use Jest to test functions using crypto or window.msCrypto.

Wing
  • 8,438
  • 4
  • 37
  • 46
  • 1
    If you update jest to v29 (https://jestjs.io/docs/upgrading-to-jest29#jsdom-upgrade) that issue is resolved. We don't need to mock crypto anymore – Nacho Zullo Jun 15 '23 at 12:41