Skip to content

Commit 96e5b60

Browse files
committed
Added tests for logger
1 parent bd9d833 commit 96e5b60

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packages/batch/src/BatchProcessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class BatchProcessor extends BasePartialBatchProcessor {
228228
const errorMessage = issues
229229
.map((issue) => `${issue?.path?.join('.')}: ${issue.message}`)
230230
.join('; ');
231-
this.logger.error(errorMessage);
231+
this.logger.debug(errorMessage);
232232
throw new Error(errorMessage);
233233
}
234234

packages/batch/tests/unit/BatchProcessor.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,5 +675,58 @@ describe('Class: AsyncBatchProcessor', () => {
675675
FullBatchFailureError
676676
);
677677
});
678+
679+
it('uses a custom logger when provided', async () => {
680+
// Prepare
681+
const logger = {
682+
debug: vi.fn(),
683+
info: vi.fn(),
684+
warn: vi.fn(),
685+
error: vi.fn(),
686+
};
687+
const unsupportedSchema = v.object({
688+
Message: v.string(),
689+
});
690+
const firstRecord = sqsRecordFactory(JSON.stringify(successPayload1));
691+
const secondRecord = sqsRecordFactory(JSON.stringify(successPayload2));
692+
const records = [firstRecord, secondRecord];
693+
const processor = new BatchProcessor(EventType.SQS, {
694+
innerSchema: unsupportedSchema,
695+
transformer: 'json',
696+
logger,
697+
});
698+
699+
// Act
700+
processor.register(records, sqsRecordHandler, options);
701+
702+
// Assess
703+
await expect(processor.process()).rejects.toThrowError(
704+
FullBatchFailureError
705+
);
706+
expect(logger.error).toHaveBeenCalledWith(
707+
'The schema provided is not supported. Only Zod schemas are supported for extension.'
708+
);
709+
});
710+
711+
it('emits debug logs when AWS_LAMBDA_LOG_LEVEL is set to DEBUG', async () => {
712+
// Prepare
713+
const consoleSpy = vi.spyOn(console, 'debug');
714+
vi.stubEnv('AWS_LAMBDA_LOG_LEVEL', 'DEBUG');
715+
const firstRecord = sqsRecordFactory(JSON.stringify(failurePayload1));
716+
const records = [firstRecord];
717+
const processor = new BatchProcessor(EventType.SQS, {
718+
innerSchema: customSchema,
719+
transformer: 'json',
720+
});
721+
722+
// Act
723+
processor.register(records, sqsRecordHandler, options);
724+
725+
// Assess
726+
await expect(processor.process()).rejects.toThrowError(
727+
FullBatchFailureError
728+
);
729+
expect(consoleSpy).toHaveBeenCalled();
730+
});
678731
});
679732
});

0 commit comments

Comments
 (0)