events.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * The `node:events` shim's dispatch semantics. Harness code registers on this
  3. * class through the module proxy table and branches on what it returns, so the
  4. * cases below pin the parts a hand-written emitter gets wrong: the boolean
  5. * `emit` reports, the point at which `once` unregisters, and the listener set an
  6. * in-flight emit dispatches to.
  7. */
  8. import { describe, expect, it } from 'vitest'
  9. import { EventEmitter } from '../../src/node/builtin_modules/implemented/events.ts'
  10. describe('emit', () => {
  11. it('reports whether the event reached a listener', () => {
  12. const emitter = new EventEmitter()
  13. expect(emitter.emit('ready')).toBe(false)
  14. emitter.on('ready', () => {})
  15. expect(emitter.emit('ready')).toBe(true)
  16. emitter.removeAllListeners('ready')
  17. expect(emitter.emit('ready')).toBe(false)
  18. })
  19. it('calls the listeners in registration order with every argument', () => {
  20. const emitter = new EventEmitter()
  21. const seen: string[] = []
  22. emitter.on('data', (...args) => { seen.push(`first:${args.join(',')}`) })
  23. emitter.on('data', (...args) => { seen.push(`second:${args.join(',')}`) })
  24. emitter.emit('data', 'a', 1, true)
  25. expect(seen).toEqual(['first:a,1,true', 'second:a,1,true'])
  26. })
  27. it('puts a prepended listener ahead of the ones already registered', () => {
  28. const emitter = new EventEmitter()
  29. const seen: string[] = []
  30. emitter.on('data', () => { seen.push('registered') })
  31. emitter.prependListener('data', () => { seen.push('prepended') })
  32. emitter.emit('data')
  33. expect(seen).toEqual(['prepended', 'registered'])
  34. })
  35. it('dispatches to the listeners present when the emit began', () => {
  36. // Node dispatches over a copy, so a removal from inside a listener takes
  37. // effect on the NEXT emit; a shim that iterated the live list would skip the
  38. // second listener here.
  39. const emitter = new EventEmitter()
  40. const seen: string[] = []
  41. const second = (): void => { seen.push('second') }
  42. emitter.on('data', () => {
  43. seen.push('first')
  44. emitter.off('data', second)
  45. })
  46. emitter.on('data', second)
  47. emitter.emit('data')
  48. emitter.emit('data')
  49. expect(seen).toEqual(['first', 'second', 'first'])
  50. })
  51. })
  52. describe('once', () => {
  53. it('unregisters before it calls, so a re-entrant emit does not repeat it', () => {
  54. const emitter = new EventEmitter()
  55. let calls = 0
  56. emitter.once('settled', () => {
  57. calls += 1
  58. // A listener that reacts by emitting the same event is the shape that
  59. // re-enters a once listener whose removal happens after the call.
  60. emitter.emit('settled')
  61. })
  62. emitter.emit('settled')
  63. expect(calls).toBe(1)
  64. expect(emitter.listenerCount('settled')).toBe(0)
  65. })
  66. it('passes the emit arguments through the wrapper', () => {
  67. const emitter = new EventEmitter()
  68. const seen: unknown[][] = []
  69. emitter.once('exit', (...args) => { seen.push(args) })
  70. emitter.emit('exit', 3, 'SIGTERM')
  71. expect(seen).toEqual([[3, 'SIGTERM']])
  72. })
  73. it('unregisters through the original listener, not only the wrapper', () => {
  74. // Node reaches the once wrapper by the listener handed to `once`, so a caller
  75. // that never saw the wrapper can still cancel its own registration.
  76. const emitter = new EventEmitter()
  77. let calls = 0
  78. const listener = (): void => { calls += 1 }
  79. emitter.once('ready', listener)
  80. emitter.off('ready', listener)
  81. expect(emitter.listenerCount('ready')).toBe(0)
  82. expect(emitter.emit('ready')).toBe(false)
  83. expect(calls).toBe(0)
  84. })
  85. })
  86. describe('registration bookkeeping', () => {
  87. it('hands out a copy of the listener list', () => {
  88. const emitter = new EventEmitter()
  89. emitter.on('data', () => {})
  90. emitter.listeners('data').length = 0
  91. expect(emitter.listenerCount('data')).toBe(1)
  92. })
  93. it('clears one event by name and every event without one', () => {
  94. const emitter = new EventEmitter()
  95. emitter.on('data', () => {})
  96. emitter.on('error', () => {})
  97. emitter.removeAllListeners('data')
  98. expect([emitter.listenerCount('data'), emitter.listenerCount('error')]).toEqual([0, 1])
  99. emitter.removeAllListeners()
  100. expect(emitter.listenerCount('error')).toBe(0)
  101. })
  102. it('removes only the listener named, and tolerates one that never registered', () => {
  103. const emitter = new EventEmitter()
  104. const kept = (): void => {}
  105. const dropped = (): void => {}
  106. emitter.on('data', kept).on('data', dropped)
  107. emitter.removeListener('data', dropped)
  108. emitter.off('data', (): void => {})
  109. emitter.off('absent', kept)
  110. expect(emitter.listeners('data')).toEqual([kept])
  111. })
  112. it('removes the last registration of a listener added twice', () => {
  113. // Removal searches from the tail and stops at one match, as Node does, so
  114. // the earlier registration is the one that stays — visible here in the order
  115. // the surviving listeners run.
  116. const emitter = new EventEmitter()
  117. const seen: string[] = []
  118. const repeated = (): void => { seen.push('repeated') }
  119. emitter.on('data', repeated)
  120. emitter.on('data', () => { seen.push('other') })
  121. emitter.on('data', repeated)
  122. emitter.off('data', repeated)
  123. emitter.emit('data')
  124. expect(seen).toEqual(['repeated', 'other'])
  125. })
  126. })