emit-unlimited.test.js 775 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict'
  2. const test = require('tap').test
  3. const build = require('..')
  4. test('emit should emit a given code unlimited times', t => {
  5. t.plan(50)
  6. const { create, emit, emitted } = build()
  7. let runs = 0
  8. const expectedRun = []
  9. const times = 10
  10. process.on('warning', onWarning)
  11. function onWarning (warning) {
  12. t.equal(warning.name, 'FastifyDeprecation')
  13. t.equal(warning.code, 'CODE')
  14. t.equal(warning.message, 'Hello world')
  15. t.ok(emitted.get('CODE'))
  16. t.equal(runs++, expectedRun.shift())
  17. }
  18. create('FastifyDeprecation', 'CODE', 'Hello world', { unlimited: true })
  19. for (let i = 0; i < times; i++) {
  20. expectedRun.push(i)
  21. emit('CODE')
  22. }
  23. setImmediate(() => {
  24. process.removeListener('warning', onWarning)
  25. t.end()
  26. })
  27. })