-
Notifications
You must be signed in to change notification settings - Fork 171
Description
Summary
The packages/batch/tests/helpers/handlers.ts
file contains multiple duplicate function implementations that violate the DRY (Don't Repeat Yourself) principle. SonarQube has identified 3 instances where functions have identical implementations:
- Line 17: Function identical to the one on line 8 (
sqsRecordHandler
vsasyncSqsRecordHandler
pattern) - Line 37: Function identical to the one on line 26 (
kinesisRecordHandler
vsasyncKinesisRecordHandler
pattern) - Line 57: Function identical to the one on line 46 (
dynamodbRecordHandler
vsasyncDynamodbRecordHandler
pattern)
Each pair of functions follows the same pattern where a synchronous handler and its asynchronous counterpart have nearly identical logic, differing only in their return type (string
/object
vs Promise<string>
/Promise<object>
).
Why is this needed?
- Code Quality: Eliminates code duplication and improves maintainability
- SonarQube Compliance: Resolves 3 MAJOR severity issues (typescript:S4144) that are currently flagged
- Consistency: Establishes a pattern for creating test handlers that can be reused across the batch processing tests
- Maintenance Burden: Reduces the risk of inconsistencies when updating test logic, as changes would only need to be made in one place
Currently, any changes to the test logic require updating multiple functions, increasing the chance of introducing bugs or inconsistencies.
Which area does this relate to?
Batch Processing, Tests
Solution
Important
The following changes are included as reference to help you understand the refactoring. Before implementing, please make sure to check the codebase and ensure that they make sense and they are exhaustive.
Refactor the duplicate functions using a base handler with async wrapper approach:
const baseSqsHandler = (record: SQSRecord): string => {
const body = record.body;
if (body.includes('fail')) {
throw Error('Failed to process record.');
}
return body;
};
const sqsRecordHandler = baseSqsHandler;
const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> =>
Promise.resolve(baseSqsHandler(record));
const baseKinesisHandler = (record: KinesisStreamRecord): string => {
const body = record.kinesis.data;
if (body.includes('fail')) {
throw Error('Failed to process record.');
}
return body;
};
const kinesisRecordHandler = baseKinesisHandler;
const asyncKinesisRecordHandler = async (record: KinesisStreamRecord): Promise<string> =>
Promise.resolve(baseKinesisHandler(record));
const baseDynamodbHandler = (record: DynamoDBRecord): object => {
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
if (body.S?.includes('fail')) {
throw Error('Failed to process record.');
}
return body;
};
const dynamodbRecordHandler = baseDynamodbHandler;
const asyncDynamodbRecordHandler = async (record: DynamoDBRecord): Promise<object> =>
Promise.resolve(baseDynamodbHandler(record));
This approach:
- Maintains clear, readable function signatures
- Minimizes changes to existing test code
- Clearly separates sync and async behavior
- Is easy to understand and maintain
Implementation Details
-
Files to modify:
packages/batch/tests/helpers/handlers.ts
-
Testing:
- Ensure all existing tests continue to pass
- Verify both sync and async handlers behave identically
- Run SonarQube analysis to confirm issues are resolved
-
Validation:
- Run
npm run test:unit -w packages/batch
to ensure no regressions - Run
npm run lint -w packages/batch
to verify code style compliance
- Run
Additional Context
This issue was identified as part of a SonarQube code quality review. The batch package currently has only 3 remaining open SonarQube issues, all of which are these duplicate function implementations. Resolving this will bring the batch package to zero open code quality issues.
SonarQube Issue Keys:
AYmMjm3ioaCZcgqYG06t
(Line 17)AYmMjm3ioaCZcgqYG06u
(Line 37)AYmMjm3ioaCZcgqYG06v
(Line 57)
Acknowledgment
- This request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status