我在React中使用Firebase auth,并尝试用react-hooks-testing-library测试它。我写的代码起作用了。但是,当我尝试用react-hooks-testing-library进行测试时,我得到了以下错误:
FirebaseError: Firebase:未创建Firebase应用程序“默认”,请调用Firebase App.initializeApp() ( App / No -app)。
以下是我的实际代码:
useAuth.tsx
const defaultValue = {
.. some value here
}
const AuthContext = createContext(defaultValue)
const AuthContextProvider = (props) => {
const auth = useFirebaseAuth()
return (
<AuthContext.Provider
value={auth}
>
{props.children}
</AuthContext.Provider>
);
}
const useAuth = () => {
return useContext(AuthContext)
}
// I will like to test the function and behaviour inside this hooks, useFirebaseAuth() here
export default function useFirebaseAuth() {
const [user, setUser] = useState(null)
const loginWithEmailPassword = (email: string, password: string) => {
const auth = getAuth() // the error point to this line!!!
//this code I followed the Firebase docs
return signInWithEmailAndPassword(auth, email, password)
.then(res => {
}).catch(error => {
})
}
const signUpWithEmailPassword = () => {
}
return {
user,
loginWithEmailPassword,
signUpWithEmailPassword
}
}
export { useAuth, AuthContextProvider }在这个钩子里面将有三个项目,其中user,loginWithEmailPassword,signUpWithEmailPassword
这是我的测试
useAuth.test.tsx
import React from 'react'
import { renderHook, act } from '@testing-library/react-hooks/native'
import useFirebaseAuth from '../useAuth';
// Here I tried to mock the auth function of Firebase
jest.mock('firebase/app', () => {
return {
App: () => ({
app: mockGetApp
}),
auth: () => ({
signInWithEmailAndPassword: mockSignIn,
createUserWithEmailAndPassword: mockSignUp
})
}
})
const mockInitializeFirebase = jest.fn()
const mockSignUp = jest.fn(() => Promise.resolve())
const mockSignIn = jest.fn(() => Promise.resolve())
const mockGetApp = jest.fn()
jest.mock('../../utils/initAuth', () => {
return {
app: mockInitializeFirebase
}
})
describe('useAuth Hooks testing', () => {
test('Login with Email and Password', () => {
const { result } = renderHook(() => useFirebaseAuth())
console.log(result)
//arrange it, confirm the initial state
expect(result.current.user).toBe(null)
expect(typeof result.current.loginWithEmailPassword).toBe('function')
expect(typeof result.current.signUpWithEmailPassword).toBe('function')
const email = 'abc@gmail.com'
const password = '123456'
// here act
act(() => {
// The problem come from this line
result.current.loginWithEmailPassword(email, password)
})
// here assert
expect(mockSignIn).toBeCalledWith(email, password)
})
})因此,当我启动loginWithEmailPassword(email,password)函数时,它一直在显示No Firebase App error。但是在我的项目中,我已经有了这个文件,它已经初始化了Firebase应用程序。
./initFirebase.tsx,在这里已经初始化了应用程序,并在index.tsx中调用了它
import { initializeApp, getApps, getApp } from "firebase/app";
import getEnvVars from '../environment'
const env = getEnvVars()
interface firebaseType {
apiKey: string,
authDomain: string,
projectId: string,
messagingSenderId: string
}
let firebaseConfig: firebaseType;
if (env !== null) {
const { apiKey, authDomain, projectId, messagingSenderId } = env
firebaseConfig = {
apiKey: apiKey,
authDomain: authDomain,
projectId: projectId,
messagingSenderId: messagingSenderId
};
}
export const initFirebase = () => {
if (getApps().length === 0) {
initializeApp(firebaseConfig);
} else {
getApp()
}
}因此,错误只发生在测试中,所以我认为我应该模拟initializeApp函数,并在测试的某个地方调用它。但我不知道该怎么做。
我刚开始在testing领域工作。有经验的人请帮忙。
以下是一个问题:
提前谢谢你。
发布于 2021-09-01 16:50:24
通过这样的模拟来解决这个问题:
const mockSignUp = jest.fn(() => {
return Promise.resolve({
user: {
uid: "fakeuid",
},
});
})
const mockSignIn = jest.fn(() => Promise.resolve({
user: {
uid: "fakeUid"
}
}))
const mockGetAuth = jest.fn()
jest.mock('firebase/auth', () => {
return {
getAuth: () => mockGetAuth,
signInWithEmailAndPassword: () => mockSignIn,
createUserWithEmailAndPassword: () => mockSignUp
}
})注意到这一点:
getAuth: ()=> mockGetAuth中的3个函数,这将在firebase中模拟为getAuth()到我定义的mockGetAuth函数。
然后,在测试中,我可以像这样调用模拟函数:
// here check the mock function
mockSignIn(mockGetAuth, email, password)
expect(mockSignIn).toBeCalledWith(mockGetAuth, email, password)https://stackoverflow.com/questions/69015195
复制相似问题