Completed
Push — master ( d71171...8b8f67 )
by Vitaly
24s
created

test.js (2 issues)

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'); StackTracey.resetCache ()
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
            fileRelative: 'test.js',
44
            fileShort: 'test.js',
45
            thirdParty: false
46
        })
47
    })
48
49
    it ('allows to read sources', () => {
50
51
        const stack = shouldBeVisibleInStackTrace ().withSources // @hide
52
53
              stack.should.be.an.instanceof (StackTracey)
54
              stack[0].beforeParse.should.not.be.undefined // should preserve previous fields
0 ignored issues
show
The result of the property access to stack.0.beforeParse.should.not.be.undefined is not used.
Loading history...
55
              stack[0].sourceLine.should.equal ('    const shouldBeVisibleInStackTrace = () => new StackTracey () ')
56
              stack[0].hide.should.equal (true) // reads // @hide marker
57
              stack[1].hide.should.equal (true) // reads // @hide marker
58
59
        const cleanStack = stack.clean
60
61
        cleanStack.should.be.an.instanceof (StackTracey)
62
63
        StackTracey.locationsEqual (cleanStack[0], stack[0]).should.equal (true)  // should not clean top element
64
        StackTracey.locationsEqual (cleanStack[1], stack[1]).should.equal (false) // should clean second element (due to // @hide)
65
    })
66
        
67
    it ('allows creation from array + groups duplicate lines', () => {
68
69
        const stack = new StackTracey ([
70
            { file: 'yo.js',  line: 11, callee: 'a.funkktion',   calleeShort: 'a' },
71
            { file: 'yo.js',  line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' },
72
            { file: 'yo.js',  line: 10, callee: 'foobar.boobar', calleeShort: 'foobar' },
73
            { file: 'lol.js', line: 10, callee: '',              calleeShort: '' },
74
        ])
75
76
        const clean = stack.clean.map (x => Object.assign ({
77
                                                    file: x.file,
78
                                                    line: x.line,
79
                                                    callee: x.callee,
80
                                                    calleeShort: x.calleeShort }))
81
82
        clean.should.be.an.instanceof (StackTracey)
83
84
        Array.from (clean).should.deep.equal ([ // .should does not recognize StackTracey as normal array...
85
86
            { file: process.cwd () + '/yo.js',  line: 11, callee: 'a.funkktion',   calleeShort: 'a' },
87
            { file: process.cwd () + '/yo.js',  line: 10, callee: 'foobar.boobar → foobar.boobar', calleeShort: 'foobar → foobar' },
88
            { file: process.cwd () + '/lol.js', line: 10, callee: '',              calleeShort: '' },
89
        ])
90
    })
91
92
    it ('handles inaccessible files', () => {
93
94
        const stack = shouldBeVisibleInStackTrace ()
95
              stack[0].file = '^___^'
96
              stack.withSources[0].sourceLine.should.equal ('')
97
              stack.withSources[0].error.should.be.an.instanceof (Error)
98
    })
99
100
    it ('exposes some Array methods', () => {
101
102
        const stack = shouldBeVisibleInStackTrace ()
103
        const sliced = stack.slice (1)
104
        const deltaLength = (stack.length - sliced.length)
105
106
        deltaLength.should.equal (1)
107
        sliced.should.be.an.instanceof (StackTracey)
108
109
        sliced.filter (x => true).should.be.an.instanceof (StackTracey)
0 ignored issues
show
The parameter x is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
110
    })
111
112
    it ('works with sourcemaps', () => {
113
114
        const path = require ('path'),
115
              mkay = require ('./test_files/mkay.uglified')
116
117
        try {
118
            mkay ()
119
        }
120
        catch (e) {
121
122
            e.message.should.equal ('mkay')
123
124
            const top = new StackTracey (e).withSources[0]
125
126
            top.line        .should.equal (4)
127
            top.column      .should.equal (22)
128
            top.sourceLine  .should.equal ('\t\t\t\t\tthrow new Error (\'mkay\') }')
129
130
            top.file        .should.equal (path.resolve ('./test_files/mkay.js'))
131
            top.fileShort   .should.equal ('test_files/mkay.js')
132
            top.fileName    .should.equal ('mkay.js')
133
        }
134
    })
135
136
    it ('pretty printing works', function prettyTest () {
137
138
        const pretty = new StackTracey ().clean.pretty
139
140
        pretty.split ('\n')[0].should.equal ('at prettyTest                      test.js:138    const pretty = new StackTracey ().clean.pretty')
141
142
        ;(new StackTracey ([
143
            { },
144
            { }
145
        ]).clean.pretty).trim ().should.equal ('at <anonymous> → <anonymous>')
146
    })
147
148
    it ('trims too long columns in the pretty printed output', () => {
149
150
        const stack = new StackTracey ([
151
            { file: '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
                stack[0].syntaxError.should.equal (true)
207
                stack[0].column.should.equal (5)
208
                stack.pretty.split ('\n')[0].should.equal ('at (syntax error)                  test_files/syntax_error.js:2  foo->bar ()                                     ')
209
            }
210
        })
211
    }
212
213
    it ('implements StackTracey.isThirdParty', () => {
214
215
        StackTracey.isThirdParty.include (path => path === 'test.js')
216
217
        new StackTracey ()[0].thirdParty.should.equal (true)
218
219
        StackTracey.isThirdParty.except (path => path === 'test.js')
220
        
221
        new StackTracey ()[0].thirdParty.should.equal (false)
222
    })
223
224
    it ('.withSource', () => {
225
226
        const line = new StackTracey ().withSource (0).sourceLine.trim ()
227
        line.should.equal ('const line = new StackTracey ().withSource (0).sourceLine.trim ()')
228
    })
229
230
    it ('.at', () => {
231
        
232
        new StackTracey ().at (0).file.includes ('stacktracey/test.js').should.equal (true)
233
    })
234
235
    it ('detects Array methods as native', () => {
236
237
        const arr = [1,2,3]
238
        const stack = arr.reduce (() => new StackTracey ())
239
240
        stack[1].native.should.equal (true)
241
    })
242
243
    it ('works on Windows', () => {
244
245
        const dir = process.cwd ()
246
247
        const windowsStack =
248
                [
249
                'Error',
250
                '    at Context.it (' + dir + '\\test.js:31:22)',
251
                '    at callFn (' + dir + '\\node_modules\\mocha\\lib\\runnable.js:354:21)',
252
                '    at runCallback (timers.js:800:20)'
253
                ].join ('\n')
254
255
        const stack = new StackTracey (windowsStack)
256
        const lines = stack.pretty.split ('\n')
257
258
        lines[0].should.equal ('at it           test.js:31                 stack.should.be.an.instanceof (Array)')
259
        lines[1].indexOf      ('at callFn       mocha/lib/runnable.js:354').should.equal (0)
260
    })
261
262
})
263
264
265
266