首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从@react-native-firebase/auth模拟身份验证

如何从@react-native-firebase/auth模拟身份验证
EN

Stack Overflow用户
提问于 2021-02-26 19:56:07
回答 1查看 484关注 0票数 0

我试图模拟npm库@react-native-firebase/auth中的auth模块,但是,我总是收到这个错误。我试着在下面嘲笑它,但显然它肯定是不正确的,我只是不确定什么是不正确的。

代码语言:javascript
复制
TypeError: Cannot read property 'credential' of undefined
代码语言:javascript
复制
jest.mock('@react-native-firebase/auth', () => ({
  auth: {
    GoogleAuthProvider: {
      credential: jest.fn().mockReturnValue('123'),
    },
  },
}));

jest.mock('@react-native-community/google-signin', () => ({
  GoogleSignin: {
    signIn: jest.fn().mockReturnValue('123'),
  },
}));
代码语言:javascript
复制
    import auth from '@react-native-firebase/auth';
    import {GoogleSignin} from '@react-native-community/google-signin';
    
    export const GoogleLogin = async function (): Promise<void | string> {
      // Get the users ID token
      const {idToken} = await GoogleSignin.signIn();
    
      // Create a Google credential with the token
      const googleCredential = auth.GoogleAuthProvider.credential(idToken);
    
      try {
        auth().signInWithCredential(googleCredential);
      } catch (e) {
        console.log(e);
      }
    };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-26 20:12:51

您正在模拟@react-native-firebase/auth,就好像它导出了firebase (其中auth名称空间是作为firebase.auth访问的),但是您应该模拟它,就像它直接导出auth名称空间一样。

在您当前的模拟中,您定义了auth.auth.GoogleAuthProvider.credential而不是auth.GoogleAuthProvider.credential

代码语言:javascript
复制
jest.mock('@react-native-firebase/auth', () => ({
  GoogleAuthProvider: {
    credential: jest.fn().mockReturnValue('123'),
  },
}));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66385421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档