Skill: Testing Angular Components (angular-user-search)
Setup pattern
let userServiceSpy: jasmine.SpyObj<UserService>;
beforeEach(async () => {
userServiceSpy = jasmine.createSpyObj('UserService', ['getUsers']);
await TestBed.configureTestingModule({
imports: [UserSearchComponent, ReactiveFormsModule],
providers: [{ provide: UserService, useValue: userServiceSpy }]
}).compileComponents();
});
Rules
- Use
jasmine.createSpyObj— notjasmine.createSpy. of(mockData)for success,throwError(() => new Error(...))for errors.fakeAsync+tick(300)to advance the debounce timer.- Assert on component properties — not DOM.
Test naming
method_condition_expectedResult
Design Constraints
- Do not use
fixture.debugElementornativeElementqueries — component state only - Do not use
TestBed.configureTestingModulewith full imports — import only what's needed