You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mocking firebase.initializeApp() and firebase.auth() using JavaScript/Jest
import*asfirebasefrom'firebase'constonAuthStateChanged=jest.fn()constgetRedirectResult=jest.fn(()=>{returnPromise.resolve({user: {displayName: 'redirectResultTestDisplayName',email: '[email protected]',emailVerified: true}})})constsendEmailVerification=jest.fn(()=>{returnPromise.resolve('result of sendEmailVerification')})constsendPasswordResetEmail=jest.fn(()=>Promise.resolve())constcreateUserWithEmailAndPassword=jest.fn(()=>{returnPromise.resolve('result of createUserWithEmailAndPassword')})constsignInWithEmailAndPassword=jest.fn(()=>{returnPromise.resolve('result of signInWithEmailAndPassword')})constsignInWithRedirect=jest.fn(()=>{returnPromise.resolve('result of signInWithRedirect')})constinitializeApp=jest.spyOn(firebase,'initializeApp').mockImplementation(()=>{return{auth: ()=>{return{
createUserWithEmailAndPassword,
signInWithEmailAndPassword,currentUser: {
sendEmailVerification
},
signInWithRedirect
}}}})jest.spyOn(firebase,'auth').mockImplementation(()=>{return{
onAuthStateChanged,currentUser: {displayName: 'testDisplayName',email: '[email protected]',emailVerified: true},
getRedirectResult,
sendPasswordResetEmail
}})firebase.auth.FacebookAuthProvider=jest.fn(()=>{})firebase.auth.GoogleAuthProvider=jest.fn(()=>{})
example usage
test('<insert your method name> that calls firebase.auth().getRedirectResult',async()=>{constresult=awaityourModuleName.getRedirectResult()// (i.e. your method that calls firebase.auth().getRedirectResult)expect(result).toEqual({user: {displayName: 'redirectResultTestDisplayName',email: '[email protected]',emailVerified: true}})expect(firebase.auth).toHaveBeenCalled()expect(getRedirectResult).toHaveBeenCalled()})