【问题标题】:Jest Test - The current environment does not support the specified persistence typeJest Test - 当前环境不支持指定的持久化类型
【发布时间】:2018-12-08 00:28:26
【问题描述】:

我使用 jest 在我的 Create React App 上运行一些测试,使用 Firebase Web SDK 和 FirebaseUI

每当我尝试使用 --env=jsdom 运行一些测试时,我都会遇到: The current environment does not support the specified persistence type. 似乎与 Auth 相关

是否有任何已知的相关问题/解决方法?除了测试之外,代码似乎可以正常工作/编译。

Google 帮不上什么忙

这是测试,非常基础。 不得不添加import "firebase/storage";,因为这个:firebase.storage() is not a function in jest test cases

提前致谢

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}]);
    });
});

【问题讨论】:

  • 关于这个问题的任何更新?我也遇到了同样的事情。

标签: firebase jestjs firebaseui


【解决方案1】:

您是否在 Firebase 配置中设置了持久性?测试环境不支持持久化,所以你可以这样做来规避它:

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

【讨论】:

    【解决方案2】:

    我也遇到过这个问题。问题似乎来自 firebaseui 构造函数,特别是我的应用程序中的这行代码:

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

    我为解决这个问题所做的只是在实际使用它来登录时初始化 ui 对象,而不仅仅是作为静态(打字稿)变量。这让我可以运行没有尝试登录的开玩笑测试。

    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
    }
    

    这样,所有不调用 startAuthOnElement 的代码都不会运行 firebaseui 构造函数。这让我可以很好地运行我的笑话测试,并且身份验证仍然可以在应用程序中运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-01
      • 2022-06-14
      • 2021-03-26
      • 1970-01-01
      • 2019-09-22
      • 2020-03-11
      • 2016-12-26
      • 2023-03-09
      相关资源
      最近更新 更多