我们有一个npm包供我们的团队使用,里面我们正在使用这个uuidjs
。现在我已经在我的Create-React-App上安装了这个自定义的npm包,它正在使用testing-library
进行测试。
当我测试从这个具有uuidjs
的自定义包中导入文件的组件时,我得到以下错误:
crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported
我知道这个问题的答案是install react-native-get-random-values
。但我的困惑是,我可以在使用React.js的CRA中安装它吗?react-native
和它有什么关系,还是它的独立性?
发布于 2021-09-09 12:58:41
问题
当您运行测试时,我怀疑您是在运行Node.js的测试环境中运行它。crypto.getRandomValues
仅在web API中可用。在crypto
下的Node中不存在,请参考Node的documentation on crypto
。Node确实提供了一个具有getRandomValues
的web crypto API,但是uuid
库不会知道它。
解决方案
您可以自己模拟或填充crypto.getRandomValues
,请参阅:How to use Jest to test functions using crypto or window.msCrypto。
https://stackoverflow.com/questions/69118216
复制相似问题