angular / angular/angularfire

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

Đang mở
#3,668 0 bình luận 5 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
7.8k
Fork
2.2k
Merge trung bình
22 giờ 28 phút
Pull request đã merge (30 ngày)
6

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện FirebaseError: No Firebase App '[DEFAULT]' has been created trong một ứng dụng Angular sử dụng @angular/fire/compat hoặc @angular/fire với Jest. Xem xét sự tương tác giữa getAuth(), initializeApp() và TestBed.configureTestingModule. Công việc được xem là hoàn thành khi repository cung cấp một chiến lược kiểm thử Jest đáng tin cậy, có tài liệu, hoặc một tiện ích cho các service Angular phụ thuộc vào Firebase.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
angular, firebase, typescript
Lĩnh vực
frontend, testing
Loại issue
Tài liệu
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
28/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.