首页 > 解决方案 > 为什么通过 cypress-firebase npm 模块登录时 firebase 未定义?

问题描述

我在用着

"cypress-firebase": "^2.0.0",
"firebase-admin": "^9.11.1"

在我的柏树command.js文件中:

import firebase from "firebase/app";
import "firebase/auth";
import "firebase/database";
import "firebase/firestore";
import { attachCustomCommands } from "cypress-firebase";

const fbConfig = {
}

firebase.initializeApp(fbConfig);

attachCustomCommands({ Cypress, cy, firebase });

当我尝试执行代码时,我面临以下问题:

类型错误

以下错误源自您的测试代码,而不是赛普拉斯:

> 无法读取未定义的属性“initializeApp”

标签: javascriptfirebasenpmcypresscypress-component-test-runner

解决方案


在 Firebase SDK v9 中,API 界面更改为使用模块化、可摇树的代码。您看到的几乎所有文档或示例代码都是为需要更新的 v8 或更早版本的 Firebase SDK 编写的。

在此处阅读有关迁移的更多信息。

由于cypress-firebase尚未更新支持 v9 SDK,因此需要导入兼容 SDK。请注意,兼容性 SDK 已弃用,理想情况下,您应该找到已更新以支持 v9 的软件包。

import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/database";
import "firebase/compat/firestore";
import { attachCustomCommands } from "cypress-firebase";

const fbConfig = {
}

firebase.initializeApp(fbConfig);

attachCustomCommands({ Cypress, cy, firebase });

推荐阅读