Skip to content
Back to skills

Angular Testing

ASecurity

Without the component source, first map its public contract: inputs, outputs, visible states, user actions, and injected services. Replace the names below with the actual API. For a standalone component, import it in `TestBed`; for an NgModule-based component, declare it and provide the modules its template uses. The harness keeps the spec focused on behavior instead of template selectors: ```ts import { ComponentHarness, HarnessLoader } from '@angular/cdk/testing'; import { TestbedHarnessEnv...

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

Works with

  • cli
  • 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-b8505d78/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-testing-b8505d78)

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
# A practical `user.component.spec.ts` structure

Without the component source, first map its public contract: inputs, outputs, visible states, user actions, and injected services. Replace the names below with the actual API. For a standalone component, import it in `TestBed`; for an NgModule-based component, declare it and provide the modules its template uses.

The harness keeps the spec focused on behavior instead of template selectors:

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

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

class UserHarness extends ComponentHarness {
  static hostSelector = 'app-user';

  private readonly name = this.locatorFor('[data-testid="user-name"]');
  private readonly save = this.locatorFor(
    'button[aria-label="Save user"]',
  );
  private readonly empty = this.locatorForOptional(
    '[data-testid="empty-state"]',
  );

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

  async clickSave(): Promise<void> {
    await (await this.save()).click();
  }

  async hasEmptyState(): Promise<boolean> {
    return (await this.empty()) !== null;
  }
}

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

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [UserComponent],
      // Replace real collaborators here, for example:
      // providers: [{ provide: UserService, useValue: userServiceStub }],
    }).compileComponents();

    fixture = TestBed.createComponent(UserComponent);
    component = fixture.componentInstance;
    loader = TestbedHarnessEnvironment.loader(fixture);
  });

  it('renders the supplied user', async () => {
    const user: User = { id: 'u1', name: 'Ada Lovelace' };
    fixture.componentRef.setInput('user', user);
    fixture.detectChanges();

    const userView = await loader.getHarness(UserHarness);
    expect(await userView.getName()).toBe('Ada Lovelace');
  });

  it('emits the user when Save is clicked', async () => {
    const user: User = { id: 'u1', name: 'Ada Lovelace' };
    let emitted: User | undefined;
    component.saved.subscribe(value => (emitted = value));

    fixture.componentRef.setInput('user', user);
    fixture.detectChanges();
    await (await loader.getHarness(UserHarness)).clickSave();

    expect(emitted).toEqual(user);
  });

  it('shows the empty state when there is no user', async () => {
    fixture.componentRef.setInput('user', null);
    fixture.detectChanges();

    const userView = await loader.getHarness(UserHarness);
    expect(await userView.hasEmptyState()).toBe(true);
  });
});
```

If `user` is an `input.required<User>()`, do not set it to `null`; use the component's actual optional input or model the empty state through the real API. `setInput()` is also the correct way to provide a signal input and should be followed by `detectChanges()` before assertions. For asynchronous service work, cover loading, success, and error states. If the component makes HTTP requests, use `provideHttpClient()` plus `provideHttpClientTesting()` and assert them with `HttpTestingController`; do not hand-write an `HttpClient` mock. Keep a fresh fixture and fresh service double per 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.