Closed
Description
Description:
I face a NG0203: inject error when I use async
pipe inside template when testing.
How to reproduce:
- Add the code as described below
- Run tests
- See error in terminal
// demo.component.spec.ts
import { screen, render } from '@testing-library/angular';
import { CommonModule } from '@angular/common';
import { BehaviorSubject } from 'rxjs';
import { Component, Inject, Injectable } from '@angular/core';
// Service
@Injectable()
export class DemoService {
buttonTitle = new BehaviorSubject<string>('Click me');
}
// Component
@Component({
selector: 'demo',
standalone: true,
imports: [CommonModule],
providers: [DemoService],
template: `
<button>
<!-- 👇 I only get the Inject error when I use the async pipe here -->
{{ demoService.buttonTitle | async }}
</button>
`,
})
export class DemoComponent {
constructor(@Inject(DemoService) public demoService: DemoService) {}
}
// Test
describe(DemoComponent.name, () => {
it('should render button', async () => {
await render(DemoComponent);
const button = screen.getByRole('button', {
name: /Click me/,
});
expect(button).toBeVisible();
});
});
Question:
Do you have any idea why this occurs and if it has to do with angular-testing-library
?
Metadata
Metadata
Assignees
Labels
No labels