mcpField
A
angular-testing

angular-testing

github.com/oguzhan18テスト・品質7197 回インストール30 週間前に更新
リポジトリ

angular-testing について

Angularアプリのテストを行う開発者向けのリファレンススキルです。TestBed、非同期テスト、HTTP設定に加え、Angular 21以降のVitestも扱います。

主な機能

  1. 01TestBedによるコンポーネントテスト
  2. 02非同期テストの処理
  3. 03サービスのモックとスパイ
  4. 04HTTPテストの設定
  5. 05フォームとDOM動作のテスト
  6. 06Angular 21以降でのVitest利用

活用シーン

  1. 01コンポーネントの描画と動作のテスト
  2. 02非同期サービス処理の確認
  3. 03HTTPインターセプターとフォームの検証

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/oguzhan18/angular-ecosystem-skills --skill angular-testing

インストール後、Claude Code で/angular-testingを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

references/discussions/
_INDEX.md
references/docs/
_INDEX.md
references/issues/
_INDEX.md
references/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();

コメント