跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
开发与测试

angularjs-unit-testing

Use this skill for AngularJS unit testing, maintenance, and migration tasks

greedychipmunk · 已有 AI 审核记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add greedychipmunk/agent-skills --skill angularjs-unit-testing

技能说明


name: angularjs-unit-testing description: Use this skill for AngularJS unit testing, maintenance, and migration tasks

AngularJS Unit Testing Skill

Overview

This skill specializes in writing, refactoring, and maintaining high-quality unit tests for AngularJS (1.x) applications. It covers controllers, services, filters, directives, HTTP mocking, promises, and dependency injection — everything you need to keep an AngularJS codebase well-tested and reliable.

Note: AngularJS reached end-of-life in December 2021. It receives only critical security fixes. New projects should use Angular 19+. For teams maintaining AngularJS codebases, this skill provides the latest testing patterns, tooling, and migration guidance. See Migration Path at the bottom of this document.

Skill Capabilities

Core Testing Competencies

  • Controllers: Write tests for controller logic, state management, and event handling
  • Services: Test factory/service dependencies, HTTP calls, and business logic
  • Filters: Validate filter transformations and edge cases
  • Directives: Test directive compilation, linking, and DOM manipulation
  • HTTP Mocking: Mock HTTP calls using $httpBackend (Jasmine/Karma) or MSW/fetch mocks (Jest)
  • Promises & Async: Handle $q, deferred objects, and $timeout
  • Dependency Injection: Test with mocked and real dependencies
  • Scope Management: Test scope lifecycle, watchers, and event broadcasting
  • Coverage Analysis: Generate and interpret code coverage reports
  • Test Organization: Structure tests following best practices and maintainability principles
  • Framework Choice: Jest for new work and hybrid repos; Jasmine/Karma for existing suites

Testing Patterns

This skill implements the following testing patterns:

  1. AAA Pattern (Arrange-Act-Assert)

    • Organize tests with clear setup, execution, and verification phases
    • Improves readability and maintainability
  2. Mocking & Spying

    • Use Jasmine spies or Jest mocks to mock functions and verify calls
    • Mock HTTP responses and service dependencies
    • Simulate user interactions
  3. Test Fixtures

    • Create reusable test data and helper functions
    • Use beforeEach/afterEach for common setup/teardown
    • Reduce test duplication
  4. Edge Case Testing

    • Test boundary conditions, empty states, and error scenarios
    • Validate error handling and graceful degradation
    • Test asynchronous operations and race conditions
  5. Snapshot Testing (Jest)

    • Capture expected component output
    • Detect unintended changes in UI rendering
  6. Deterministic Async

    • Prefer fake timers, controlled promises, and isolated state over timing-sensitive assertions

Testing Frameworks & Test Runners

Jasmine (Legacy Default)

Jasmine remains the safest choice when you are preserving an existing AngularJS + Karma suite.

Key Concepts:

  • describe(): Group related tests into a test suite
  • it(): Define individual test cases
  • expect(): Create assertions
  • beforeEach() / afterEach(): Setup and teardown hooks
  • spyOn() / jasmine.createSpy(): Mock functions and track calls

Setup:

npm install --save-dev jasmine karma karma-jasmine karma-chrome-launcher

Use when:

  • You are maintaining an existing Jasmine/Karma suite
  • You want the smallest possible change set for legacy AngularJS code

Jest (Recommended)

Jest is the modern default for AngularJS test maintenance and migration work. It runs tests in parallel, has stronger mocking APIs, supports snapshots, and includes built-in coverage reporting.

Key Concepts:

  • describe(): Group related tests
  • test() or it(): Define individual test cases
  • expect(): Create assertions
  • beforeEach() / afterEach(): Setup and teardown hooks
  • jest.fn(): Create mock functions
  • jest.spyOn(): Spy on existing methods
  • jest.mock(): Mock modules

Setup:

npm install --save-dev jest jest-preset-angular angular-mocks
npm install @angular/core

Jest Configuration (jest.config.js):

module.exports = {
  preset: 'jest-preset-angular',
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
  transform: {
    '^.+\.js$': 'babel-jest'
  },
  collectCoverage: true,
  collectCoverageFrom: ['src/**/*.js', '!src/**/*.spec.js']
};

Use when:

  • You want faster feedback from parallel execution
  • You need better mocking, snapshots, and coverage out of the box
  • You are preparing an AngularJS codebase for an Angular migration

Karma (Legacy Runner)

Karma is the legacy browser test runner traditionally paired with Jasmine. It is still usable for existing suites, but it has seen no major releases since 2021 and should not be the basis for new investment.

Configuration:

  • karma.conf.js: Main configuration file
  • Specifies browser environment, files to load, and plugins
  • Supports code coverage reporting and CI integration

Jest Migration Guide

Use these steps when moving an AngularJS test suite from Jasmine/Karma to Jest:

  1. Install the core tooling

    npm install --save-dev jest jest-preset-angular angular-mocks
    npm install @angular/core
    

    Add @angular/core when the repo is hybrid or actively migrating toward Angular.

  2. Create a Jest setup file

    • Add setup-jest.js or setup-jest.ts
    • Load angular, angular-mocks, and any shared test polyfills there
  3. Configure Jest for AngularJS files

    • Use jest.config.js with testEnvironment: 'jsdom'
    • Add a transform for legacy JavaScript sources
    • Keep template or DOM-specific setup in the Jest bootstrap file
  4. Load AngularJS modules in Jest

    beforeEach(() => {
      require('angular');
      require('angular-mocks');
      angular.mock.module('myApp');
    });
    
  5. Migrate spies and stubs

    • spyOn(obj, 'method') → `jest.spyO
阅读完整来源 ↗