firebase.storage()不是jest测试用例中的函数 [英] firebase.storage() is not a function in jest test cases
问题描述
我正在使用Jest来测试我的firebase功能。这一切都在浏览器中,因此我与服务器端的firebase没有任何冲突。当我使用 firebase.auth()
或 firebase.database()
时一切正常。当我尝试使用 firebase.storage()
时,我的测试失败。
I am using Jest to test my firebase functions. This is all in the browser, so I don't have any conflicts with firebase on the server side. When I use firebase.auth()
or firebase.database()
everything works fine. When I try to use firebase.storage()
my tests fail.
这是我的firebase导入和初始化:
Here is my firebase import and initialization:
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文件,里面有上传功能:
I have an imageUtils file that has an upload function in it:
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();
});
});
};
我正在尝试为该函数创建一个测试用例,并且每次失败时都会:
And I am trying to create a test case for that function and every time it fails with:
TypeError: _firebase3.firebaseApp.storage is not a function
当我运行应用程序时,一切正常,我从未得到关于存储()未定义或不是函数的错误。只有当我尝试运行测试用例时才会这样。
When I run the app normally everything works fine and I never get errors about storage() being undefined or not a function. It is only when I try to run a test case.
我设置了一个 console.dir(firebaseApp);
在firebase导入中的行,它返回时使用 auth()
和 database()
,但没有存储空间。如何正确导入/初始化/存在存储?
I have set a console.dir(firebaseApp);
line in the firebase import, and it comes back with both auth()
and database()
but no storage. How can I get storage to import/initialize/exist properly?
推荐答案
添加以下导入
import "firebase/storage";
这篇关于firebase.storage()不是jest测试用例中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!