Firebase.storage () is not a function in joke tests

I am using Jest to test my firebase functions. It's all in the browser, so I have no server side firebase conflicts. When I use firebase.auth()

or firebase.database()

everything works fine. When I try to use firebase.storage()

, my tests fail.

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();

      

I have an imageUtils file that has a upload function:

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 this function and every time it fails:

TypeError: _firebase3.firebaseApp.storage is not a function

      

When I run the application everything works fine and I never get errors that storage () is undefined or not a function. Only when I try to run a test case.

I have set a string console.dir(firebaseApp);

in firebase import and it comes back with auth()

and database()

without storage. How can I get the store to import / initialize / exist correctly?

+4


source to share


3 answers


Add the following import



import "firebase/storage";

      

+14


source


It looks like this was fixed in a recent update in the firebase javascript package : update for firebase java package



+2


source


I have the same problem. Another possible solution:

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

      

0


source







All Articles