Closed
Description
Summary
While working on #3301, I noticed that the getTestEvent
function is doing synchronous reads, file path resolution and JSON parsing when loading in the JSON test events. While this works, it is possible to load JSON files directly in TypeScript and let the runtime take care all that housekeeping by using dynamic import:
export const getTestEvent = <T extends Record<string, unknown>>({
eventsPath,
filename,
}: {
eventsPath: string;
filename: string;
}): T =>
JSON.parse(
readFileSync(
join(__dirname, '..', '..', 'events', eventsPath, `${filename}.json`),
'utf-8'
)
) as T;
// becomes
export const getTestEvent = async <T extends Record<string, unknown>>({
eventsPath,
filename,
}: {
eventsPath: string;
filename: string;
}): Promise<T> => {
const { default: event } = await import(`../../events/${eventsPath}/${filename}.json`);
return event as T;
};
This does mean that getTestEvent
will now be an async function so we'd have to change all the tests to reflect this. Again, I'm happy to implement this if we decide to do it.
Why is this needed?
Using the built in module resolution is simpler than hand-rolling it ourselves.
Which area does this relate to?
Parser
Solution
No response
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
Closed