File: test.coffee

package info (click to toggle)
node-error-ex 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112 kB
  • sloc: javascript: 108; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 6,093 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
should = require 'should'
errorEx = require '../'

Error.stackTraceLimit = Infinity

it 'should create a default error type', ->
  TestError = errorEx()
  err = new TestError 'herp derp'
  err.should.be.instanceOf TestError
  err.should.be.instanceOf Error
  err.name.should.equal Error.name
  err.message.should.equal 'herp derp'

it 'should create a new error type', ->
  TestError = errorEx 'TestError'
  err = new TestError 'herp derp'
  err.should.be.instanceOf TestError
  err.should.be.instanceOf Error
  err.name.should.equal 'TestError'
  testLine = err.stack.toString().split(/\r?\n/g)[0]
  testLine.should.equal 'TestError: herp derp'

it 'should add a custom property line', ->
  TestError = errorEx 'TestError', foo:line: -> 'bar'
  err = new TestError 'herp derp'
  testLine = err.stack.toString().split(/\r?\n/g)[1]
  testLine.should.equal '    bar'

it 'should allow properties', ->
  TestError = errorEx 'TestError', foo:line: (v)-> "foo #{v}" if v
  err = new TestError 'herp derp'
  testLine = err.stack.toString().split(/\r?\n/g)[1]
  testLine.substr(0, 3).should.not.equal 'foo'
  err.foo = 'bar'
  testLine = err.stack.toString().split(/\r?\n/g)[1]
  testLine.should.equal '    foo bar'

it 'should allow direct editing of the stack', ->
  TestError = errorEx 'TestError',
    foo:stack: (v, stack)-> stack[0] += " #{v}" if v
  err = new TestError 'herp derp'
  err.foo = 'magerp'
  testLine = err.stack.toString().split(/\r?\n/g)[0]
  testLine.should.equal 'TestError: herp derp magerp'

it 'should work on existing errors', ->
  originalErr = new Error 'herp derp'
  TestError = errorEx 'TestError', foo:line: (v)-> "foo #{v}"
  TestError.call originalErr
  originalErr.message.should.equal 'herp derp'
  originalErr.name.should.equal 'TestError'
  originalErr.foo = 'bar'
  testLine = originalErr.stack.toString().split(/\r?\n/g)[1]
  testLine.should.equal '    foo bar'

it 'should take in an existing error to the constructor', ->
  originalErr = new Error 'herp derp'
  TestError = errorEx 'TestError'
  newErr = new TestError originalErr
  newErr.message.should.equal originalErr.message

it 'should allow the editing of the message', ->
  originalErr = new Error 'herp derp'
  TestError = errorEx 'TestError', foo:message: ()-> 'foobar'
  TestError.call originalErr
  originalErr.message.should.equal 'foobar'

it 'should allow the editing of the message (with value)', ->
  originalErr = new Error 'herp derp'
  TestError = errorEx 'TestError', foo:message: (v)-> "foobar #{v}"
  TestError.call originalErr
  originalErr.foo = '1234'
  originalErr.message.should.equal 'foobar 1234'

it 'should allow the editing of the message (multiple lines)', ->
  originalErr = new Error 'herp derp'
  TestError = errorEx 'TestError', foo:message: (v)-> ['hello', "foobar #{v}"]
  TestError.call originalErr
  originalErr.foo = '1234'
  originalErr.message.should.equal 'hello\nfoobar 1234'

it 'should allow the editing of the message (append original)', ->
  originalErr = new Error 'herp derp'
  TestError = errorEx 'TestError', foo:message: (v, message)-> message.concat ['hello, there']
  TestError.call originalErr
  originalErr.message.should.equal 'herp derp\nhello, there'

describe 'helpers', ->
  describe 'append', ->
    it 'should append to the error string', ->
      TestError = errorEx 'TestError', fileName: errorEx.append 'in %s'
      err = new TestError 'error'
      err.fileName = '/a/b/c/foo.txt'
      testLine = err.stack.toString().split(/\r?\n/g)[0]
      testLine.should.equal 'TestError: error in /a/b/c/foo.txt'

    it 'should append to a multi-line error string', ->
      TestError = errorEx 'TestError', fileName: errorEx.append 'in %s'
      err = new TestError 'error\n}\n^'
      err.fileName = '/a/b/c/foo.txt'
      testLine = err.stack.toString().split(/\r?\n/g)[0]
      testLine.should.equal 'TestError: error in /a/b/c/foo.txt'
      err.message.should.equal 'error in /a/b/c/foo.txt\n}\n^'

    it 'should append and use toString()', ->
      TestError = errorEx 'TestError', fileName: errorEx.append 'in %s'
      err = new TestError 'error'
      err.fileName = '/a/b/c/foo.txt'
      err.toString().should.equal 'TestError: error in /a/b/c/foo.txt'
      err.message.should.equal 'error in /a/b/c/foo.txt'

    it 'should append and use toString() on existing error', ->
      TestError = errorEx 'TestError', fileName: errorEx.append 'in %s'
      err = new Error 'error'
      TestError.call err
      err.fileName = '/a/b/c/foo.txt'
      err.toString().should.equal 'TestError: error in /a/b/c/foo.txt'
      err.message.should.equal 'error in /a/b/c/foo.txt'

  describe 'line', ->
    it 'should create a new line', ->
      TestError = errorEx 'TestError', fileName: errorEx.line 'in %s'
      err = new TestError 'error'
      err.fileName = '/a/b/c/foo.txt'
      testLine = err.stack.toString().split(/\r?\n/g)[1]
      testLine.should.equal '    in /a/b/c/foo.txt'

describe 'bluebird support', ->
  it 'should pass the bluebird stack write test', ->
    bluebirdPropertyWritable = (obj, prop)->
        descriptor = Object.getOwnPropertyDescriptor(obj, prop)
        return !!(!descriptor || descriptor.writable || descriptor.set)

    TestError = errorEx 'TestError', fileName: errorEx.line 'in %s'
    err = new TestError 'error'
    err_native = new Error 'hello'

    (bluebirdPropertyWritable err_native, 'stack').should.be.ok()
    (bluebirdPropertyWritable err, 'stack').should.be.ok()

  it 'should allow the stack to be set while preserving custom properties', ->
    TestError = errorEx 'TestError', fileName: errorEx.line 'in %s'

    err = new TestError 'error'
    err.fileName = '/a/b/c/foo.txt'
    testLine = err.stack.toString().split(/\r?\n/g)[1]
    testLine.should.equal '    in /a/b/c/foo.txt'

    err.stack = 'TestError: overridden error\n    at null:1:1'
    err.stack.toString().should.equal 'TestError: overridden error\n    in /a/b/c/foo.txt\n    at null:1:1'

    err.stack = null
    testLine = err.stack.toString().split(/\r?\n/g)[1]
    testLine.should.equal '    in /a/b/c/foo.txt'