Angular Services cannot be mocked in Angular TestBed
See original GitHub issueIf I mock an Angular Injectable Service, I get the following as soon as I include it in the module providers list:
TypeError: function is not iterable (cannot read property Symbol(Symbol.iterator))
at new Set (<anonymous>)
Pseudo code (edited for simplicity)
const issueServiceMock = mock<IssueService>();
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, RouterTestingModule],
declarations: [
IssueDashboardContainerComponent
],
providers: [
{provide: IssueService, useValue: issueServiceMock},
]
});
fixture = TestBed.createComponent(IssueDashboardContainerComponent); // CRASHES HERE
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
Angular unit test mock service - Stack Overflow
Your test does not inject the UserApiService properly, thus when you call getUserList() it attempts to start UserApiService.
Read more >Testing services - Angular
The TestBed is the most important of the Angular testing utilities. The TestBed creates a dynamically-constructed Angular test module that emulates an Angular...
Read more >The correct way of mocking dependencies in the Angular unit ...
The code above shows a simple and quick solution. You can create a mock class that extends the UserService . Then you can...
Read more >Mocking an Angular Service with Jest - Beyond Java
Mocking Angular services without using Angular. However, using the TestBed adds a layer of complexity to your test. Generally speaking, testing ...
Read more >All You Need to Know About Mocking in Angular Tests (2020)
For mocking component, the easiest solution I have found is to avoid needing to mock components by shallow testing, that is, use schemas:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I fixed this issue by providing the service using factory. But another problem is that the fixture is not updated in jest when async code is finished. Do you also have this problem?
Edit: Nevermind the problem. It happens only inside NX Workspaces.
The problem was, in my case, I had a template using an async pipe on an observable that would have been on my mock.
The async pipe is trying to use
someObservable$
as an iterable, but it’s really a function. The solution is to override those props after creating the mock: