Skip to content

Commit

Permalink
fix: ensure baseFormatter.format is called when event name is not rec…
Browse files Browse the repository at this point in the history
…ognized; test: add mock for baseFormatter to verify format method calls
  • Loading branch information
rorteg committed Sep 3, 2024
1 parent 69653fd commit 4f85225
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/core/log-standard-event-formatter-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ export class LogStandardEventFormatterDecorator implements FormatterInterface {
!args.global_event_name ||
!this.isKnownEventName(args.global_event_name)
) {
return JSON.stringify({
...formattedMessage,
context: args?.context || {},
});
return this.baseFormatter.format(message, level, args);
}

args.context = args.context || {};
Expand All @@ -77,4 +74,4 @@ export class LogStandardEventFormatterDecorator implements FormatterInterface {
context: args.context,
});
}
}
}
18 changes: 17 additions & 1 deletion tests/unit/log-standard-event-formatter-decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('Formatter', () => {
beforeEach(() => {
mockFormatter = {
serviceName: 'service',
format: jest.fn()
} as unknown as jest.Mocked<FormatterInterface>;

logStandardFormatterDecorator = new LogStandardEventFormatterDecorator(mockFormatter);
Expand All @@ -20,6 +21,17 @@ describe('Formatter', () => {
});

it('Should format correctly with a generic event name', () => {
mockFormatter.format.mockReturnValue(
JSON.stringify({
message: 'mensagem',
level: LogLevel.info,
global_event_timestamp: '2021-01-01T03:00:00.000Z',
service_name: 'service',
global_event_name: 'teste',
context: {},
})
);

const response = logStandardFormatterDecorator.format('mensagem', LogLevel.info, {
global_event_name: 'teste',
});
Expand All @@ -30,10 +42,14 @@ describe('Formatter', () => {
global_event_timestamp: '2021-01-01T03:00:00.000Z',
service_name: 'service',
global_event_name: 'teste',
context: {}
context: {},
});

expect(response).toBe(expected);

expect(mockFormatter.format).toHaveBeenCalledWith('mensagem', LogLevel.info, {
global_event_name: 'teste',
});
});

it('Should format correctly when PROCESS_STARTUP_FAILED is sent with all required fields and correct log level', () => {
Expand Down

0 comments on commit 4f85225

Please sign in to comment.