| 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
Loading history...
|
|||
| 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 ('Array.filter works', () => { |
||
| 111 | |||
| 112 | const stack = new StackTracey ([ |
||
| 113 | { file: 'yo.js', line: 10 }, |
||
| 114 | { file: 'lol.js', line: 10 }, |
||
| 115 | ]) |
||
| 116 | |||
| 117 | const filtered = stack.filter (x => x.file === 'lol.js') |
||
| 118 | |||
| 119 | filtered.length.should.equal (1) |
||
| 120 | filtered[0].should.deep.equal ({ file: 'lol.js', line: 10 }) |
||
| 121 | }) |
||
| 122 | |||
| 123 | it ('shortens path correctly', () => { |
||
| 124 | |||
| 125 | StackTracey.shortenPath ('webpack:///~/jquery/dist/jquery.js') |
||
| 126 | .should.equal ( '~/jquery/dist/jquery.js') |
||
| 127 | |||
| 128 | StackTracey.shortenPath ('webpack:/webpack/bootstrap') |
||
| 129 | .should.equal ( 'webpack/bootstrap') |
||
| 130 | }) |
||
| 131 | |||
| 132 | it ('works with sourcemaps', () => { |
||
| 133 | |||
| 134 | const path = require ('path'), |
||
| 135 | mkay = require ('./test_files/mkay.uglified') |
||
| 136 | |||
| 137 | try { |
||
| 138 | mkay () |
||
| 139 | } |
||
| 140 | catch (e) { |
||
| 141 | |||
| 142 | e.message.should.equal ('mkay') |
||
| 143 | |||
| 144 | const top = new StackTracey (e).withSources[0] |
||
| 145 | |||
| 146 | top.line .should.equal (4) |
||
| 147 | top.column .should.equal (22) |
||
| 148 | top.sourceLine .should.equal ('\t\t\t\t\tthrow new Error (\'mkay\') }') |
||
| 149 | |||
| 150 | top.file .should.equal (path.resolve ('./test_files/mkay.js')) |
||
| 151 | top.fileShort .should.equal ('test_files/mkay.js') |
||
| 152 | top.fileName .should.equal ('mkay.js') |
||
| 153 | } |
||
| 154 | }) |
||
| 155 | |||
| 156 | it ('pretty printing works', function prettyTest () { |
||
| 157 | |||
| 158 | const pretty = new StackTracey ().clean.pretty |
||
| 159 | |||
| 160 | pretty.split ('\n')[0].should.equal ('at prettyTest test.js:158 const pretty = new StackTracey ().clean.pretty') |
||
| 161 | }) |
||
| 162 | }) |
||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 |