我试图模拟npm库@react-native-firebase/auth中的auth模块,但是,我总是收到这个错误。我试着在下面嘲笑它,但显然它肯定是不正确的,我只是不确定什么是不正确的。
TypeError: Cannot read property 'credential' of undefinedjest.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);
}
};发布于 2021-02-26 20:12:51
您正在模拟@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'),
},
}));https://stackoverflow.com/questions/66385421
复制相似问题