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 摘要。