Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase.storage() is not a function in jest test cases

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.

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 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.

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?

like image 777
Bonitis Avatar asked Dec 02 '22 13:12

Bonitis


2 Answers

Add the following import

import "firebase/storage";
like image 84
Hans Avatar answered Dec 06 '22 11:12

Hans


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

like image 33
Mike Bifulco Avatar answered Dec 06 '22 10:12

Mike Bifulco