angular-testing について
Angularアプリのテストを行う開発者向けのリファレンススキルです。TestBed、非同期テスト、HTTP設定に加え、Angular 21以降のVitestも扱います。
主な機能
- 01TestBedによるコンポーネントテスト
- 02非同期テストの処理
- 03サービスのモックとスパイ
- 04HTTPテストの設定
- 05フォームとDOM動作のテスト
- 06Angular 21以降でのVitest利用
活用シーン
- 01コンポーネントの描画と動作のテスト
- 02非同期サービス処理の確認
- 03HTTPインターセプターとフォームの検証
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ oguzhan18/ angular-ecosystem-skills --skill angular-testingインストール後、Claude Code で/angular-testingを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
references/discussions/_INDEX.mdreferences/docs/_INDEX.mdreferences/issues/_INDEX.mdreferences/releases/_INDEX.md詳細
Angular のテスト
バージョン: Angular 21(2025) タグ: テスト、Jasmine、Karma、Vitest、Cypress、Jest
参考資料: テストガイド • テスト API • Angular ブログ
API の変更点
このセクションでは、バージョン固有の最近の API の変更点について説明します。
-
新機能:Vitest がデフォルトに — Angular 21 以降では Jasmine/Karma の代わりに Vitest を使用します 出典
-
新機能:テンプレートカバレッジ — カバレッジレポートに HTML テンプレートが含まれるようになりました 出典
-
新機能:コンポーネント入力のテスト —
Component输入を使用してコンポーネントの入力を直接テストできます -
新機能:テスト用の関数型インターセプター — HTTP インターセプターをより簡単にテストできます
-
非推奨:Karma テストランナー — 最新の Angular では Vitest に移行してください
ベストプラクティス
- コンポーネントのテストには TestBed を使用します
import { TestBed } from '@angular/core/testing';
import { MyComponent } from './my.component';
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MyComponent]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
- AAA パターン(Arrange-Act-Assert)を使用します
it('should calculate total correctly', () => {
// Arrange
const service = new CalculatorService();
// Act
const result = service.add(2, 3);
// Assert
expect(result).toBe(5);
});
- 非同期処理には
fakeAsyncとtickを使用します
import { fakeAsync, tick } from '@angular/core/testing';
it('should fetch data after delay', fakeAsync(() => {
service.getData();
tick(1000);
expect(component.data).toBeTruthy();
}));
- モックにはスパイを使用します
it('should call service', () => {
spyOn(service, 'getData').and.returnValue(of({ name: 'Test' }));
component.loadData();
expect(service.getData).toHaveBeenCalled();
});
- DOM 要素の検索には
by.cssを使用します
import { By } from '@angular/platform-browser';
it('should display title', () => {
fixture.detectChanges();
const el = fixture.debugElement.query(By.css('.title'));
expect(el.nativeElement.textContent).toBe('Hello');
});
- 実装ではなく、動作をテストします
// ❌ Bad - tests implementation details
expect(component['privateMethod']).toHaveBeenCalled();
// ✅ Good - tests behavior
expect(fixture.nativeElement.querySelector('.result')).toContain('expected');
- HTTP のテストには
provideHttpClientを使用します
TestBed.configureTestingModule({
providers: [
provideHttpClient(withInterceptors([authInterceptor]))
]
});
- 依存関係にはモックサービスを使用します
const mockAuthService = {
isAuthenticated: jasmine.createSpy().and.returnValue(true),
getToken: jasmine.createSpy().and.returnValue('fake-token')
};
TestBed.configureTestingModule({
providers: [{ provide: AuthService, useValue: mockAuthService }]
});
- フォームのバリデーションをテストします
it('should show error for invalid email', () => {
component.form.controls.email.setValue('invalid');
component.form.controls.email.markAsTouched();
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('.error')).toBeTruthy();
});
detectChangesを適切に使用します
// After changing component properties
component.value = 'new value';
fixture.detectChanges();
// For async operations
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
コメント
Markdown 対応