Matthieu Veillon
Matthieu Veillon

Reputation: 180

Jest Test - The current environment does not support the specified persistence type

I use jest to run some test on my Create React App with Firebase Web SDK coupled with FirebaseUI

Whenever I try to run some tests with --env=jsdom - I run into : The current environment does not support the specified persistence type. seems related to Auth

Are there any known related issue/workaround ? the code seems to work/compile properly aside from the tests.

Google didn't help much

Here is the test, pretty basic. HAd to add import "firebase/storage"; because of this : firebase.storage() is not a function in jest test cases

Thanks in advance

import React from "react";

import Enzyme from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import "firebase/storage";
import {filterIngredientsToRemove} from "./shoppingList-reducer";

Enzyme.configure({adapter: new Adapter()});

describe("", () => {
    let shoppingList;
    let recipeId;

    beforeEach(() => {
        shoppingList = {
            shoppingListItems: {
                "1234": {ingredientId: 987, name: "patate", recipeId: 1234},
                "2345": {ingredientId: 987, name: "patate", recipeId: 5432}
            },
            shoppingListRecipes: {
                "1234": {portion: 3}
            }
        };

        recipeId = 1234;
    });

    it("should filter out the shoppinglistItems with the provided recipeId", () => {
        const result = filterIngredientsToRemove(recipeId, shoppingList.shoppingListItems);
        expect(result).toEqual([{ingredientId: 987, name: "patate", recipeId: 1234}]);
    });
});

Upvotes: 8

Views: 1776

Answers (2)

patrick haggerty
patrick haggerty

Reputation: 106

I ran into this issue too. The problem seems to come from the firebaseui constructor, specifically this line of code in my app:

new firebaseui.auth.AuthUI(this.firebase.auth())

What I did to solve it was initialize the ui object only when actually using it to sign on, not just as a static (typescript) variable. This let me run jest tests that didn't try to sign on just fine.

private static ui: firebaseui.auth.AuthUI | undefined

static startAuthOnElement (selectorToUse: string, onSignedIn: () => void) {
    if (this.ui === undefined) this.ui = new firebaseui.auth.AuthUI(this.firebase.auth())
// more code
}

This way all the code that doesn't call startAuthOnElement never runs the firebaseui constructor. This lets me run my jest tests just fine and auth still works in the app.

Upvotes: 1

Pete Tyldesley
Pete Tyldesley

Reputation: 66

Are you setting persistence in your firebase config? Persistence is not supported in the test environment, so you can do something like this to circumvent it:

firebase.auth().setPersistence(process.env.NODE_ENV === 'test' 
  ? firebase.auth.Auth.Persistence.NONE 
  : firebase.auth.Auth.Persistence.LOCAL);

Upvotes: 5

Related Questions