Fix removeAllListeners

This commit is contained in:
2026-08-10 02:32:25 +00:00
parent 57e809157b
commit ef797a5f20
2 changed files with 36 additions and 3 deletions
+29
View File
@@ -305,6 +305,31 @@ const testEventEmitterRemoveAllListeners = (): void => {
expect(countListener).not.toHaveBeenCalled();
};
/**
* Tests that removeAllListeners() cancels a pending debounced callback.
*/
const testEventEmitterRemoveAllListenersCancelsPendingDebouncedCallback = async (): Promise<void> => {
vi.useFakeTimers();
try {
const emitter = new EventEmitter<TestEvents>();
const listener = vi.fn();
// Arm a debounce timer, then clear every listener before it expires.
emitter.on('message', listener, 100);
emitter.emit('message', 'should not arrive');
emitter.removeAllListeners();
await vi.advanceTimersByTimeAsync(100);
// Cleared listeners must not receive delayed debounced delivery.
expect(listener).not.toHaveBeenCalled();
} finally {
vi.useRealTimers();
vi.restoreAllMocks();
}
};
/**
* Tests that waitFor() resolves when a matching event is emitted.
*/
@@ -658,6 +683,10 @@ const runTests = async (): Promise<void> => {
testEventEmitterOffCancelsPendingDebouncedCallback,
);
test('EventEmitter: removes all listeners when removeAllListeners is called', testEventEmitterRemoveAllListeners);
test(
'EventEmitter: cancels a pending debounced callback when removeAllListeners is called',
testEventEmitterRemoveAllListenersCancelsPendingDebouncedCallback,
);
test('EventEmitter: resolves waitFor when a matching event is emitted', testEventEmitterWaitForResolvesOnMatch);
test('EventEmitter: ignores non-matching events while waiting with waitFor', testEventEmitterWaitForIgnoresNonMatchingEvents);
test('EventEmitter: rejects waitFor when the timeout is reached', testEventEmitterWaitForRejectsOnTimeout);