-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
reporter.spec.js
245 lines (198 loc) · 8.79 KB
/
reporter.spec.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
import {EventEmitter} from 'events'
import File from '../../lib/file'
import {loadFile} from 'mocks'
import path from 'path'
var _ = require('../../lib/helper')._
describe('reporter', () => {
var m
beforeEach(() => {
m = loadFile(path.join(__dirname, '/../../lib/reporter.js'))
})
describe('formatError', () => {
var emitter
var formatError = emitter = null
beforeEach(() => {
emitter = new EventEmitter()
formatError = m.createErrorFormatter('', emitter)
})
it('should indent', () => {
expect(formatError('Something', '\t')).to.equal('\tSomething\n')
})
it('should handle empty message', () => {
expect(formatError(null)).to.equal('\n')
})
it('should handle arbitrary error objects', () => {
expect(
formatError({hello: 'world'})
).to.equal(
JSON.stringify({hello: 'world'}) + '\n'
)
})
it('should handle error objects', () => {
expect(
formatError(new Error('fail'))
).to.equal(
'fail\n'
)
})
it('should remove domain from files', () => {
expect(formatError('file http://localhost:8080/base/usr/a.js and http://127.0.0.1:8080/absolute/home/b.js')).to.be.equal('file usr/a.js and /home/b.js\n')
})
// TODO(vojta): enable once we serve source under urlRoot
it.skip('should handle non default karma service folders', () => {
formatError = m.createErrorFormatter('', '/_karma_/')
expect(formatError('file http://localhost:8080/_karma_/base/usr/a.js and http://127.0.0.1:8080/_karma_/base/home/b.js')).to.be.equal('file usr/a.js and home/b.js\n')
})
it('should remove shas', () => {
var ERROR = 'file http://localhost:8080/base/usr/file.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9 and http://127.0.0.1:8080/absolute/home/file.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9'
expect(formatError(ERROR)).to.be.equal('file usr/file.js and /home/file.js\n')
})
it('should indent all lines', () => {
expect(formatError('first\nsecond\nthird', '\t')).to.equal('\tfirst\n\tsecond\n\tthird\n')
})
it('should restore base paths', () => {
formatError = m.createErrorFormatter('/some/base', emitter)
expect(formatError('at http://localhost:123/base/a.js?123')).to.equal('at a.js\n')
})
it('should restore absolute paths', () => {
var ERROR = 'at http://local:1233/absolute/usr/path.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9'
expect(formatError(ERROR)).to.equal('at /usr/path.js\n')
})
it('should preserve line numbers', () => {
var ERROR = 'at http://local:1233/absolute/usr/path.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9:2'
expect(formatError(ERROR)).to.equal('at /usr/path.js:2\n')
})
it('should preserve absolute word', () => {
var ERROR = 'contains absolute'
expect(formatError(ERROR)).to.equal('contains absolute\n')
})
it('should preserve base word', () => {
var ERROR = 'contains base'
expect(formatError(ERROR)).to.equal('contains base\n')
})
describe('source maps', () => {
var originalPositionForCallCount = 0
var sourceMappingPath = null
class MockSourceMapConsumer {
constructor (sourceMap) {
this.source = sourceMap.content.replace('SOURCE MAP ', sourceMappingPath)
}
originalPositionFor (position) {
originalPositionForCallCount++
if (position.line === 0) {
throw new TypeError('Line must be greater than or equal to 1, got 0')
}
return {
source: this.source,
line: position.line + 2,
column: position.column + 2
}
}
}
beforeEach(() => {
originalPositionForCallCount = 0
sourceMappingPath = '/original/'
})
MockSourceMapConsumer.GREATEST_LOWER_BOUND = 1
MockSourceMapConsumer.LEAST_UPPER_BOUND = 2
it('should rewrite stack traces', (done) => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'}
servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'}
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at http://localhost:123/base/b.js:2:6'
expect(formatError(ERROR)).to.equal('at /original/b.js:4:8 <- b.js:2:6\n')
done()
})
})
it('should rewrite stack traces to the first column when no column is given', (done) => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'}
servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'}
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at http://localhost:123/base/b.js:2'
expect(formatError(ERROR)).to.equal('at /original/b.js:4:2 <- b.js:2\n')
done()
})
})
it('should rewrite relative url stack traces', (done) => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'}
servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'}
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at /base/b.js:2:6'
expect(formatError(ERROR)).to.equal('at /original/b.js:4:8 <- b.js:2:6\n')
done()
})
})
it('should resolve relative urls from source maps', (done) => {
sourceMappingPath = 'original/' // Note: relative path.
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/path/a.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.fancyjs'}
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at /base/path/a.js:2:6'
expect(formatError(ERROR)).to.equal('at path/original/a.fancyjs:4:8 <- path/a.js:2:6\n')
done()
})
})
it('should fall back to non-source-map format if originalPositionFor throws', (done) => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'}
servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'}
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at http://localhost:123/base/b.js:0:0'
expect(formatError(ERROR)).to.equal('at b.js\n')
done()
})
})
it('should not try to use source maps when no line is given', (done) => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
var servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP a.js'}
servedFiles[1].sourceMap = {content: 'SOURCE MAP b.js'}
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at http://localhost:123/base/b.js'
expect(formatError(ERROR)).to.equal('at b.js\n')
expect(originalPositionForCallCount).to.equal(0)
done()
})
})
describe('Windows', () => {
formatError = null
var servedFiles = null
beforeEach(() => {
formatError = m.createErrorFormatter('/some/base', emitter, MockSourceMapConsumer)
servedFiles = [new File('C:/a/b/c.js')]
servedFiles[0].sourceMap = {content: 'SOURCE MAP b.js'}
})
it('should correct rewrite stack traces without sha', (done) => {
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at http://localhost:123/absoluteC:/a/b/c.js:2:6'
expect(formatError(ERROR)).to.equal('at c:/original/b.js:4:8 <- C:/a/b/c.js:2:6\n')
done()
})
})
it('should correct rewrite stack traces with sha', (done) => {
emitter.emit('file_list_modified', {served: servedFiles})
_.defer(() => {
var ERROR = 'at http://localhost:123/absoluteC:/a/b/c.js?da39a3ee5e6:2:6'
expect(formatError(ERROR)).to.equal('at c:/original/b.js:4:8 <- C:/a/b/c.js:2:6\n')
done()
})
})
})
})
})
})