如何从@react-native-firebase/auth模拟auth



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

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);
}
};

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

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

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

最新更新