Skip to content

Commit

Permalink
fix: createLumberjackLogFormatter failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoVazquez committed Nov 11, 2023
1 parent 70ba887 commit fa79538
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { LumberjackTimeService } from '../time/lumberjack-time.service';
* API.
*/
@Injectable()
export class LumberjackOrchestrator<TPayload extends LumberjackLogPayload | void = void> implements Lumberjack<TPayload> {
export class LumberjackOrchestrator<TPayload extends LumberjackLogPayload | void = void>
implements Lumberjack<TPayload>
{
/**
* The registered drivers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,59 @@ const logFormattingErrorScope = 'LumberjackLogFormattingError';
describe(createLumberjackLogFormatter.name, () => {
function setup(options?: LumberjackOptions) {
const fakeTime = createFakeTime();
const formatter = createLumberjackLogFormatter({
const formatLog = createLumberjackLogFormatter({
getUnixEpochTicks: fakeTime.getUnixEpochTicks,
config: createLumberjackConfig(false, options),
});

return {
fakeTime,
service: formatter,
formatLog,
};
}

describe('Log', () => {
it('returns the same log when formatting succeeds', () => {
const { service, fakeTime } = setup();
const { formatLog, fakeTime } = setup();
const expectedLog = createErrorLogBuilder(fakeTime.getUnixEpochTicks)('').build();

const { log: actualLog } = service.formatLog(expectedLog);
const { log: actualLog } = formatLog(expectedLog);

expect(actualLog).toBe(expectedLog);
});

it('returns an error log when formatting fails', () => {
const formatterErrorMessage = 'TestFormatter';
const { service, fakeTime } = setup({
const { formatLog, fakeTime } = setup({
format: () => {
throw new Error(formatterErrorMessage);
},
});
const debugLog = createDebugLogBuilder(fakeTime.getUnixEpochTicks)('Test debug message').build();
const expectedLog = createFormattingErrorLog(formatterErrorMessage, debugLog, fakeTime.getUnixEpochTicks);

const { log: actualLog } = service.formatLog(debugLog);
const { log: actualLog } = formatLog(debugLog);

expect(actualLog).toEqual(expectedLog);
});
});

describe('Formatted message', () => {
it('returns the formatted log when formatting succeeds', () => {
const { fakeTime, service } = setup({
const { fakeTime, formatLog } = setup({
format: ({ level }) => level,
});
const warning = createWarningLogBuilder(fakeTime.getUnixEpochTicks)('').build();

const { formattedLog: actualFormattedLog } = service.formatLog(warning);
const { formattedLog: actualFormattedLog } = formatLog(warning);

expect(actualFormattedLog).toBe(LumberjackLevel.Warning);
});

describe('Error message', () => {
it('returns a format error when formatting fails because of an Error', () => {
const formatterErrorMessage = 'TestFormatter';
const { fakeTime, service } = setup({
const { fakeTime, formatLog } = setup({
format: () => {
throw new Error(formatterErrorMessage);
},
Expand All @@ -95,7 +95,7 @@ describe(createLumberjackLogFormatter.name, () => {
fakeTime.getUnixEpochTicks
);

const { formattedLog: actualFormattedLog } = service.formatLog(criticalLog);
const { formattedLog: actualFormattedLog } = formatLog(criticalLog);

expect(actualFormattedLog).toBe(
`${formattingErrorLog.level} ${nowTimestamp} [${logFormattingErrorScope}] ${formattingErrorLog.message}`
Expand All @@ -104,7 +104,7 @@ describe(createLumberjackLogFormatter.name, () => {

it('returns a format error when formatting fails with a string error message', () => {
const formatterErrorMessage = 'TestFormatter';
const { fakeTime, service } = setup({
const { fakeTime, formatLog } = setup({
format: () => {
throw formatterErrorMessage;
},
Expand All @@ -119,7 +119,7 @@ describe(createLumberjackLogFormatter.name, () => {
fakeTime.getUnixEpochTicks
);

const { formattedLog: actualFormattedLog } = service.formatLog(criticalLog);
const { formattedLog: actualFormattedLog } = formatLog(criticalLog);

expect(actualFormattedLog).toBe(
`${formattingErrorLog.level} ${nowTimestamp} [${logFormattingErrorScope}] ${formattingErrorLog.message}`
Expand Down

0 comments on commit fa79538

Please sign in to comment.