Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove test listeners after the event has happened #9115

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/core/medusa-test-utils/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export const waitSubscribersExecution = (
eventName: string,
eventBus: IEventBusModuleService
) => {
const subscriberPromises: Promise<any>[] = []
const eventEmitter: EventEmitter = (eventBus as any).eventEmitter_
const subscriberPromises: Promise<any>[] = []
const originalListeners = eventEmitter.listeners(eventName)

// If there are no existing listeners, resolve once the event happens. Otherwise, wrap the existing subscribers in a promise and resolve once they are done.
if (!eventEmitter.listeners(eventName).length) {
Expand Down Expand Up @@ -38,5 +39,10 @@ export const waitSubscribersExecution = (
})
}

return Promise.all(subscriberPromises)
return Promise.all(subscriberPromises).finally(() => {
eventEmitter.removeAllListeners(eventName)
originalListeners.forEach((listener) => {
eventEmitter.on(eventName, listener as (...args: any) => void)
})
})
}
Loading