Skip to content
Back to skills

Angular Testing

ASecurity

An `input()` value is a read-only signal inside the component. Set it through the fixture's component reference with `setInput()`, not by assigning to `fixture.componentInstance.user`. Run change detection after each input update before asserting a rendered value or a computed signal. Signals update synchronously, so most signal-input tests do not need `fakeAsync`. For a component with a required input: ```ts import { ChangeDetectionStrategy, Component, computed, input, } from '@angular/core'...

  • 584 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 5, 2026
developmentgoreactangulartestingapi

Works with

  • api

Security analysis

A100/100

Pro scans all 18 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Angular Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Angular Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-angular-testing-4ea5b3aa/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-testing-4ea5b3aa)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Testing signal inputs in Angular component tests

An `input()` value is a read-only signal inside the component. Set it through the fixture's component reference with `setInput()`, not by assigning to `fixture.componentInstance.user`. Run change detection after each input update before asserting a rendered value or a computed signal. Signals update synchronously, so most signal-input tests do not need `fakeAsync`.

For a component with a required input:

```ts
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from '@angular/core';

type User = { name: string; role: string };

@Component({
  standalone: true,
  selector: 'app-user-badge',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <strong data-testid="name">{{ user().name }}</strong>
    <span data-testid="label">{{ label() }}</span>
  `,
})
export class UserBadgeComponent {
  readonly user = input.required<User>();
  readonly label = computed(() => `${this.user().name} (${this.user().role})`);
}
```

Test it by setting the public input name before the first detection pass:

```ts
import { ComponentHarness, HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { UserBadgeComponent } from './user-badge.component';

class UserBadgeHarness extends ComponentHarness {
  static hostSelector = 'app-user-badge';
  private readonly label = this.locatorFor('[data-testid="label"]');

  async getLabel(): Promise<string> {
    return (await this.label()).text();
  }
}

describe('UserBadgeComponent', () => {
  let fixture: ComponentFixture<UserBadgeComponent>;
  let loader: HarnessLoader;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [UserBadgeComponent],
    }).compileComponents();

    fixture = TestBed.createComponent(UserBadgeComponent);
    loader = TestbedHarnessEnvironment.loader(fixture);
  });

  it('renders the initial signal input', async () => {
    fixture.componentRef.setInput('user', {
      name: 'Ada Lovelace',
      role: 'Engineer',
    });
    fixture.detectChanges();

    expect(fixture.componentInstance.user()).toEqual({
      name: 'Ada Lovelace',
      role: 'Engineer',
    });
    expect(fixture.componentInstance.label()).toBe('Ada Lovelace (Engineer)');

    const badge = await loader.getHarness(UserBadgeHarness);
    expect(await badge.getLabel()).toBe('Ada Lovelace (Engineer)');
  });

  it('reacts when the parent changes the input', () => {
    fixture.componentRef.setInput('user', { name: 'Ada', role: 'Engineer' });
    fixture.detectChanges();

    fixture.componentRef.setInput('user', {
      name: 'Grace',
      role: 'Scientist',
    });
    fixture.detectChanges();

    expect(fixture.componentInstance.label()).toBe('Grace (Scientist)');
  });
});
```

`createComponent()` does not require an initial detection pass, which is useful for required inputs: provide the value first, then call `detectChanges()`. The same `setInput()` API works for ordinary inputs. If the component uses an input alias, pass the public input name expected by `ComponentRef.setInput()`. A host component with `[user]="user"` is appropriate when the goal is to test the parent-to-child binding itself; `setInput()` is usually the simpler child unit test.

Files in this skill

  • eval-1.baseline.md2.6 KB
  • eval-1.with-skill.md2.7 KB
  • eval-2.baseline.md2.9 KB
  • eval-2.with-skill.md3.1 KB
  • eval-3.baseline.md2.6 KB
  • eval-3.with-skill.md2.8 KB
  • eval-4.baseline.md2.9 KB
  • eval-4.with-skill.md3.3 KB
  • eval-5.baseline.md3.1 KB
  • eval-5.with-skill.md3.4 KB
  • eval-6.baseline.md2.9 KB
  • eval-6.with-skill.md2.7 KB
  • trigger-1.md124 B
  • trigger-2.md143 B
  • trigger-3.md136 B
  • trigger-4.md132 B
  • trigger-5.md143 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.