Skip to content

Inject error when using async pipe #435

Closed
@Thyregod

Description

@Thyregod

Description:
I face a NG0203: inject error when I use async pipe inside template when testing.

Error:
image

How to reproduce:

  1. Add the code as described below
  2. Run tests
  3. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions