Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript firebase.storage()不是jest测试用例中的函数_Javascript_Firebase_Firebase Authentication_Firebase Storage_Jestjs - Fatal编程技术网

Javascript firebase.storage()不是jest测试用例中的函数

Javascript firebase.storage()不是jest测试用例中的函数,javascript,firebase,firebase-authentication,firebase-storage,jestjs,Javascript,Firebase,Firebase Authentication,Firebase Storage,Jestjs,我正在使用Jest测试我的firebase函数。这些都在浏览器中,因此我与服务器端的firebase没有任何冲突。当我使用firebase.auth()或firebase.database()时,一切正常。当我尝试使用firebase.storage()时,我的测试失败 以下是我的firebase导入和初始化: import firebase from 'firebase'; import config from '../config'; export const firebaseApp =

我正在使用Jest测试我的firebase函数。这些都在浏览器中,因此我与服务器端的firebase没有任何冲突。当我使用
firebase.auth()
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)行,返回时既有
auth()
又有
database()
,但没有存储。如何使存储正确导入/初始化/存在

添加以下导入

import "firebase/storage";

看起来这是在一个:
我也有同样的问题。另一种可能的解决办法:

import * as firebase from "firebase";
import "firebase/app";
import "firebase/storage";

这有用吗?我不这么认为。这是指nodejs上的firebase,我严格地在浏览器中使用它。根据firebase文档,从浏览器调用存储仍然是受支持的用例。在我的测试中导入
firebase/storage
?或者导入并用于我的storageRef的初始化?将其导入到storageRef的初始化或声明中,我发现这也修复了我的测试。但是,Firebase提供了与datesWow相关的警告,谢谢!你为我节省了很多时间。这在5.0.4版中就发生了。hsc的答案仍然解决了这个问题。