nestjs-hot-shots
Version:
Hot-shots Module for Nest.js Framework
54 lines (40 loc) • 1.58 kB
text/typescript
import { Test } from '@nestjs/testing';
import { StatsD } from 'hot-shots';
import { TimingCollector, HotShotsModule, MetricsService } from '../../../src';
describe('TimingCollector', () => {
let metricsService: MetricsService, statsD: StatsD;
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
imports: [HotShotsModule.forRoot({ mock: true })]
}).compile();
statsD = moduleRef.get(StatsD);
metricsService = moduleRef.get(MetricsService);
});
it('should be defined', () => {
expect(statsD).toBeDefined();
expect(metricsService).toBeDefined();
});
it('should create TimingCollector', () => {
const instance = metricsService.getTiming('test_metric');
expect(instance).toBeDefined();
expect(instance).toBeInstanceOf(TimingCollector);
});
it('should record timing value', () => {
const instance = metricsService.getTiming('test_metric');
instance.record(10);
expect(statsD.mockBuffer[0]).toBe('test_metric:10|ms');
});
it('should record timing value with tags', () => {
const instance = metricsService.getTiming('test_metric');
instance.record(10, { tag1: 'value1', tag2: 'value2' });
expect(statsD.mockBuffer[0]).toBe('test_metric:10|ms|#tag1:value1,tag2:value2');
});
it('should record timing value with merge tags', () => {
const instance = metricsService.getTiming('test_metric', { tags: { tag1: 'value1' } });
instance.record(10, { tag2: 'value2' });
expect(statsD.mockBuffer[0]).toBe('test_metric:10|ms|#tag1:value1,tag2:value2');
});
afterEach(() => {
jest.clearAllMocks();
});
});