Skip to content

Maintenance: Parser getTestEvent function can be simplified #3305

Closed
@svozza

Description

@svozza

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

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

internalPRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.)

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions