-
Notifications
You must be signed in to change notification settings - Fork 22
/
test.js
356 lines (247 loc) · 12.7 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"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 (StackTracey)
stack.items[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,
externalDomain: undefined
})
})
it ('allows to read sources', () => {
const stack = shouldBeVisibleInStackTrace ().withSources () // @hide
stack.should.be.an.instanceof (StackTracey)
stack.items[0].beforeParse.should.not.be.undefined // should preserve previous fields
stack.items[0].sourceLine.should.equal (' const shouldBeVisibleInStackTrace = () => new StackTracey () ')
stack.items[0].hide.should.equal (true) // reads // @hide marker
stack.items[1].hide.should.equal (true) // reads // @hide marker
const cleanStack = stack.clean ()
cleanStack.should.be.an.instanceof (StackTracey)
StackTracey.locationsEqual (cleanStack.items[0], stack.items[0]).should.equal (true) // should not clean top element
StackTracey.locationsEqual (cleanStack.items[1], stack.items[1]).should.equal (false) // should clean second element (due to // @hide)
})
it ('allows to read sources (async)', () => {
return shouldBeVisibleInStackTrace ().withSourcesAsync ().then (stack => { // @hide
stack.should.be.an.instanceof (StackTracey)
stack.items[0].beforeParse.should.not.be.undefined // should preserve previous fields
stack.items[0].sourceLine.should.equal (' const shouldBeVisibleInStackTrace = () => new StackTracey () ')
stack.items[0].hide.should.equal (true) // reads // @hide marker
stack.items[1].hide.should.equal (true) // reads // @hide marker
return stack.cleanAsync ().then (cleanStack => {
cleanStack.should.be.an.instanceof (StackTracey)
StackTracey.locationsEqual (cleanStack.items[0], stack.items[0]).should.equal (true) // should not clean top element
StackTracey.locationsEqual (cleanStack.items[1], stack.items[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)
clean.items.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.items[0].file = '^___^'
stack.withSources ().items[0].sourceLine.should.equal ('')
stack.withSourceAt (0).error.should.be.an.instanceof (Error)
})
it ('exposes some Array methods', () => {
const stack = shouldBeVisibleInStackTrace ()
const sliced = stack.slice (1)
const deltaLength = (stack.items.length - sliced.items.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).withSourceAt (0)
top.line .should.equal (4)
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 ().asTable ()
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.withSources ().asTable ().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.items.should.deep.equal ([ { file: 'foo', i: 0 }, { file: 'bar', i: 1 } ])
mapped.should.be.an.instanceof (StackTracey)
const filtered = stack.filter (x => x.file === 'bar')
filtered.items.length.should.equal (1)
filtered.items[0].should.deep.equal ({ file: 'bar', i: 1 })
})
it ('computes relative path correctly', () => {
StackTracey.prototype.decomposePath ('webpack:///~/jquery/dist/jquery.js')
.should.deep.equal ( ['~/jquery/dist/jquery.js', undefined])
StackTracey.prototype.decomposePath ('webpack:/webpack/bootstrap')
.should.deep.equal ( ['webpack/bootstrap', undefined])
})
it ('computes short path correctly', () => {
StackTracey.prototype.shortenPath ('webpack/bootstrap/jquery/dist/jquery.js')
.should.equal ('jquery/dist/jquery.js')
StackTracey.prototype.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.asTable (), '\n')
stack.items[0].syntaxError.should.equal (true)
stack.items[0].column.should.equal (5)
const spaces = nodeVersion > 8 ? ' ' : ' '
const spaces2 = nodeVersion > 8 ? (nodeVersion > 11 ? ' ' : ' ') : ' '
stack.asTable ().split ('\n')[0].should.equal ('at (syntax error)' + spaces + 'test_files/syntax_error.js:2' + spaces2 + 'foo->bar () ')
}
})
}
it ('allows to override isThirdParty()', () => {
class MyStackTracey extends StackTracey {
isThirdParty (path) {
return super.isThirdParty (path) || (path === 'test.js')
}
}
new MyStackTracey ().items[0].thirdParty.should.equal (true)
class MyStackTracey2 extends MyStackTracey {
isThirdParty (path) {
return super.isThirdParty (path) && (path !== 'test.js')
}
}
new MyStackTracey2 ().items[0].thirdParty.should.equal (false)
})
it ('.withSourceAt', () => {
const line = new StackTracey ().withSourceAt (0).sourceLine.trim ()
line.should.equal ('const line = new StackTracey ().withSourceAt (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.items[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.withSources ().asTable ()
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 (StackTracey)')
lines[1].indexOf ('at callFn mocha/lib/runnable.js:354').should.equal (0)
})
it ('parses "eval at" stuff', () => {
function bar() {
const entry = new StackTracey().items[1]
entry.callee.should.equal('eval')
entry.fileName.should.equal('test.js')
}
function foo() {
eval('bar()')
}
foo()
})
it ('recognizes externalDomain', () => {
const stack =
[
'Error',
' at foo (test.js:38:22)',
' at bar (http://shmoogle.google.com/hohoho/test.js:38:22)',
].join ('\n')
const items = new StackTracey(stack).items
;(items[0].externalDomain === undefined).should.be.true
items[1].externalDomain.should.equal('shmoogle.google.com')
items[0].thirdParty.should.be.false
items[1].thirdParty.should.be.true
items[1].fileRelative.should.equal('hohoho/test.js')
})
it ('recognizes locations without column', () => {
const stack = [
'Error',
' at ValidateCompatibilityWithBindGroupLayout (../../third_party/dawn/src/dawn_native/ShaderModule.cpp:395)',
].join ('\n')
const items = new StackTracey(stack).items
items[0].should.contain({
callee: 'ValidateCompatibilityWithBindGroupLayout',
calleeShort: 'ValidateCompatibilityWithBindGroupLayout',
fileRelative: '../../third_party/dawn/src/dawn_native/ShaderModule.cpp',
fileShort: '../../third_party/dawn/src/dawn_native/ShaderModule.cpp',
fileName: 'ShaderModule.cpp'
})
})
})