TestBed is primary API for unit testing. Configures testing module, creates components, provides dependencies. Methods include configureTestingModule(), createComponent(). Essential for component/service testing.
HTTP testing using HttpTestingController. Mock requests/responses, verify request parameters. Test success/error scenarios, multiple requests. Important for API integration testing.
E2E testing verifies full application flow. Implemented using Protractor/Cypress. Test user scenarios, navigation, interactions. Important for regression testing, feature verification.
Pipe testing through isolated tests, transformation verification. Test with different inputs, edge cases. Consider pure/impure pipes, parameter handling. Simple to test due to pure function nature.
Test doubles include: Spies, Stubs, Mocks, Fakes, Dummies. Used for isolating components, controlling dependencies. Choose based on testing needs, complexity. Important for unit testing.
Form testing through ReactiveFormsModule/FormsModule. Test form validation, value changes, submissions. Simulate user input, verify form state. Consider complex validation scenarios.
Integration testing verifies component interactions. Test parent-child communications, service integration. Use TestBed for configuration, fixture for interaction. Consider component relationships.
Shallow testing renders component without children, deep includes child components. Choose based on testing scope, complexity. Consider component dependencies, testing goals.
Common patterns: setup/teardown, data builders, test utilities, shared configurations. Consider reusability, maintenance. Important for testing efficiency, consistency.
Test isolation ensures tests run independently. Reset state between tests, mock dependencies. Consider test order, shared resources. Important for reliable testing.
Directive testing through component creation, DOM manipulation. Test directive behavior, inputs/outputs. Create test host components. Consider element interactions, lifecycle hooks.
Route testing using RouterTestingModule, Location service. Test navigation, route parameters, guards. Mock router service, verify navigation calls. Important for application flow testing.
Observable testing using marbles, fakeAsync. Test stream behavior, error handling. Consider unsubscription, memory leaks. Important for reactive programming testing.
NgRx testing through TestStore, mock store. Test actions, reducers, effects separately. Consider state changes, async operations. Important for state management testing.
Fixtures provide component testing environment. Created using TestBed.createComponent(). Access component instance, debug element. Essential for component testing, DOM interaction.
Async validator testing through fakeAsync, tick. Mock validation service, test timing. Consider error cases, loading states. Important for form validation testing.
Angular supports: Unit Testing (isolated component/service testing), Integration Testing (component interactions), E2E Testing (full application flow). Uses Jasmine framework, Karma test runner, Protractor/Cypress for E2E.
Service testing through TestBed injection, isolated testing. Mock dependencies, test methods/properties. Use dependency injection, spies for external services. Test async operations using fakeAsync/tick.
Spies mock method behavior, track calls. Created using spyOn(), createSpy(). Track method calls, arguments, return values. Essential for mocking dependencies, verifying interactions.
Code coverage measures tested code percentage. Generated using ng test --code-coverage. Track statements, branches, functions coverage. Important for quality assurance, identifying untested code.
Snapshot testing compares component output with stored snapshot. Useful for UI regression testing. Consider update process, maintenance. Important for template stability.
Custom event testing through EventEmitter simulation, output binding. Test event emission, handler execution. Consider event payload, timing. Important for component interaction.
Error boundary testing through error triggering, recovery verification. Test error handling components, fallback UI. Consider different error scenarios, user experience.
Test harnesses provide component interaction API. Simplify component testing, abstract DOM details. Used in Angular Material testing. Important for complex component testing.
Component testing using TestBed, ComponentFixture. Test isolated logic, template bindings, component interactions. Mock dependencies, simulate events. Use shallow/deep rendering based on needs.
Async testing handles asynchronous operations. Use async/fakeAsync/waitForAsync helpers. Test promises, observables, timers. Important for testing real-world scenarios.
Best practices: arrange-act-assert pattern, single responsibility tests, proper isolation, meaningful descriptions. Consider test maintainability, readability. Follow testing pyramid principles.
Error testing through exception throwing, error handling verification. Test error states, recovery mechanisms. Consider different error types, user feedback. Important for robustness.
Lifecycle testing through hook method spies, state verification. Test initialization, destruction logic. Consider timing, dependencies. Important for component behavior testing.