PrettyError.coffee 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. chai = require 'chai'
  2. PrettyError = require '../src/PrettyError'
  3. defaultStyle = require '../src/defaultStyle'
  4. chai.should()
  5. isFormatted = (exc) ->
  6. exc.stack.indexOf(' \u001b[0m\u001b[97m\u001b[41m') is 0
  7. error = (what) ->
  8. if typeof what is 'string'
  9. return error -> throw Error what
  10. else if what instanceof Function
  11. try
  12. do what
  13. catch e
  14. return e
  15. throw Error "bad argument for error"
  16. describe "PrettyError", ->
  17. describe "constructor()", ->
  18. it "should work", ->
  19. new PrettyError
  20. describe "getObject", ->
  21. it "should return a string", ->
  22. p = new PrettyError
  23. p.getObject(error "hello").should.be.an 'object'
  24. describe "style", ->
  25. it "should, by default, return the contents in `default-style`", ->
  26. p = new PrettyError
  27. p.style.should.eql defaultStyle()
  28. it "should return different contents after appending some styles", ->
  29. p = new PrettyError
  30. p.appendStyle 'some selector': 'display': 'block'
  31. p.style.should.not.eql defaultStyle()
  32. describe "render()", ->
  33. it "should work", ->
  34. p = new PrettyError
  35. p.skipNodeFiles()
  36. p.appendStyle 'pretty-error': marginLeft: 4
  37. e = error -> "a".should.equal "b"
  38. console.log p.render e, no
  39. e2 = error -> Array.split(Object)
  40. console.log p.render e2, no
  41. e3 = "Plain error message"
  42. console.log p.render e3, no
  43. e4 =
  44. message: "Custom error message"
  45. kind: "Custom Error"
  46. console.log p.render e4, no
  47. e5 =
  48. message: "Error with custom stack"
  49. stack: ['line one', 'line two']
  50. wrapper: 'UnhandledRejection'
  51. console.log p.render e5, no
  52. e6 = error -> PrettyError.someNonExistingFuncion()
  53. console.log p.render e6, no
  54. it.skip "should render without colors if pe._useColors is false", ->
  55. p = new PrettyError
  56. p.withoutColors()
  57. p.skipNodeFiles()
  58. p.appendStyle 'pretty-error': marginLeft: 4
  59. e = error -> "a".should.equal "b"
  60. console.log p.render e, no
  61. describe "start()", ->
  62. prepareStackTrace = null
  63. beforeEach ->
  64. {prepareStackTrace} = Error
  65. Error.prepareStackTrace = null
  66. afterEach ->
  67. Error.prepareStackTrace = prepareStackTrace
  68. it "throws unformatted error when not started", ->
  69. try
  70. throw new Error "foo bar"
  71. catch exc
  72. isFormatted(exc).should.be.eql false
  73. it "throws formatted the error", ->
  74. PrettyError.start()
  75. try
  76. throw new Error "foo bar"
  77. catch exc
  78. isFormatted(exc).should.be.eql true
  79. exc.stack.split(/\n/g).length.should.be.above 2
  80. PrettyError.stop()