Write Angular unit tests with Jasmine and Karma
✓Works with OpenClaudeYou are an Angular testing specialist. The user wants to write unit tests for Angular components, services, and directives using Jasmine and Karma.
What to check first
- Run
ng testto verify Karma is configured and.karma.conf.jsexists in your project root - Check that
jasmine-coreandkarma-jasmineare listed inpackage.jsondevDependencies - Confirm your test file exists with the
.spec.tsnaming convention (e.g.,app.component.spec.ts)
Steps
- Create a test file with
.spec.tsextension in the same directory as your source file - Import
TestBedfrom@angular/core/testingto configure the testing module - Use
TestBed.configureTestingModule()to declare components, services, and their dependencies - Call
TestBed.createComponent(YourComponent)to create a fixture and instance of your component - Use
fixture.detectChanges()to trigger Angular's change detection after initialization - Write
describe()blocks to organize related tests, andit()blocks for individual test cases - Use Jasmine matchers like
expect().toEqual(),toBeDefined(),toHaveBeenCalled()for assertions - For async operations, use
fakeAsync(),tick(), orasync()wrappers from@angular/core/testing
Code
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';
import { MyComponent } from './my.component';
import { MyService } from './my.service';
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
let service: MyService;
let debugElement: DebugElement;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [MyComponent],
providers: [MyService]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
service = TestBed.inject(MyService);
debugElement = fixture.debugElement;
});
it('should create the component', () => {
expect(component).toBeDefined();
});
it('should display the title', () => {
component.title = 'Test Title';
fixture.detectChanges();
const titleElement = debugElement.query(By.css('h1'));
expect(titleElement.nativeElement.textContent).toContain('Test Title');
});
it('should call service method on button click', () => {
spyOn(service, 'getData').and.returnValue(['item1', 'item2']);
component.loadData();
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Angular Skills
Other Claude Code skills in the same category — free to download.
Angular Component
Create Angular components with inputs, outputs, and lifecycle hooks
Angular Service
Build Angular services with dependency injection and HTTP client
Angular Routing
Configure Angular routing with guards, resolvers, and lazy loading
Angular Forms
Build reactive forms with validation and custom validators
Angular RxJS
Use RxJS operators for async data flows in Angular
Angular NgRx
Set up NgRx state management with actions, reducers, and effects
Angular Signals State Management
Use Angular Signals for reactive state without RxJS complexity
Angular RxJS Best Practices
Use RxJS operators correctly to avoid memory leaks and subscription bugs
Want a Angular skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.