question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular Services cannot be mocked in Angular TestBed

See original GitHub issue

If 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:open
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
myin142commented, Jul 10, 2020

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.

0reactions
dgradwell-amscommented, Mar 15, 2021

The problem was, in my case, I had a template using an async pipe on an observable that would have been on my mock.

const foo = mock<IFoo>();
   <div *ngFor="let item of foo.someObservable$ | async"> 
       ...

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:

const foo = Object.assign(mock<IFoo>(), { someObservable$: of(...) });
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found