Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Some of the event handlers cannot be unit tested #49

Open
onderceylan opened this issue Mar 7, 2022 · 1 comment
Open

Some of the event handlers cannot be unit tested #49

onderceylan opened this issue Mar 7, 2022 · 1 comment

Comments

@onderceylan
Copy link
Contributor

Hi again!

I'd like to cover the functionality of the error handlers of my implementation with unit tests, but some of the error handlers are not reachable. AFAIK it's not possible to test them with unit tests currently, as we can't reproduce error scenarios with the public interface of SqsConsumer.

Please see the code below for examples of unreachable code blocks;

sqsConsumer.on(SqsConsumerEvents.error, err => {
  errorHandler('error', queueType, err, logger);
});

sqsConsumer.on(SqsConsumerEvents.connectionError, err => {
  errorHandler('connection_error', queueType, err, logger);
});

sqsConsumer.on(SqsConsumerEvents.s3PayloadError, err => {
  errorHandler('s3_payload_error', queueType, err, logger);
});

Proposition for the solution

To make libraries which depend on event handlers more test friendly, the interfaces that implement EventEmitter might be either updated to publicly expose the EventEmitter instance, or make the class inherit the EventEmitter interface.

Once such a change is in place, it would be possible to trigger the events hence test the event handlers like below example;

sqsConsumer.emit(SqsConsumerEvents.error, new Error('test'));

Please let me know what you think, and if you'd be interested in a PR.
Cheers!

@habmic
Copy link
Member

habmic commented Mar 8, 2022

Hi @onderceylan thank you for opening an issue, I would be happy to make it more testable. Would you be interested in creating a PR for it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants