Skip to content

Commit

Permalink
log-handlers-and-docs-upd
Browse files Browse the repository at this point in the history
- добавлены тесты хендлеров
  • Loading branch information
krutoo committed Aug 9, 2024
1 parent fad6d14 commit 7ddb70e
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/log/handler/__test__/pino.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pino from 'pino';
import { createPinoHandler } from '../pino';
import { createLogger } from '../../logger';

describe('createPinoHandler', () => {
it('should make handler properly', () => {
const stub: pino.Logger = {
info: jest.fn(),
warn: jest.fn(),
debug: jest.fn(),
error: jest.fn(),
} as unknown as pino.Logger;

const logger = createLogger();

logger.subscribe(createPinoHandler(stub));

expect(stub.info).toHaveBeenCalledTimes(0);
logger.info('AAA');
expect(stub.info).toHaveBeenCalledTimes(1);
expect(stub.info).toHaveBeenCalledWith('AAA');

expect(stub.info).toHaveBeenCalledTimes(1);
logger.log('AAA');
expect(stub.info).toHaveBeenCalledTimes(2);
expect(stub.info).toHaveBeenCalledWith('AAA');

expect(stub.warn).toHaveBeenCalledTimes(0);
logger.warn('BBB');
expect(stub.warn).toHaveBeenCalledTimes(1);
expect(stub.warn).toHaveBeenCalledWith('BBB');

expect(stub.debug).toHaveBeenCalledTimes(0);
logger.debug('CCC');
expect(stub.debug).toHaveBeenCalledTimes(1);
expect(stub.debug).toHaveBeenCalledWith('CCC');

expect(stub.error).toHaveBeenCalledTimes(0);
logger.error('DDD');
expect(stub.error).toHaveBeenCalledTimes(1);
expect(stub.error).toHaveBeenCalledWith('DDD');
});
});
96 changes: 96 additions & 0 deletions src/log/handler/__test__/sentry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { createSentryHandler } from '../sentry';
import { createLogger } from '../../logger';
import { Breadcrumb, DetailedError } from '../../errors';
import type { Scope } from '@sentry/types';

describe('createSentryHandler', () => {
it('should handle function as scopeInit', () => {
const scope: Scope = {
setLevel: jest.fn(),
setContext: jest.fn(),
setExtra: jest.fn(),
captureException: jest.fn(),
addBreadcrumb: jest.fn(),
} as unknown as Scope;

const getScope = jest.fn(() => scope);

const logger = createLogger();

logger.subscribe(createSentryHandler(getScope));

expect(scope.captureException).toHaveBeenCalledTimes(0);
logger.error('fake error 1');
logger.error('fake error 2');
logger.error('fake error 3');
expect(scope.captureException).toHaveBeenCalledTimes(3);
});

it('should make handler properly', () => {
const scope: Scope = {
setLevel: jest.fn(),
setContext: jest.fn(),
setExtra: jest.fn(),
captureException: jest.fn(),
addBreadcrumb: jest.fn(),
} as unknown as Scope;

const logger = createLogger();

logger.subscribe(createSentryHandler(scope));

// error level with non DetailedError
expect(scope.captureException).toHaveBeenCalledTimes(0);
logger.error('fake error');
expect(scope.captureException).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledWith('fake error');

// error level with DetailedError with defined level
const error = new DetailedError('fake', { level: 'fatal' });
expect(scope.setLevel).toHaveBeenCalledTimes(0);
expect(scope.captureException).toHaveBeenCalledTimes(1);
logger.error(error);
expect(scope.setLevel).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(2);
expect(scope.captureException).toHaveBeenCalledWith(error);

// error level with DetailedError with defined context
const error2 = new DetailedError('fake', { context: { key: 'CTX', data: {} } });
expect(scope.setContext).toHaveBeenCalledTimes(0);
expect(scope.captureException).toHaveBeenCalledTimes(2);
logger.error(error2);
expect(scope.setContext).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(3);
expect(scope.captureException).toHaveBeenCalledWith(error2);

// error level with DetailedError with defined context as array
const error3 = new DetailedError('fake', { context: [{ key: 'CTX', data: {} }] });
expect(scope.setContext).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(3);
logger.error(error3);
expect(scope.setContext).toHaveBeenCalledTimes(2);
expect(scope.captureException).toHaveBeenCalledTimes(4);
expect(scope.captureException).toHaveBeenCalledWith(error3);

// error level with DetailedError with defined extra
const error4 = new DetailedError('fake', { extra: { key: 'EXTRA', data: {} } });
expect(scope.setExtra).toHaveBeenCalledTimes(0);
expect(scope.captureException).toHaveBeenCalledTimes(4);
logger.error(error4);
expect(scope.setExtra).toHaveBeenCalledTimes(1);
expect(scope.captureException).toHaveBeenCalledTimes(5);
expect(scope.captureException).toHaveBeenCalledWith(error4);

// non error level with non Breadcrumb
expect(scope.captureException).toHaveBeenCalledTimes(5);
logger.info('hello');
expect(scope.captureException).toHaveBeenCalledTimes(5);

// non error level with Breadcrumb
const breadcrumb = new Breadcrumb({ category: 'test' });
expect(scope.addBreadcrumb).toHaveBeenCalledTimes(0);
logger.info(breadcrumb);
expect(scope.addBreadcrumb).toHaveBeenCalledTimes(1);
expect(scope.addBreadcrumb).toHaveBeenCalledWith(breadcrumb.data);
});
});

0 comments on commit 7ddb70e

Please sign in to comment.