ParsedError.coffee 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "kind", ->
  28. it "should return 'Error' for normal error", ->
  29. e = new ParsedError error 'a'
  30. e.kind.should.equal 'Error'
  31. it "should recognize 'ReferenceError'", ->
  32. e = new ParsedError error -> a.b = c
  33. e.kind.should.equal 'ReferenceError'
  34. describe "type", ->
  35. it.skip "should return original error type", ->
  36. e = new ParsedError error -> a.b = c
  37. e.type.should.be.equal 'not_defined'
  38. describe "arguments", ->
  39. it.skip "should return original error arguments", ->
  40. e = new ParsedError error -> a.b = c
  41. e.arguments.should.be.eql ['a']
  42. describe "stack", ->
  43. it "should return original error stack", ->
  44. e = new ParsedError error -> a.b = c
  45. e.stack.should.be.equal e.error.stack
  46. describe "trace", ->
  47. it "should include correct information about each trace item", ->
  48. e = new ParsedError error -> a.b = c
  49. e.trace.should.have.length.above 2
  50. item = e.trace[0]
  51. item.should.include.keys 'original',
  52. 'what', 'path', 'addr',
  53. 'file', 'dir', 'col',
  54. 'line', 'jsCol', 'jsLine'
  55. 'packageName', 'shortenedPath', 'shortenedAddr'
  56. item.path.should.equal module.filename.replace(/[\\]+/g, '/')
  57. item.line.should.be.a 'number'
  58. item.col.should.be.a 'number'
  59. describe "_rectifyPath()", ->
  60. it "should work", ->
  61. ParsedError::_rectifyPath('F:/a/node_modules/b/node_modules/d/node_modules/e/f.js').path.should.equal '[a]/[b]/[d]/[e]/f.js'
  62. it "should return path when `node_modules` is not present", ->
  63. ParsedError::_rectifyPath('a/b/c').path.should.equal 'a/b/c'