forked from xpl/stacktracey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
281 lines (188 loc) · 9.83 KB
/
test.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"use strict";
/* ------------------------------------------------------------------------ */
const nodeVersion = Math.floor (Number (process.version.match (/^v(\d+\.\d+)/)[1]))
/* ------------------------------------------------------------------------ */
require ('chai').should ()
/* ------------------------------------------------------------------------ */
describe ('impl/partition', () => {
const partition = require ('./impl/partition')
const spans = partition ([ 'a', 'b', 'c', undefined, undefined, 42], x => typeof x)
spans.should.deep.equal ([ { label: 'string', items: ['a', 'b', 'c'] },
{ label: 'undefined', items: [undefined, undefined] },
{ label: 'number', items: [42] } ])
})
/* ------------------------------------------------------------------------ */
describe ('StackTracey', () => {
const path = require ('path')
const StackTracey = require ('./stacktracey')
StackTracey.resetCache ()
const shouldBeVisibleInStackTrace = () => new StackTracey () // @hide
it ('works', () => {
const stack = shouldBeVisibleInStackTrace ()
stack.should.be.an.instanceof (Array)
stack[0].should.deep.equal ({
beforeParse: 'at shouldBeVisibleInStackTrace (' + path.join (process.cwd (), 'test.js') + ':32:47)',
callee: 'shouldBeVisibleInStackTrace',
index: false,
native: false,
file: path.join (process.cwd (), 'test.js').replace (/\\/g, '/'),
line: 32,
column: 47,
calleeShort: 'shouldBeVisibleInStackTrace',
fileName: 'test.js',
fileRelative: 'test.js',
fileShort: 'test.js',
thirdParty: false
})
})
it ('allows to read sources', () => {
const stack = shouldBeVisibleInStackTrace ().withSources // @hide
stack.should.be.an.instanceof (StackTracey)
stack[0].beforeParse.should.not.be.undefined // should preserve previous fields
stack[0].sourceLine.should.equal (' const shouldBeVisibleInStackTrace = () => new StackTracey () ')
stack[0].hide.should.equal (true) // reads // @hide marker
stack[1].hide.should.equal (true) // reads // @hide marker
const cleanStack = stack.clean
cleanStack.should.be.an.instanceof (StackTracey)
StackTracey.locationsEqual (cleanStack[0], stack[0]).should.equal (true) // should not clean top element
StackTracey.locationsEqual (cleanStack[1], stack[1]).should.equal (false) // should clean second element (due to // @hide)
})
it ('allows creation from array + groups duplicate lines', () => {
const stack = new StackTracey ([
{ file: 'yo.js', line: 11, callee: 'a.funkktion', calleeShort: 'a' },
{ file: 'yo.js', line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' },
{ file: 'yo.js', line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' },
{ file: 'lol.js', line: 10, callee: '', calleeShort: '' },
])
const clean = stack.clean.map (x => Object.assign ({
file: x.file,
line: x.line,
callee: x.callee,
calleeShort: x.calleeShort }))
clean.should.be.an.instanceof (StackTracey)
Array.from (clean).should.deep.equal ([ // .should does not recognize StackTracey as normal array...
{ file: 'yo.js', line: 11, callee: 'a.funkktion', calleeShort: 'a' },
{ file: 'yo.js', line: 10, callee: 'foobar.boobar → foobar.boobar', calleeShort: 'foobar → foobar' },
{ file: 'lol.js', line: 10, callee: '', calleeShort: '' },
])
})
it ('handles inaccessible files', () => {
const stack = shouldBeVisibleInStackTrace ()
stack[0].file = '^___^'
stack.withSources[0].sourceLine.should.equal ('')
stack.withSources[0].error.should.be.an.instanceof (Error)
})
it ('exposes some Array methods', () => {
const stack = shouldBeVisibleInStackTrace ()
const sliced = stack.slice (1)
const deltaLength = (stack.length - sliced.length)
deltaLength.should.equal (1)
sliced.should.be.an.instanceof (StackTracey)
sliced.filter (x => true).should.be.an.instanceof (StackTracey)
})
it ('works with sourcemaps', () => {
const mkay = require ('./test_files/mkay.uglified')
try {
mkay ()
}
catch (e) {
e.message.should.equal ('mkay')
const top = new StackTracey (e).withSources[0]
top.line .should.equal (4)
//top.column .should.equal (23)
top.sourceLine .should.equal ('\t\t\t\t\tthrow new Error (\'mkay\') }')
top.file .should.equal (path.resolve ('./test_files/mkay.js').replace (/\\/g, '/'))
top.fileShort .should.equal ('test_files/mkay.js')
top.fileName .should.equal ('mkay.js')
}
})
it ('pretty printing works', function prettyTest () {
const pretty = new StackTracey ().clean.pretty
console.log ('')
console.log (pretty, '\n')
//const spaces = nodeVersion > 8 ? ' ' : ' ';
console.log (pretty.split ('\n')[0].match (/at prettyTest\s+test.js\:144\s+const pretty = new StackTracey \(\)\.clean\.pretty/))
})
it ('trims too long columns in the pretty printed output', () => {
const stack = new StackTracey ([
{ fileShort: 'dasdasdasdadadadasdasdasdasdasddasdsadadasdassdasdaddadasdas.js', line: 11, calleeShort: 'dadasdasdasdasdasdasdasdasdasdasdasdasd' },
])
stack.pretty.split ('\n')[0].should.equal ('at dadasdasdasdasdasdasdasdasdas… …asdadadadasdasdasdasdasddasdsadadasdassdasdaddadasdas.js:11 ')
})
it ('exposes Array methods', () => {
const stack = new StackTracey ([
{ file: 'foo' },
{ file: 'bar' }
])
const mapped = stack.map ((x, i) => Object.assign (x, { i }))
mapped.should.deep.equal ([ { file: 'foo', i: 0 }, { file: 'bar', i: 1 } ])
mapped.should.be.an.instanceof (Array)
mapped.should.be.an.instanceof (StackTracey)
stack.reduce ((memo, x) => memo + x.file, '').should.equal ('foobar')
const filtered = stack.filter (x => x.file === 'bar')
filtered.length.should.equal (1)
filtered[0].should.deep.equal ({ file: 'bar', i: 1 })
})
it ('computes relative path correctly', () => {
StackTracey.relativePath ('webpack:///~/jquery/dist/jquery.js')
.should.equal ( '~/jquery/dist/jquery.js')
StackTracey.relativePath ('webpack:/webpack/bootstrap')
.should.equal ( 'webpack/bootstrap')
})
it ('computes short path correctly', () => {
StackTracey.shortenPath ('webpack/bootstrap/jquery/dist/jquery.js')
.should.equal ('jquery/dist/jquery.js')
StackTracey.shortenPath ('node_modules/jquery/dist/jquery.js')
.should.equal ('jquery/dist/jquery.js')
})
if (nodeVersion >= 5) {
it ('recognizes SyntaxErrors', () => {
try { require ('./test_files/syntax_error.js') }
catch (e) {
const stack = new StackTracey (e).clean
console.log ('')
console.log (stack.pretty, '\n')
stack[0].syntaxError.should.equal (true)
stack[0].column.should.equal (5)
const spaces = nodeVersion > 8 ? ' ' : ' '
const spaces2 = nodeVersion > 8 ? (nodeVersion > 11 ? ' ' : ' ') : ' '
stack.pretty.split ('\n')[0].should.equal ('at (syntax error)' + spaces + 'test_files/syntax_error.js:2' + spaces2 + 'foo->bar () ')
}
})
}
it ('implements StackTracey.isThirdParty', () => {
StackTracey.isThirdParty.include (path => path === 'test.js')
new StackTracey ()[0].thirdParty.should.equal (true)
StackTracey.isThirdParty.except (path => path === 'test.js')
new StackTracey ()[0].thirdParty.should.equal (false)
})
it ('.withSource', () => {
const line = new StackTracey ().withSource (0).sourceLine.trim ()
line.should.equal ('const line = new StackTracey ().withSource (0).sourceLine.trim ()')
})
it ('.at', () => {
new StackTracey ().at (0).file.includes ('stacktracey/test.js').should.equal (true)
})
it ('detects Array methods as native', () => {
const arr = [1,2,3]
const stack = arr.reduce (() => new StackTracey ())
stack[1].native.should.equal (true)
})
it ('works on Windows', () => {
const dir = process.cwd ()
const windowsStack =
[
'Error',
' at Context.it (' + dir + '\\test.js:38:22)',
' at callFn (' + dir + '\\node_modules\\mocha\\lib\\runnable.js:354:21)',
' at runCallback (timers.js:800:20)'
].join ('\n')
const stack = new StackTracey (windowsStack)
const pretty = stack.pretty
const lines = pretty.split ('\n')
console.log ('')
console.log (pretty, '\n')
lines[0].should.equal ('at it test.js:38 stack.should.be.an.instanceof (Array)')
lines[1].indexOf ('at callFn mocha/lib/runnable.js:354').should.equal (0)
})
})