Skip to content

Commit

Permalink
fix: add tracing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goshander committed Oct 16, 2024
1 parent 606f02e commit 3c0fbdd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/tests/logging.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {NodeKit} from '..';

const logger = {
write: jest.fn(),
const setupNodeKit = () => {
const logger = {
write: jest.fn(),
};

const nodekit = new NodeKit({config: {appLoggingDestination: logger}});

return {nodekit, logger};
};

test('check base logging system', () => {
const nodeKit = new NodeKit({config: {appLoggingDestination: logger}});
const {nodekit, logger} = setupNodeKit();

// log function
nodeKit.ctx.log('log info');
nodekit.ctx.log('log info');
let log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({
Expand All @@ -17,7 +23,7 @@ test('check base logging system', () => {
});

// logError function
nodeKit.ctx.logError('log error');
nodekit.ctx.logError('log error');
log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({
Expand All @@ -28,7 +34,7 @@ test('check base logging system', () => {
// logError function with error object
const err = new Error('error object');

nodeKit.ctx.logError('log error with error object', err);
nodekit.ctx.logError('log error with error object', err);
log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({
Expand All @@ -43,34 +49,34 @@ test('check base logging system', () => {
});

test('check logging with extra data', () => {
const nodeKit = new NodeKit({config: {appLoggingDestination: logger}});
const {nodekit, logger} = setupNodeKit();

const extra = Math.random().toString();

// log function with extra param
nodeKit.ctx.log('log info', {extra});
nodekit.ctx.log('log info', {extra});
let log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({extra});

// add extra data to ctx
const traceId = Math.random().toString();
nodeKit.ctx.addLoggerExtra('traceId', traceId);
nodekit.ctx.addLoggerExtra('traceId', traceId);

// log function with extra ctx data
nodeKit.ctx.log('log info');
nodekit.ctx.log('log info');
log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({traceId});

// log function with extra param and extra ctx data
nodeKit.ctx.log('log info', {extra});
nodekit.ctx.log('log info', {extra});
log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({traceId, extra});

// logError function with extra param and extra ctx data
nodeKit.ctx.logError('log error', new Error('err'), {extra});
nodekit.ctx.logError('log error', new Error('err'), {extra});
log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({
Expand All @@ -81,20 +87,20 @@ test('check logging with extra data', () => {
});

test('check logging from nested ctx', () => {
const nodeKit = new NodeKit({config: {appLoggingDestination: logger}});
const {nodekit, logger} = setupNodeKit();

const traceId = Math.random().toString();
nodeKit.ctx.addLoggerExtra('traceId', traceId);
nodekit.ctx.addLoggerExtra('traceId', traceId);

// log function from parent ctx
nodeKit.ctx.log('log info');
nodekit.ctx.log('log info');
let log = JSON.parse(logger.write.mock.lastCall?.pop() || '{}');

expect(log).toMatchObject({traceId});

const ctxName = Math.random().toString();
const logPostfix = Math.random().toString();
const newCtx = nodeKit.ctx.create(ctxName, {loggerPostfix: logPostfix});
const newCtx = nodekit.ctx.create(ctxName, {loggerPostfix: logPostfix});

// log function from nested ctx
newCtx.log('log info');
Expand Down
40 changes: 40 additions & 0 deletions src/tests/tracing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {SpanContext} from 'opentracing';

import {NodeKit} from '..';

const setupNodeKit = () => {
const nodekit = new NodeKit({
config: {
appTracingEnabled: false,
appTracingServiceName: 'app',
},
});

const traceId = Math.random().toString();
const spanId = Math.random().toString();

jest.spyOn(SpanContext.prototype, 'toTraceId').mockImplementation(() => {
return traceId;
});

jest.spyOn(SpanContext.prototype, 'toSpanId').mockImplementation(() => {
return spanId;
});

return {
nodekit,
tracing: {
traceId,
spanId,
},
};
};

test('check traceId and spanId exist at child ctx', () => {
const {nodekit, tracing} = setupNodeKit();

const ctx = nodekit.ctx.create('app');

expect(ctx.getTraceId()).toBe(tracing.traceId);
expect(ctx.getSpanId()).toBe(tracing.spanId);
});

0 comments on commit 3c0fbdd

Please sign in to comment.