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:
-
AAA Pattern (Arrange-Act-Assert)
- Organize tests with clear setup, execution, and verification phases
- Improves readability and maintainability
-
Mocking & Spying
- Use Jasmine spies or Jest mocks to mock functions and verify calls
- Mock HTTP responses and service dependencies
- Simulate user interactions
-
Test Fixtures
- Create reusable test data and helper functions
- Use beforeEach/afterEach for common setup/teardown
- Reduce test duplication
-
Edge Case Testing
- Test boundary conditions, empty states, and error scenarios
- Validate error handling and graceful degradation
- Test asynchronous operations and race conditions
-
Snapshot Testing (Jest)
- Capture expected component output
- Detect unintended changes in UI rendering
-
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 suiteit(): Define individual test casesexpect(): Create assertionsbeforeEach()/afterEach(): Setup and teardown hooksspyOn()/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 teststest()orit(): Define individual test casesexpect(): Create assertionsbeforeEach()/afterEach(): Setup and teardown hooksjest.fn(): Create mock functionsjest.spyOn(): Spy on existing methodsjest.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:
-
Install the core tooling
npm install --save-dev jest jest-preset-angular angular-mocks npm install @angular/coreAdd
@angular/corewhen the repo is hybrid or actively migrating toward Angular. -
Create a Jest setup file
- Add
setup-jest.jsorsetup-jest.ts - Load
angular,angular-mocks, and any shared test polyfills there
- Add
-
Configure Jest for AngularJS files
- Use
jest.config.jswithtestEnvironment: 'jsdom' - Add a transform for legacy JavaScript sources
- Keep template or DOM-specific setup in the Jest bootstrap file
- Use
-
Load AngularJS modules in Jest
beforeEach(() => { require('angular'); require('angular-mocks'); angular.mock.module('myApp'); }); -
Migrate spies and stubs
spyOn(obj, 'method')→ `jest.spyO