[Unit Testing] Difficulties with mocking Firebase services in Jest/Angular environment
- Linguagem predominante
- TypeScript
- Estrelas
- 7.8k
- Forks
- 2.2k
- Merge médio
- 22h 28min
- PRs com merge (30d)
- 6
Descrição
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.
Guia de contribuição
Direção de pesquisa
Comece reproduzindo o FirebaseError: No Firebase App '[DEFAULT]' has been created em uma aplicação Angular usando @angular/fire/compat ou @angular/fire com Jest. Analise as interações entre getAuth(), initializeApp() e TestBed.configureTestingModule. O trabalho estará concluído quando o repositório fornecer uma estratégia de testes com Jest confiável e documentada ou um utilitário para serviços Angular dependentes do Firebase.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- angular, firebase, typescript
- Domínio
- frontend, testing
- Tipo de issue
- Documentação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Estagnada
- Clareza
- Precisa de esclarecimento
- Facilidade para iniciantes
- 28/100