Angular Testing
ASecurityWithout 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
Works with
Security analysis
100/100Pro scans all 18 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-testing --agent claude-codeAre you the author of Angular Testing?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-testing-b8505d78)# 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.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- eval-4.baseline.md
- eval-4.with-skill.md
- eval-5.baseline.md
- eval-5.with-skill.md
- eval-6.baseline.md
- eval-6.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.