Firebase -使用Jest进行测试时抛出错误“内部Assert失败:应为类定义”

eufgjt7s  于 2022-11-17  发布在  Jest
关注(0)|答案(1)|浏览(173)

bounty将在3天后过期。回答此问题可获得+100声望奖励。Dominic Scheirlinck希望吸引更多人关注此问题。

我正在用Jest和Enzyme测试我的React应用程序。我也使用Firebase,当我尝试为使用带有Firebase登录的Header组件的页面编写测试时,我得到以下错误:
测试套件运行失败

INTERNAL ASSERTION FAILED: Expected a class definition

  42 | firebase.initializeApp(config);
  43 |
> 44 | export const auth = firebase.auth();
     |                              ^
  45 | export const firestore = firebase.firestore();
  46 |
  47 | const provider = new firebase.auth.GoogleAuthProvider();

  at debugFail (node_modules/@firebase/auth/src/core/util/assert.ts:256:9)
  at debugAssert (node_modules/@firebase/auth/src/core/util/assert.ts:271:5)
  at Object._getInstance (node_modules/@firebase/auth/src/core/util/instantiator.ts:34:3)
  at new CompatPopupRedirectResolver (node_modules/@firebase/auth-compat/src/popup_redirect.ts:29:9)
  at _getInstance (node_modules/@firebase/auth/src/core/util/instantiator.ts:45:14)
  at AuthImpl.Object.<anonymous>.AuthImpl._initializeWithPersistence (node_modules/@firebase/auth/src/core/auth/auth_impl.ts:121:37)
  at _initializeAuthInstance (node_modules/@firebase/auth/src/core/auth/initialize.ts:86:8)
  at node_modules/@firebase/auth/src/core/auth/register.ts:87:11
  at Component.instanceFactory (node_modules/@firebase/auth/src/core/auth/register.ts:90:10)
  at Provider.Object.<anonymous>.Provider.getOrInitializeService (node_modules/@firebase/component/src/provider.ts:318:33)
  at Provider.Object.<anonymous>.Provider.initialize (node_modules/@firebase/component/src/provider.ts:242:27)
  at new Auth (node_modules/@firebase/auth-compat/src/auth.ts:68:31)
  at Component.instance.INTERNAL.registerComponent.component.Component.setServiceProps.ActionCodeInfo.Operation.EMAIL_SIGNIN [as instanceFactory] (node_modules/@firebase/auth-compat/index.ts:82:16)
  at Provider.Object.<anonymous>.Provider.getOrInitializeService (node_modules/@firebase/component/src/provider.ts:318:33)
  at Provider.Object.<anonymous>.Provider.getImmediate (node_modules/@firebase/component/src/provider.ts:115:21)
  at FirebaseAppImpl.Object.<anonymous>.FirebaseAppImpl._getService (node_modules/@firebase/app-compat/src/firebaseApp.ts:138:21)
  at FirebaseAppImpl.firebaseAppImpl.<computed> [as auth] (node_modules/@firebase/app-compat/src/firebaseNamespaceCore.ts:185:29)
  at Object.auth (node_modules/@firebase/app-compat/src/firebaseNamespaceCore.ts:166:59)
  at Object.<anonymous> (src/firebase/firebase.utils.js:44:30)
  at Object.<anonymous> (src/components/header/header.component.jsx:6:1)
  at Object.<anonymous> (src/pages/finder/finder.component.jsx:5:1)
  at Object.<anonymous> (src/pages/finder/finder.test.js:3:1)

这是我的Firebase配置文件:

import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
import 'firebase/compat/auth';

const config = {
    ...
  }

export const createUserProfileDocument = async (userAuth, additionalData) => {
  if (!userAuth) return;

  const userRef = firestore.doc(`users/${userAuth.uid}`); 
  
  const snapShot = await userRef.get();

  if(!snapShot.exist) {
    const { displayName, email } = userAuth;
    const createdAt = new Date();

    try {
      await userRef.set({
        displayName,
        email,
        createdAt,
        ...additionalData
      })
    } catch (error) {
      console.log("error creating user", error.message);
    }
  }

  return userRef;

}

firebase.initializeApp(config);

export const auth = firebase.auth();
export const firestore = firebase.firestore();

const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({ prompt: 'select_account' });
export const signInWithGoogle = () => auth.signInWithPopup(provider);

export default firebase;

我现在使用的是Firebase 9.8.0。
有没有什么想法,我错过了什么?应用程序本身和认证流程工作正常。

oyjwcjzk

oyjwcjzk1#

我遇到了同样的问题,这可能是使用紧凑版firebase的问题。只需要使用模块化版本。import

import { getAuth,signInWithPopup } from 'firebase/auth';

而不是
export const auth = firebase.auth();
从getAuth函数获取auth,如下所示
export const auth = getAuth();
然后您应该从'firebase/auth'导入signIn或signUp方法,并将auth作为第一个参数传递。

export const signInWithGoogle = () => auth.signInWithPopup(provider);

应将其替换为:

export const signInWithGoogle = () => signInWithPopup(auth,provider)

记住替换signInWithEmailAndPassword或createUserWithEmailAndPassword或firebase/auth的其他方法,如果您已经从firebase/auth导入它们,并将auth作为第一个参数传递,这样您的应用和测试将无错误运行。

相关问题