Skip to main content

Testing

Running Tests

npm test                       # All tests
npm run test:statuspages # Statux Pages only
npm run test:alerting # Statux Alerts only
npm run test:synthetics # Statux Synthetics only
npm run test:cov # With coverage

Service Test Pattern

describe('ComponentsService', () => {
let service: ComponentsService;
let repo: MockRepository<Component>;

beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
ComponentsService,
{
provide: getRepositoryToken(Component),
useValue: {
find: jest.fn(),
findOne: jest.fn(),
create: jest.fn(),
save: jest.fn(),
merge: jest.fn((e, ...s) => Object.assign(e, ...s)),
},
},
],
}).compile();

service = module.get(ComponentsService);
repo = module.get(getRepositoryToken(Component));
});

describe('findOne', () => {
it('should return component', async () => {
const component = { id: '123', name: 'Test' };
repo.findOne.mockResolvedValue(component);

const result = await service.findOne('123', 'project-id');

expect(result).toEqual(component);
});

it('should throw NotFoundException', async () => {
repo.findOne.mockResolvedValue(null);

await expect(service.findOne('123', 'project-id'))
.rejects.toThrow(NotFoundException);
});
});
});