| 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 path = require ('path') |
||
| 24 | const StackTracey = require ('./stacktracey') |
||
| 25 | |||
| 26 | StackTracey.resetCache () |
||
| 27 | |||
| 28 | const shouldBeVisibleInStackTrace = () => new StackTracey () // @hide |
||
| 29 | |||
| 30 | it ('works', () => { |
||
| 31 | |||
| 32 | const stack = shouldBeVisibleInStackTrace () |
||
| 33 | |||
| 34 | stack.should.be.an.instanceof (Array) |
||
| 35 | |||
| 36 | stack[0].should.deep.equal ({ |
||
| 37 | beforeParse: 'at shouldBeVisibleInStackTrace (' + path.join (process.cwd (), 'test.js') + ':28:47)', |
||
| 38 | callee: 'shouldBeVisibleInStackTrace', |
||
| 39 | index: false, |
||
| 40 | native: false, |
||
| 41 | file: path.join (process.cwd (), 'test.js').replace (/\\/g, '/'), |
||
| 42 | line: 28, |
||
| 43 | column: 47, |
||
| 44 | calleeShort: 'shouldBeVisibleInStackTrace', |
||
| 45 | fileName: 'test.js', |
||
| 46 | fileRelative: 'test.js', |
||
| 47 | fileShort: 'test.js', |
||
| 48 | thirdParty: false |
||
| 49 | }) |
||
| 50 | }) |
||
| 51 | |||
| 52 | it ('allows to read sources', () => { |
||
| 53 | |||
| 54 | const stack = shouldBeVisibleInStackTrace ().withSources // @hide |
||
| 55 | |||
| 56 | stack.should.be.an.instanceof (StackTracey) |
||
| 57 | stack[0].beforeParse.should.not.be.undefined // should preserve previous fields |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 58 | stack[0].sourceLine.should.equal (' const shouldBeVisibleInStackTrace = () => new StackTracey () ') |
||
| 59 | stack[0].hide.should.equal (true) // reads // @hide marker |
||
| 60 | stack[1].hide.should.equal (true) // reads // @hide marker |
||
| 61 | |||
| 62 | const cleanStack = stack.clean |
||
| 63 | |||
| 64 | cleanStack.should.be.an.instanceof (StackTracey) |
||
| 65 | |||
| 66 | StackTracey.locationsEqual (cleanStack[0], stack[0]).should.equal (true) // should not clean top element |
||
| 67 | StackTracey.locationsEqual (cleanStack[1], stack[1]).should.equal (false) // should clean second element (due to // @hide) |
||
| 68 | }) |
||
| 69 | |||
| 70 | it ('allows creation from array + groups duplicate lines', () => { |
||
| 71 | |||
| 72 | const stack = new StackTracey ([ |
||
| 73 | { file: 'yo.js', line: 11, callee: 'a.funkktion', calleeShort: 'a' }, |
||
| 74 | { file: 'yo.js', line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' }, |
||
| 75 | { file: 'yo.js', line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' }, |
||
| 76 | { file: 'lol.js', line: 10, callee: '', calleeShort: '' }, |
||
| 77 | ]) |
||
| 78 | |||
| 79 | const clean = stack.clean.map (x => Object.assign ({ |
||
| 80 | file: x.file, |
||
| 81 | line: x.line, |
||
| 82 | callee: x.callee, |
||
| 83 | calleeShort: x.calleeShort })) |
||
| 84 | |||
| 85 | clean.should.be.an.instanceof (StackTracey) |
||
| 86 | |||
| 87 | Array.from (clean).should.deep.equal ([ // .should does not recognize StackTracey as normal array... |
||
| 88 | |||
| 89 | { file: 'yo.js', line: 11, callee: 'a.funkktion', calleeShort: 'a' }, |
||
| 90 | { file: 'yo.js', line: 10, callee: 'foobar.boobar → foobar.boobar', calleeShort: 'foobar → foobar' }, |
||
| 91 | { file: 'lol.js', line: 10, callee: '', calleeShort: '' }, |
||
| 92 | ]) |
||
| 93 | }) |
||
| 94 | |||
| 95 | it ('handles inaccessible files', () => { |
||
| 96 | |||
| 97 | const stack = shouldBeVisibleInStackTrace () |
||
| 98 | stack[0].file = '^___^' |
||
| 99 | stack.withSources[0].sourceLine.should.equal ('') |
||
| 100 | stack.withSources[0].error.should.be.an.instanceof (Error) |
||
| 101 | }) |
||
| 102 | |||
| 103 | it ('exposes some Array methods', () => { |
||
| 104 | |||
| 105 | const stack = shouldBeVisibleInStackTrace () |
||
| 106 | const sliced = stack.slice (1) |
||
| 107 | const deltaLength = (stack.length - sliced.length) |
||
| 108 | |||
| 109 | deltaLength.should.equal (1) |
||
| 110 | sliced.should.be.an.instanceof (StackTracey) |
||
| 111 | |||
| 112 | sliced.filter (x => true).should.be.an.instanceof (StackTracey) |
||
|
0 ignored issues
–
show
|
|||
| 113 | }) |
||
| 114 | |||
| 115 | it ('works with sourcemaps', () => { |
||
| 116 | |||
| 117 | const mkay = require ('./test_files/mkay.uglified') |
||
| 118 | |||
| 119 | try { |
||
| 120 | mkay () |
||
| 121 | } |
||
| 122 | catch (e) { |
||
| 123 | |||
| 124 | e.message.should.equal ('mkay') |
||
| 125 | |||
| 126 | const top = new StackTracey (e).withSources[0] |
||
| 127 | |||
| 128 | top.line .should.equal (4) |
||
| 129 | top.column .should.equal (22) |
||
| 130 | top.sourceLine .should.equal ('\t\t\t\t\tthrow new Error (\'mkay\') }') |
||
| 131 | |||
| 132 | top.file .should.equal (path.resolve ('./test_files/mkay.js').replace (/\\/g, '/')) |
||
| 133 | top.fileShort .should.equal ('test_files/mkay.js') |
||
| 134 | top.fileName .should.equal ('mkay.js') |
||
| 135 | } |
||
| 136 | }) |
||
| 137 | |||
| 138 | it ('pretty printing works', function prettyTest () { |
||
| 139 | |||
| 140 | const pretty = new StackTracey ().clean.pretty |
||
| 141 | |||
| 142 | console.log ('') |
||
|
0 ignored issues
–
show
|
|||
| 143 | console.log (pretty, '\n') |
||
| 144 | |||
| 145 | pretty.split ('\n')[0].should.equal ('at prettyTest test.js:140 const pretty = new StackTracey ().clean.pretty') |
||
| 146 | }) |
||
| 147 | |||
| 148 | it ('trims too long columns in the pretty printed output', () => { |
||
| 149 | |||
| 150 | const stack = new StackTracey ([ |
||
| 151 | { fileShort: 'dasdasdasdadadadasdasdasdadasdassdasdaddadasdas.js', line: 11, calleeShort: 'dadasdasdasdasdasdasdasdasdasdasdasdasd' }, |
||
| 152 | ]) |
||
| 153 | |||
| 154 | stack.pretty.split ('\n')[0].should.equal ('at dadasdasdasdasdasdasdasdasdas… …adasdasdasdadasdassdasdaddadasdas.js:11 ') |
||
| 155 | }) |
||
| 156 | |||
| 157 | it ('exposes Array methods', () => { |
||
| 158 | |||
| 159 | const stack = new StackTracey ([ |
||
| 160 | { file: 'foo' }, |
||
| 161 | { file: 'bar' } |
||
| 162 | ]) |
||
| 163 | |||
| 164 | const mapped = stack.map ((x, i) => Object.assign (x, { i })) |
||
| 165 | |||
| 166 | mapped.should.deep.equal ([ { file: 'foo', i: 0 }, { file: 'bar', i: 1 } ]) |
||
| 167 | mapped.should.be.an.instanceof (Array) |
||
| 168 | mapped.should.be.an.instanceof (StackTracey) |
||
| 169 | |||
| 170 | stack.reduce ((memo, x) => memo + x.file, '').should.equal ('foobar') |
||
| 171 | |||
| 172 | const filtered = stack.filter (x => x.file === 'bar') |
||
| 173 | |||
| 174 | filtered.length.should.equal (1) |
||
| 175 | filtered[0].should.deep.equal ({ file: 'bar', i: 1 }) |
||
| 176 | }) |
||
| 177 | |||
| 178 | it ('computes relative path correctly', () => { |
||
| 179 | |||
| 180 | StackTracey.relativePath ('webpack:///~/jquery/dist/jquery.js') |
||
| 181 | .should.equal ( '~/jquery/dist/jquery.js') |
||
| 182 | |||
| 183 | StackTracey.relativePath ('webpack:/webpack/bootstrap') |
||
| 184 | .should.equal ( 'webpack/bootstrap') |
||
| 185 | }) |
||
| 186 | |||
| 187 | it ('computes short path correctly', () => { |
||
| 188 | |||
| 189 | StackTracey.shortenPath ('webpack/bootstrap/jquery/dist/jquery.js') |
||
| 190 | .should.equal ('jquery/dist/jquery.js') |
||
| 191 | |||
| 192 | StackTracey.shortenPath ('node_modules/jquery/dist/jquery.js') |
||
| 193 | .should.equal ('jquery/dist/jquery.js') |
||
| 194 | }) |
||
| 195 | |||
| 196 | const nodeVersion = Number (process.version.match(/^v(\d+\.\d+)/)[1]) |
||
| 197 | if (nodeVersion >= 5) { |
||
| 198 | |||
| 199 | it ('recognizes SyntaxErrors', () => { |
||
| 200 | |||
| 201 | try { require ('./test_files/syntax_error.js') } |
||
| 202 | catch (e) { |
||
| 203 | |||
| 204 | const stack = new StackTracey (e).clean |
||
| 205 | |||
| 206 | console.log ('') |
||
|
0 ignored issues
–
show
|
|||
| 207 | console.log (stack.pretty, '\n') |
||
| 208 | |||
| 209 | stack[0].syntaxError.should.equal (true) |
||
| 210 | stack[0].column.should.equal (5) |
||
| 211 | |||
| 212 | stack.pretty.split ('\n')[0].should.equal ('at (syntax error) test_files/syntax_error.js:2 foo->bar () ') |
||
| 213 | } |
||
| 214 | }) |
||
| 215 | } |
||
| 216 | |||
| 217 | it ('implements StackTracey.isThirdParty', () => { |
||
| 218 | |||
| 219 | StackTracey.isThirdParty.include (path => path === 'test.js') |
||
| 220 | |||
| 221 | new StackTracey ()[0].thirdParty.should.equal (true) |
||
| 222 | |||
| 223 | StackTracey.isThirdParty.except (path => path === 'test.js') |
||
| 224 | |||
| 225 | new StackTracey ()[0].thirdParty.should.equal (false) |
||
| 226 | }) |
||
| 227 | |||
| 228 | it ('.withSource', () => { |
||
| 229 | |||
| 230 | const line = new StackTracey ().withSource (0).sourceLine.trim () |
||
| 231 | line.should.equal ('const line = new StackTracey ().withSource (0).sourceLine.trim ()') |
||
| 232 | }) |
||
| 233 | |||
| 234 | it ('.at', () => { |
||
| 235 | |||
| 236 | new StackTracey ().at (0).file.includes ('stacktracey/test.js').should.equal (true) |
||
| 237 | }) |
||
| 238 | |||
| 239 | it ('detects Array methods as native', () => { |
||
| 240 | |||
| 241 | const arr = [1,2,3] |
||
| 242 | const stack = arr.reduce (() => new StackTracey ()) |
||
| 243 | |||
| 244 | stack[1].native.should.equal (true) |
||
| 245 | }) |
||
| 246 | |||
| 247 | it ('works on Windows', () => { |
||
| 248 | |||
| 249 | const dir = process.cwd () |
||
| 250 | |||
| 251 | const windowsStack = |
||
| 252 | [ |
||
| 253 | 'Error', |
||
| 254 | ' at Context.it (' + dir + '\\test.js:34:22)', |
||
| 255 | ' at callFn (' + dir + '\\node_modules\\mocha\\lib\\runnable.js:354:21)', |
||
| 256 | ' at runCallback (timers.js:800:20)' |
||
| 257 | ].join ('\n') |
||
| 258 | |||
| 259 | const stack = new StackTracey (windowsStack) |
||
| 260 | const pretty = stack.pretty |
||
| 261 | const lines = pretty.split ('\n') |
||
| 262 | |||
| 263 | console.log ('') |
||
|
0 ignored issues
–
show
|
|||
| 264 | console.log (pretty, '\n') |
||
| 265 | |||
| 266 | lines[0].should.equal ('at it test.js:34 stack.should.be.an.instanceof (Array)') |
||
| 267 | lines[1].indexOf ('at callFn mocha/lib/runnable.js:354').should.equal (0) |
||
| 268 | }) |
||
| 269 | |||
| 270 | }) |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 |