Skip to content
Back to skills

Angular Testing

ASecurity

`TestBed` creates the Angular testing environment, while `ComponentFixture` owns the component instance and its change-detection lifecycle. Configure the test module before creating the fixture. For a standalone component, import it directly; for an NgModule-based component, declare it and import the modules its template needs. The example below assumes a standalone `CounterComponent` with a `count()` signal, an `app-counter` host selector, a `data-testid="count"` element, and an accessible i...

  • 584 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 5, 2026
developmentangulartesting

Works with

  • cli

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

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
# Writing an Angular unit test with `TestBed`

`TestBed` creates the Angular testing environment, while `ComponentFixture` owns the component instance and its change-detection lifecycle. Configure the test module before creating the fixture. For a standalone component, import it directly; for an NgModule-based component, declare it and import the modules its template needs.

The example below assumes a standalone `CounterComponent` with a `count()` signal, an `app-counter` host selector, a `data-testid="count"` element, and an accessible increment button. The test uses a harness for DOM interaction so the spec does not depend on the template's CSS structure.

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

class CounterHarness extends ComponentHarness {
  static hostSelector = 'app-counter';

  private readonly count = this.locatorFor('[data-testid="count"]');
  private readonly increment = this.locatorFor(
    'button[aria-label="Increment"]',
  );

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

  async incrementByUser(): Promise<void> {
    await (await this.increment()).click();
  }
}

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

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

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

  it('increments when the user clicks the increment button', async () => {
    fixture.detectChanges();
    const counter = await loader.getHarness(CounterHarness);

    expect(await counter.getCount()).toBe('0');

    await counter.incrementByUser();

    expect(await counter.getCount()).toBe('1');
    expect(fixture.componentInstance.count()).toBe(1);
  });
});
```

`fixture.componentInstance` is useful for class-level state, but assert the rendered behavior through a harness when the behavior is user-visible. Call `fixture.detectChanges()` after setup or a synchronous state change when the DOM must be updated. Harness interactions normally stabilize and run change detection around the interaction.

For a required signal input, set it before the first change-detection pass:

```ts
fixture.componentRef.setInput('name', 'Ada');
fixture.detectChanges();
```

Keep each test isolated by creating a new fixture in `beforeEach`; configure providers, child components, directives, and pipes in the same `TestBed` configuration before `createComponent()`.

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.