index.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict'
  2. const test = require('tap').test
  3. const build = require('..')
  4. process.removeAllListeners('warning')
  5. test('Create warning with zero parameter', t => {
  6. t.plan(3)
  7. const { create } = build()
  8. const buildWarnOpts = create('FastifyWarning', 'CODE', 'Not available')
  9. const opts = buildWarnOpts()
  10. t.equal(opts.name, 'FastifyWarning')
  11. t.equal(opts.message, 'Not available')
  12. t.equal(opts.code, 'CODE')
  13. })
  14. test('Create error with 1 parameter', t => {
  15. t.plan(3)
  16. const { create } = build()
  17. const buildWarningOpts = create('FastifyWarning', 'CODE', 'hey %s')
  18. const opts = buildWarningOpts('alice')
  19. t.equal(opts.name, 'FastifyWarning')
  20. t.equal(opts.message, 'hey alice')
  21. t.equal(opts.code, 'CODE')
  22. })
  23. test('Create error with 2 parameters', t => {
  24. t.plan(3)
  25. const { create } = build()
  26. const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s')
  27. const opts = buildWarnOpts('alice', 'attitude')
  28. t.equal(opts.name, 'FastifyWarning')
  29. t.equal(opts.message, 'hey alice, I like your attitude')
  30. t.equal(opts.code, 'CODE')
  31. })
  32. test('Create error with 3 parameters', t => {
  33. t.plan(3)
  34. const { create } = build()
  35. const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s %s')
  36. const opts = buildWarnOpts('alice', 'attitude', 'see you')
  37. t.equal(opts.name, 'FastifyWarning')
  38. t.equal(opts.message, 'hey alice, I like your attitude see you')
  39. t.equal(opts.code, 'CODE')
  40. })
  41. test('Creates a deprecation warning', t => {
  42. t.plan(3)
  43. const manager = build()
  44. const builder = manager.createDeprecation('CODE', 'hello %s')
  45. const warning = builder('world')
  46. t.equal(warning.name, 'DeprecationWarning')
  47. t.equal(warning.message, 'hello world')
  48. t.equal(warning.code, 'CODE')
  49. })
  50. test('Should throw when error code has no fastify name', t => {
  51. t.plan(1)
  52. const { create } = build()
  53. t.throws(() => create(), new Error('Warning name must not be empty'))
  54. })
  55. test('Should throw when error has no code', t => {
  56. t.plan(1)
  57. const { create } = build()
  58. t.throws(() => create('name'), new Error('Warning code must not be empty'))
  59. })
  60. test('Should throw when error has no message', t => {
  61. t.plan(1)
  62. const { create } = build()
  63. t.throws(() => create('name', 'code'), new Error('Warning message must not be empty'))
  64. })
  65. test('Should throw if emit is called with unknown code ', t => {
  66. t.plan(1)
  67. const { emit } = build()
  68. t.throws(() => emit('CODE'), new Error('The code \'CODE\' does not exist'))
  69. })
  70. test('Cannot reuse the same code more than once', t => {
  71. t.plan(1)
  72. const { create } = build()
  73. create('FastifyWarning', 'CODE', 'Not available')
  74. t.throws(() => create('FastifyWarning', 'CODE', 'Not available'), new Error("The code 'CODE' already exist"))
  75. })
  76. test('Cannot set unlimited other than boolean', t => {
  77. t.plan(1)
  78. const { create } = build()
  79. t.throws(() => create('FastifyWarning', 'CODE', 'Msg', { unlimited: 42 }), new Error('Warning opts.unlimited must be a boolean'))
  80. })