firebase.storage() 不是玩笑测试用例中的函数

2024-05-13

我正在使用 Jest 来测试我的 firebase 功能。这一切都在浏览器中进行,因此我与服务器端的 firebase 没有任何冲突。当我使用firebase.auth() or firebase.database()一切正常。当我尝试使用时firebase.storage()我的测试失败了。

这是我的 firebase 导入和初始化:

import firebase from 'firebase';
import config from '../config';

export const firebaseApp = firebase.initializeApp(config.FIREBASE_CONFIG);
export const firebaseAuth = firebaseApp.auth();
export const firebaseDb = firebaseApp.database();

我有一个 imageUtils 文件,其中有上传功能:

import { firebaseApp } from './firebase';

export const uploadImage = (firebaseStoragePath, imageURL) => {
  return new Promise((resolve, reject) => {
    // reject if there is no imagePath provided
    if (!firebaseStoragePath) reject('No image path was provided. Cannot upload the file.');

    // reject if there is no imageURL provided
    if (!imageURL) reject('No image url was provided. Cannot upload the file');

    // create the reference
    const imageRef = firebaseApp.storage().ref().child(firebaseStoragePath);

    let uploadTask;
    // check if this is a dataURL
    if (isDataURL(imageURL)) {
      // the image is a base64 image string
      // create the upload task
      uploadTask = imageRef.putString(imageURL);
    } else {
      // the image is a file
      // create the upload task
      uploadTask = imageRef.put(imageURL);
    }

    // monitor the upload process for state changes
    const unsub = uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
      (snapshot) => {
        // this is where we can check on progress
      }, (error) => {
        reject(error.serverResponse);
        unsub();
      }, () => {
        // success function
        resolve(uploadTask.snapshot.downloadURL);
        unsub();
      });
  });
};

我正在尝试为该函数创建一个测试用例,每次它失败时都会显示:

TypeError: _firebase3.firebaseApp.storage is not a function

当我正常运行应用程序时,一切正常,并且我从未收到有关 storage() 未定义或不是函数的错误。只有当我尝试运行测试用例时才会出现这种情况。

我已经设置了一个console.dir(firebaseApp);firebase 导入中的行,它会同时返回auth() and database()但没有存储空间。如何让存储正确导入/初始化/存在?


添加以下导入

import "firebase/storage";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

firebase.storage() 不是玩笑测试用例中的函数 的相关文章

随机推荐