我正在使用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
当我运行应用程序时,一切正常,我从来没有得到关于存储()未定义或不是函数的错误。只有当我尝试运行测试用例时才会这样。
我在firebase导入中设置了console.dir(firebaseApp);
行,它返回auth()
和database()
,但没有存储空间。如何正确导入/初始化/存在存储?
答案 0 :(得分:14)
添加以下导入
import "firebase/storage";
答案 1 :(得分:2)
答案 2 :(得分:0)
我有同样的问题。另一个可能的解决方案:
import * as firebase from "firebase";
import "firebase/app";
import "firebase/storage";