我用玩笑在结合Firebase Web SDK和FirebaseUI的Create React App上运行一些测试
每当我尝试使用--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}]);
});
});
答案 0 :(得分:1)
我也遇到了这个问题。问题似乎来自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构造函数。这样一来,我就可以轻松地进行开玩笑的测试,并且auth仍然可以在应用程序中正常工作。
答案 1 :(得分:0)
您是否在Firebase配置中设置了持久性?测试环境不支持持久性,因此您可以执行以下操作来规避它:
firebase.auth().setPersistence(process.env.NODE_ENV === 'test'
? firebase.auth.Auth.Persistence.NONE
: firebase.auth.Auth.Persistence.LOCAL);