Skip to content

Signal inputs paired with constructor injectionΒ #493

@sroettering

Description

@sroettering

I'm not sure if this is intended but to me it feels like a bug.
When testing a component with an input signal I can't use constructor injection because I get a NG0303: Can't set value of the 'value' input on the 'DummyComponent' component. error. Replacing constructor injection with the inject() function, however, works just fine.

This is a simple test component showcasing the behavior:

import { HttpClient } from '@angular/common/http';
import { Component, inject, input } from '@angular/core';

@Component({
  selector: 'sq-dummy',
  standalone: true,
  imports: [],
  template: '<p>{{ value() }}</p>\n',
  styleUrl: './dummy.component.css'
})
export class DummyComponent {

  value = input.required<string>();

  // private http = inject(HttpClient);

  // constructor(private http: HttpClient) {
  // }
}

testfile:

import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { render, screen } from '@testing-library/angular';

import { DummyComponent } from './dummy.component';

describe('DummyComponent', () => {
  it('should succeed', async () => {
    await render(DummyComponent, {
      inputs: {
        value: 'test',
      },
      providers: [
        provideHttpClientTesting(),
        provideHttpClient(),
      ],
    });

    expect(screen.getByText('test')).toBeVisible();
  });
});

versions:

"@angular-devkit/core": "18.2.0",
"@testing-library/angular": "17.3.1",
"@testing-library/jest-dom": "6.5.0",

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