diff --git a/lib/common/utils.ts b/lib/common/utils.ts index 65f1096e7..5b2e82fbb 100644 --- a/lib/common/utils.ts +++ b/lib/common/utils.ts @@ -253,6 +253,9 @@ export function makeZoneAwareListeners(fnName: string) { return function zoneAwareEventListeners(self: any, args: any[]) { const eventName: string = args[0]; const target = self || _global; + if (!target[EVENT_TASKS]) { + return []; + } return target[EVENT_TASKS] .filter(task => task.data.eventName === eventName) .map(task => task.data.handler); diff --git a/test/node/events.spec.ts b/test/node/events.spec.ts index c32f76a9a..ae856ca61 100644 --- a/test/node/events.spec.ts +++ b/test/node/events.spec.ts @@ -58,4 +58,9 @@ describe('nodejs EventEmitter', () => { }); expect(emitter.listeners('test')).toEqual([expectZoneA, shouldNotRun]); }); + it('should return empty array when an event has no listeners', () => { + zoneA.run(() => { + expect(emitter.listeners('test')).toEqual([]); + }); + }) }); \ No newline at end of file