我试图模拟 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/