angular / angular/angularfire

[Unit Testing] Difficulties with mocking Firebase services in Jest/Angular environment

未关闭
#3,668 0 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
TypeScript
星标
7.8k
派生
2.2k
平均合并
22 小时 28 分钟
30 天内合并 PR
6

描述

When using @angular/fire/compat or @angular/fire to build Angular applications, we encounter significant challenges when trying to perform unit tests with a testing framework like Jest.

The main issue stems from the tight coupling of the Auth service (and others like Storage or Firestore) to the FirebaseApp instance. Functions like getAuth() and initializeApp() are called implicitly or explicitly by the AngularFire component, which leads to the FirebaseError: No Firebase App '[DEFAULT]' has been created error.

The core problem is that we cannot properly mock these functions before the component attempts to access them. Standard mocking techniques (e.g., jest.mock, TestBed.configureTestingModule providers) fail because the functions are either read-only properties or are called in a context where a FirebaseApp instance is expected, but not yet available.

This forces developers to use fragile workarounds to get tests to pass, making the testing process difficult and prone to errors. It also discourages writing comprehensive unit tests for components that depend on Firebase services.

Suggested Improvement:

We would appreciate it if the AngularFire team could provide an official, documented strategy for unit testing with Jest. A potential solution could involve a testing utility library or a recommended pattern that correctly mocks the underlying FirebaseApp instance and its dependent services in a reliable way.

贡献指南

打开贡献指南

调研方向

首先,在使用 @angular/fire/compat 或 @angular/fire 和 Jest 的 Angular 应用中复现 FirebaseError: No Firebase App '[DEFAULT]' has been created。检查 getAuth()、initializeApp() 和 TestBed.configureTestingModule 之间的交互。完成的标准是,repository 为依赖 Firebase 的 Angular 服务提供可靠且有文档记录的 Jest 测试策略或工具。

由索引模型根据 Issue 内容生成。

评估

技术栈
angular, firebase, typescript
领域
frontend, testing
Issue 类型
文档
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
28/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。