ParsedError.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. chai = require 'chai'
  2. ParsedError = require '../src/ParsedError'
  3. chai.should()
  4. error = (what) ->
  5. if typeof what is 'string'
  6. return error -> throw Error what
  7. else if what instanceof Function
  8. try
  9. do what
  10. return null
  11. catch e
  12. return e
  13. else
  14. throw Error "bad argument for error"
  15. describe "ParsedError", ->
  16. describe "constructor()", ->
  17. it "should accept Error() instances", ->
  18. (-> new ParsedError error -> throw Error "some message").should.not.throw()
  19. it "should accept ReferenceError() and other derivatives of Error()", ->
  20. (-> new ParsedError error -> throw ReferenceError "some message").should.not.throw()
  21. it "should accept non errors", ->
  22. (-> e = new ParsedError 'some string').should.not.throw()
  23. describe "message", ->
  24. it "should return the original error message", ->
  25. e = new ParsedError error 'a'
  26. e.message.should.equal 'a'
  27. describe "multiline message", ->
  28. it "should return the original error message", ->
  29. e = new ParsedError error 'a \n b \n c'
  30. e.message.should.equal 'a \n b \n c'
  31. describe "kind", ->
  32. it "should return 'Error' for normal error", ->
  33. e = new ParsedError error 'a'
  34. e.kind.should.equal 'Error'
  35. it "should recognize 'ReferenceError'", ->
  36. e = new ParsedError error -> a.b = c
  37. e.kind.should.equal 'ReferenceError'
  38. describe "type", ->
  39. it.skip "should return original error type", ->
  40. e = new ParsedError error -> a.b = c
  41. e.type.should.be.equal 'not_defined'
  42. describe "arguments", ->
  43. it.skip "should return original error arguments", ->
  44. e = new ParsedError error -> a.b = c
  45. e.arguments.should.be.eql ['a']
  46. describe "stack", ->
  47. it "should return original error stack", ->
  48. e = new ParsedError error -> a.b = c
  49. e.stack.should.be.equal e.error.stack
  50. describe "trace", ->
  51. it "should include correct information about each trace item", ->
  52. e = new ParsedError error -> a.b = c
  53. e.trace.should.have.length.above 2
  54. item = e.trace[0]
  55. item.should.include.keys 'original',
  56. 'what', 'path', 'addr',
  57. 'file', 'dir', 'col',
  58. 'line', 'jsCol', 'jsLine'
  59. 'packageName', 'shortenedPath', 'shortenedAddr'
  60. item.path.should.equal module.filename.replace(/[\\]+/g, '/')
  61. item.line.should.be.a 'number'
  62. item.col.should.be.a 'number'
  63. describe "_rectifyPath()", ->
  64. it "should work", ->
  65. ParsedError::_rectifyPath('F:/a/node_modules/b/node_modules/d/node_modules/e/f.js').path.should.equal '[a]/[b]/[d]/[e]/f.js'
  66. it "should return path when `node_modules` is not present", ->
  67. ParsedError::_rectifyPath('a/b/c').path.should.equal 'a/b/c'