1 | "use strict"; |
||
2 | |||
3 | /* ------------------------------------------------------------------------ */ |
||
4 | |||
5 | require ('chai').should () |
||
6 | |||
7 | /* ------------------------------------------------------------------------ */ |
||
8 | |||
9 | describe ('impl/partition', () => { |
||
10 | |||
11 | const partition = require ('./impl/partition') |
||
12 | const spans = partition ([ 'a', 'b', 'c', undefined, undefined, 42], x => typeof x) |
||
13 | |||
14 | spans.should.deep.equal ([ { label: 'string', items: ['a', 'b', 'c'] }, |
||
15 | { label: 'undefined', items: [undefined, undefined] }, |
||
16 | { label: 'number', items: [42] } ]) |
||
17 | }) |
||
18 | |||
19 | /* ------------------------------------------------------------------------ */ |
||
20 | |||
21 | describe ('StackTracey', () => { |
||
22 | |||
23 | const StackTracey = require ('./stacktracey') |
||
24 | |||
25 | const shouldBeVisibleInStackTrace = () => new StackTracey () // @hide |
||
26 | |||
27 | it ('works', () => { |
||
28 | |||
29 | const stack = shouldBeVisibleInStackTrace () |
||
30 | |||
31 | stack.should.be.an.instanceof (Array) |
||
32 | |||
33 | stack[0].should.deep.equal ({ |
||
34 | beforeParse: 'at shouldBeVisibleInStackTrace (' + process.cwd () + '/test.js:25:47)', |
||
35 | callee: 'shouldBeVisibleInStackTrace', |
||
36 | index: false, |
||
37 | native: false, |
||
38 | file: process.cwd () + '/test.js', |
||
39 | line: 25, |
||
40 | column: 47, |
||
41 | calleeShort: 'shouldBeVisibleInStackTrace', |
||
42 | fileName: 'test.js', |
||
43 | fileShort: 'test.js', |
||
44 | thirdParty: false |
||
45 | }) |
||
46 | }) |
||
47 | |||
48 | it ('allows to read sources', () => { |
||
49 | |||
50 | const stack = shouldBeVisibleInStackTrace ().withSources // @hide |
||
51 | stack.should.be.an.instanceof (StackTracey) |
||
52 | stack[0].beforeParse.should.not.be.undefined // should preserve previous fields |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
53 | stack[0].sourceLine.should.equal (' const shouldBeVisibleInStackTrace = () => new StackTracey () ') |
||
54 | stack[0].hide.should.equal (true) // reads // @hide marker |
||
55 | stack[1].hide.should.equal (true) // reads // @hide marker |
||
56 | |||
57 | const cleanStack = stack.clean |
||
58 | |||
59 | cleanStack.should.be.an.instanceof (StackTracey) |
||
60 | |||
61 | StackTracey.locationsEqual (cleanStack[0], stack[0]).should.equal (true) // should not clean top element |
||
62 | StackTracey.locationsEqual (cleanStack[1], stack[1]).should.equal (false) // should clean second element (due to // @hide) |
||
63 | }) |
||
64 | |||
65 | it ('allows creation from array + groups duplicate lines', () => { |
||
66 | |||
67 | const stack = new StackTracey ([ |
||
68 | { file: 'yo.js', line: 11, callee: 'a.funkktion', calleeShort: 'a' }, |
||
69 | { file: 'yo.js', line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' }, |
||
70 | { file: 'yo.js', line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' }, |
||
71 | { file: 'lol.js', line: 10, callee: '', calleeShort: '' }, |
||
72 | ]) |
||
73 | |||
74 | const clean = stack.clean.map (x => Object.assign ({ |
||
75 | file: x.file, |
||
76 | line: x.line, |
||
77 | callee: x.callee, |
||
78 | calleeShort: x.calleeShort })) |
||
79 | |||
80 | clean.should.be.an.instanceof (StackTracey) |
||
81 | |||
82 | Array.from (clean).should.deep.equal ([ // .should does not recognize StackTracey as normal array... |
||
83 | |||
84 | { file: process.cwd () + '/yo.js', line: 11, callee: 'a.funkktion', calleeShort: 'a' }, |
||
85 | { file: process.cwd () + '/yo.js', line: 10, callee: 'foobar.boobar → foobar.boobar', calleeShort: 'foobar → foobar' }, |
||
86 | { file: process.cwd () + '/lol.js', line: 10, callee: '', calleeShort: '' }, |
||
87 | ]) |
||
88 | }) |
||
89 | |||
90 | it ('handles inaccessible files', () => { |
||
91 | |||
92 | const stack = shouldBeVisibleInStackTrace () |
||
93 | stack[0].file = '^___^' |
||
94 | stack.withSources[0].sourceLine.should.equal ('') |
||
95 | stack.withSources[0].error.should.be.an.instanceof (Error) |
||
96 | }) |
||
97 | |||
98 | it ('exposes some Array methods', () => { |
||
99 | |||
100 | const stack = shouldBeVisibleInStackTrace () |
||
101 | const sliced = stack.slice (1) |
||
102 | const deltaLength = (stack.length - sliced.length) |
||
103 | |||
104 | deltaLength.should.equal (1) |
||
105 | sliced.should.be.an.instanceof (StackTracey) |
||
106 | |||
107 | sliced.filter (x => true).should.be.an.instanceof (StackTracey) |
||
0 ignored issues
–
show
|
|||
108 | }) |
||
109 | |||
110 | it ('shortens path correctly', () => { |
||
111 | |||
112 | StackTracey.shortenPath ('webpack:///~/jquery/dist/jquery.js') |
||
113 | .should.equal ( '~/jquery/dist/jquery.js') |
||
114 | |||
115 | StackTracey.shortenPath ('webpack:/webpack/bootstrap') |
||
116 | .should.equal ( 'webpack/bootstrap') |
||
117 | }) |
||
118 | |||
119 | it ('works with sourcemaps', () => { |
||
120 | |||
121 | const path = require ('path'), |
||
122 | mkay = require ('./test_files/mkay.uglified') |
||
123 | |||
124 | try { |
||
125 | mkay () |
||
126 | } |
||
127 | catch (e) { |
||
128 | |||
129 | e.message.should.equal ('mkay') |
||
130 | |||
131 | const top = new StackTracey (e).withSources[0] |
||
132 | |||
133 | top.line .should.equal (4) |
||
134 | top.column .should.equal (22) |
||
135 | top.sourceLine .should.equal ('\t\t\t\t\tthrow new Error (\'mkay\') }') |
||
136 | |||
137 | top.file .should.equal (path.resolve ('./test_files/mkay.js')) |
||
138 | top.fileShort .should.equal ('test_files/mkay.js') |
||
139 | top.fileName .should.equal ('mkay.js') |
||
140 | } |
||
141 | }) |
||
142 | |||
143 | it ('pretty printing works', function prettyTest () { |
||
144 | |||
145 | const pretty = new StackTracey ().clean.pretty |
||
146 | |||
147 | pretty.split ('\n')[0].should.equal ('at prettyTest test.js:145 const pretty = new StackTracey ().clean.pretty') |
||
148 | }) |
||
149 | |||
150 | it ('exposes Array methods', () => { |
||
151 | |||
152 | const stack = new StackTracey ([ |
||
153 | { file: 'foo' }, |
||
154 | { file: 'bar' } |
||
155 | ]) |
||
156 | |||
157 | const mapped = stack.map ((x, i) => Object.assign (x, { i })) |
||
158 | |||
159 | mapped.should.deep.equal ([ { file: 'foo', i: 0 }, { file: 'bar', i: 1 } ]) |
||
160 | mapped.should.be.an.instanceof (Array) |
||
161 | mapped.should.be.an.instanceof (StackTracey) |
||
162 | |||
163 | stack.reduce ((memo, x) => memo + x.file, '').should.equal ('foobar') |
||
164 | |||
165 | const filtered = stack.filter (x => x.file === 'bar') |
||
166 | |||
167 | filtered.length.should.equal (1) |
||
168 | filtered[0].should.deep.equal ({ file: 'bar', i: 1 }) |
||
169 | }) |
||
170 | }) |
||
171 | |||
172 | |||
173 | |||
174 |