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

标签 reactjs firebase react-native unit-testing jestjs

我试图模拟 npm 库 @react-native-firebase/auth 中的 auth 模块,但是,我不断收到此错误。我尝试在下面 mock 它,但显然它一定是不正确的,我只是不确定什么是不正确的。

TypeError: Cannot read property 'credential' of undefined
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'),
  },
}));
    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);
      }
    };

最佳答案

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

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

jest.mock('@react-native-firebase/auth', () => ({
  GoogleAuthProvider: {
    credential: jest.fn().mockReturnValue('123'),
  },
}));

关于reactjs - 如何从 @react-native-firebase/auth 模拟身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66385421/

相关文章:

reactjs - redux-thunk 和处理调度结果中的异常

javascript - Firebase JS API 身份验证 - 帐户存在于不同的凭据

reactjs - 重构 AutoHeightWebView 库以获取在单独的浏览器中打开的超链接

react-native - 在android上的react-native中增加 slider 拇指的大小

reactjs - EsLint 错误解释 no-unused-vars 中的导入语句

reactjs - 从 React 前端获取 Azure AD 的访问 token

ReactJS - 使用 ES6 生成器的嵌套元素的递归方法

javascript - 在 div 或 span 中 react 和 onClick 以及可能的缺点

firebase - Firestore 中的 getDocuments() 和 snapshots() 有什么区别?

firebase - 带有 Firebase 实时数据库和 futurebuilder 的 Gridview.builder