【问题标题】:Error Cannot read property 'getRandomBase64' of undefined when running test file运行测试文件时出现错误无法读取未定义的属性“getRandomBase64”
【发布时间】:2020-10-02 03:43:38
【问题描述】:

我尝试使用包 uuid 并响应本机获取随机值来创建随机 uuid。一切都很好并且工作正常。但是当我尝试使用 jest 运行我的测试文件时,我收到一个错误无法读取未定义的属性“getRandomBase64”。 如何解决这个错误?

谢谢。

【问题讨论】:

    标签: react-native jestjs


    【解决方案1】:

    react-native-get-random-values 是一个原生模块,所以在运行单元测试时需要模拟它。

    一种方法如下:转到您的 __mocks__ 文件夹(如果不存在,则在项目的根目录中创建它)并放置一个名为 react-native-get-random-values.js 的文件(名称很重要)以下内容:

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

    要了解有关模拟整个模块的更多信息,请阅读Jest docs

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,但上面的回答都不适合我。

      在我的测试文件顶部添加这个模拟解决了这个问题:

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

      aav7flthis GitHub issue 上找到的解决方案。

      我知道这个问题已经 5 个月了,但它可能对其他人有所帮助。

      【讨论】:

        【解决方案3】:

        尝试在根应用中导入“react-native-get-random-values”

        import { AppRegistry } from 'react-native';
        import App from './App';
        import { name as appName } from './app.json';
        import '@react-native-firebase/crashlytics';
        import 'react-native-get-random-values';
        console.disableYellowBox = true
        AppRegistry.registerComponent(appName, () => App);
        

        这是链接问题https://github.com/react-native-webview/react-native-webview/issues/1312

        【讨论】:

        • 感谢您的解决方案,但它仍然是错误的。当我运行我的测试文件时出现这个错误。
        • 同样的错误兄弟,“无法读取未定义的属性 'getRandomBase64'”
        • 你的 react-native-get-random-values 版本是什么?
        • 谢谢兄弟,但您的解决方案仍然无效。好消息是我可以通过在我的 package.json 文件中的转换忽略模式中添加 react native 获取随机值和 uuid 来修复该错误。 "transformIgnorePatterns": [ "/node_modules/(?!native-base|@react-native-community/netinfo|@react-native-community/async-storage|@react-native-community/geolocation|react-native-permissions |uuid|react-native-get-random-values)/" ]
        • 如果您在正常运行您的应用程序时出现问题,那么这就是解决方案。但是,如果在运行测试时出现问题,我可以通过在项目根目录中的 _mocks 文件夹中按照 martom 的建议进行模拟来解决问题。
        【解决方案4】:

        最后我可以通过在我的 package.json 文件中添加 react native get random values 和 uuid 来修复这个错误,就像这样。

        "transformIgnorePatterns": [
              "/node_modules/(?!native-base|@react-native-community/netinfo|@react-native-community/async-storage|@react-native-community/geolocation|react-native-permissions|uuid|react-native-get-random-values)/"
            ]
        

        【讨论】:

        • 我没有得到这个来解决我的问题。很高兴它对你有效。它应该做什么?允许转译尚未与转译源一起发布的 3d 派对模块? IE。它不是以 js 发布的,而是以 ts 发布的?但是我看到 uuid 和 react-native-get-random-values 都作为 js 发布(只是查看我的 node_modules 文件夹)。如果您能澄清这是做什么的,那就太好了。
        • @Tejpbit 请看我上面的回答,这应该是解决这个问题的方法。
        • 是的。谢谢!我应该澄清一下。当我看到这不起作用时,我确实采用了您的解决方案。
        • 这对我没有帮助。
        猜你喜欢
        • 2021-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        相关资源
        最近更新 更多