Write unit tests following project conventions. Generates Jest tests for Angular components, services, and NestJS services using AAA pattern.
Write unit tests following project conventions using Jest framework with AAA (Arrange-Act-Assert) pattern.
{name}.spec.ts alongside source filesnx test {project})Place test files next to the source files they test:
feature/
├── feature.service.ts
├── feature.service.spec.ts
├── feature.component.ts
└── feature.component.spec.ts
@smartsoft001/{package-name}: ClassNameit('should...') with clear behavior descriptionAlways use Arrange-Act-Assert pattern with blank line separation (no comments):
it('should perform expected operation', () => {
const input = 'test';
const result = service.performOperation(input);
expect(result).toBe('expected output');
});
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FeatureComponent } from './feature.component';
describe('@smartsoft001/crud-shell-angular: FeatureComponent', () => {
let component: FeatureComponent;
let fixture: ComponentFixture<FeatureComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [FeatureComponent],
}).compileComponents();
fixture = TestBed.createComponent(FeatureComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
it('should update when signal changes', () => {
component.count.set(5);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toContain('5');
});
it('should compute derived value', () => {
component.items.set([1, 2, 3]);
expect(component.total()).toBe(6);
});
it('should accept input value', () => {
fixture.componentRef.setInput('value', 'test');
fixture.detectChanges();
expect(component.value()).toBe('test');
});
it('should emit output event', () => {
const spy = jest.fn();
component.changed.subscribe(spy);
component.emitChange('new value');
expect(spy).toHaveBeenCalledWith('new value');
});
import { TestBed } from '@angular/core/testing';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { provideHttpClient } from '@angular/common/http';
import { DataService } from './data.service';
describe('@smartsoft001/angular: DataService', () => {
let service: DataService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [DataService, provideHttpClient(), provideHttpClientTesting()],
});
service = TestBed.inject(DataService);
httpMock = TestBed.inject(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
});
it('should fetch data', () => {
const mockData = [{ id: 1, name: 'Test' }];
service.getData().subscribe((data) => {
expect(data).toEqual(mockData);
});
const req = httpMock.expectOne('/api/data');
expect(req.request.method).toBe('GET');
req.flush(mockData);
});
});
import { Test, TestingModule } from '@nestjs/testing';
import { FeatureService } from './feature.service';
describe('@smartsoft001/package-name: FeatureService', () => {
let service: FeatureService;
let module: TestingModule;
beforeEach(async () => {
module = await Test.createTestingModule({
providers: [FeatureService],
}).compile();
service = module.get<FeatureService>(FeatureService);
});
afterEach(async () => {
await module.close();
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
const mockService = {
getData: jest.fn().mockReturnValue(of(mockData)),
createItem: jest.fn().mockReturnValue(of(mockItem)),
};
export const createMockUser = (overrides: Partial<User> = {}): User => ({
id: 'test-id',
login: 'testuser',
name: 'Test',
email: '[email protected]',
...overrides,
});
nx test <project-name>
nx test <project-name> --watch
nx test <project-name> --coverage
nx run-many --target=test
nx test <project-name> --testFile=feature.service.spec.ts