o-error-util.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const { getFullInfo, getFullStack, hasCauseInstanceOf } = require('..')
  2. const { expect } = require('chai')
  3. describe('OError.getFullInfo', function () {
  4. it('works on a normal error', function () {
  5. const err = new Error('foo')
  6. expect(getFullInfo(err)).to.deep.equal({})
  7. })
  8. it('works on an error with .info', function () {
  9. const err = new Error('foo')
  10. err.info = { userId: 123 }
  11. expect(getFullInfo(err)).to.deep.equal({ userId: 123 })
  12. })
  13. it('merges info from a cause chain', function () {
  14. const err1 = new Error('foo')
  15. const err2 = new Error('bar')
  16. err1.cause = err2
  17. err2.info = { userId: 123 }
  18. expect(getFullInfo(err1)).to.deep.equal({ userId: 123 })
  19. })
  20. it('merges info from a cause chain with no info', function () {
  21. const err1 = new Error('foo')
  22. const err2 = new Error('bar')
  23. err1.cause = err2
  24. expect(getFullInfo(err1)).to.deep.equal({})
  25. })
  26. it('merges info from a cause chain with duplicate keys', function () {
  27. const err1 = new Error('foo')
  28. const err2 = new Error('bar')
  29. err1.cause = err2
  30. err1.info = { userId: 123 }
  31. err2.info = { userId: 456 }
  32. expect(getFullInfo(err1)).to.deep.equal({ userId: 123 })
  33. })
  34. it('works on an error with .info set to a string', function () {
  35. const err = new Error('foo')
  36. err.info = 'test'
  37. expect(getFullInfo(err)).to.deep.equal({})
  38. })
  39. })
  40. describe('OError.getFullStack', function () {
  41. it('works on a normal error', function () {
  42. const err = new Error('foo')
  43. const fullStack = getFullStack(err)
  44. expect(fullStack).to.match(/^Error: foo$/m)
  45. expect(fullStack).to.match(/^\s+at /m)
  46. })
  47. it('works on an error with a cause', function () {
  48. const err1 = new Error('foo')
  49. const err2 = new Error('bar')
  50. err1.cause = err2
  51. const fullStack = getFullStack(err1)
  52. expect(fullStack).to.match(/^Error: foo$/m)
  53. expect(fullStack).to.match(/^\s+at /m)
  54. expect(fullStack).to.match(/^caused by: Error: bar$/m)
  55. })
  56. })
  57. describe('OError.hasCauseInstanceOf', function () {
  58. it('works on a normal error', function () {
  59. const err = new Error('foo')
  60. expect(hasCauseInstanceOf(null, Error)).to.be.false
  61. expect(hasCauseInstanceOf(err, Error)).to.be.true
  62. expect(hasCauseInstanceOf(err, RangeError)).to.be.false
  63. })
  64. it('works on an error with a cause', function () {
  65. const err1 = new Error('foo')
  66. const err2 = new RangeError('bar')
  67. err1.cause = err2
  68. expect(hasCauseInstanceOf(err1, Error)).to.be.true
  69. expect(hasCauseInstanceOf(err1, RangeError)).to.be.true
  70. expect(hasCauseInstanceOf(err1, TypeError)).to.be.false
  71. })
  72. })