This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
adapter.spec.js
557 lines (448 loc) · 19.4 KB
/
adapter.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
Tests for adapter/mocha.src.js
These tests are executed in browser.
*/
/* globals
createMochaReporterConstructor,
createMochaReporterNode,
createMochaStartFn,
createConfigObject,
mochaConfig: true,
formatError: true,
processAssertionError: true,
MockSocket: true,
Emitter: true,
sinon: true,
expect: true,
describe: true,
beforeEach: true,
afterEach: true,
it: true
*/
describe('adapter mocha', function () {
var Karma = window.__karma__.constructor
var sandbox
beforeEach(function () {
sandbox = sinon.sandbox.create()
})
afterEach(function () {
sandbox.restore()
})
describe('createMochaReporterConstructor', function () {
beforeEach(function () {
this.karma = new Karma(function (method, args) { })
this.karma.config = {
mocha: {
reporter: 'html'
}
}
sandbox.stub(window, 'createMochaReporterNode')
})
it('should take reporter from client config on debug page', function () {
expect(createMochaReporterConstructor(this.karma, '/debug.html')).to.equal('html')
})
it('should create node for mocha reporter', function () {
createMochaReporterConstructor(this.karma, '/debug.html')
expect(createMochaReporterNode.called).to.equal(true)
})
it('should define console reporter if does not pass reporter in config', function () {
this.karma.config.mocha.reporter = null
expect(createMochaReporterConstructor(this.karma, '/debug.html')).not.to.equal(null)
})
})
describe('reporter', function () {
var runner, tc
beforeEach(function () {
tc = new Karma(function (method, args) { })
runner = new Emitter()
var reporter = new (createMochaReporterConstructor(tc))(runner) // eslint-disable-line
})
describe('start', function () {
it('should report total number of specs', function () {
runner.total = 12
sandbox.spy(tc, 'info')
runner.emit('start')
expect(tc.info.getCall(0).args).to.deep.eq([{total: 12}])
})
})
describe('end', function () {
it('should report complete', function () {
sandbox.spy(tc, 'complete')
runner.emit('end')
expect(tc.complete.called).to.eq(true)
})
})
describe('test end', function () {
it('should report result', function () {
var beforeStartTime = Date.now()
var DURATION = 200
sandbox.stub(tc, 'result', function (result) {
var afterEndTime = Date.now()
expect(result.id).to.not.be.undefined
expect(result.description).to.eq('should do something')
expect(result.suite instanceof Array).to.eq(true)
expect(result.success).to.eq(true)
expect(result.skipped).to.to.eql(false)
expect(result.pending).to.to.eql(false)
expect(result.log instanceof Array).to.eq(true)
expect(result.assertionErrors instanceof Array).to.eq(true)
expect(result.startTime).to.be.at.least(beforeStartTime)
expect(result.endTime - result.startTime).to.be.at.least(DURATION)
expect(result.endTime).to.be.at.most(afterEndTime)
expect(result.time).to.eq(DURATION)
})
var mockMochaResult = {
duration: DURATION,
parent: {title: 'desc2', parent: {title: 'desc1', root: true}, root: false},
state: 'passed',
title: 'should do something'
}
runner.emit('test', mockMochaResult)
// wait at least 200ms to get different start and end times
var afterStartTime = Date.now()
while (Date.now() - afterStartTime < DURATION) {}
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should report skipped result', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.skipped).to.eq(true)
expect(result.pending).to.eq(true)
})
var mockMochaResult = {
parent: {root: true}
}
runner.emit('pending', mockMochaResult)
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should report time 0 for skipped tests', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.skipped).to.eq(true)
expect(result.time).to.eq(0)
})
var mockMochaResult = {
pending: true,
parent: {root: true}
}
runner.emit('test', mockMochaResult)
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should report failed result', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.success).to.to.eql(false)
expect(result.skipped).to.to.eql(false)
expect(result.log).to.deep.eq(['Big trouble.', 'Another fail.'])
expect(result.assertionErrors).to.be.empty
})
var mockMochaResult = {
parent: {title: 'desc2', root: true},
state: 'failed',
title: 'should do something'
}
runner.emit('test', mockMochaResult)
runner.emit('fail', mockMochaResult, {message: 'Big trouble.'})
runner.emit('pass', mockMochaResult)
runner.emit('fail', mockMochaResult, {message: 'Another fail.'})
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should report failed mocha result', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.log).to.deep.eq(['Big trouble.', 'Another fail.'])
expect(result.assertionErrors).to.deep.eq([{
name: 'AssertionError',
message: 'Big trouble.',
showDiff: false
}])
})
var mockMochaResult = {
parent: {title: 'desc2', root: true},
state: 'failed',
title: 'should do something'
}
runner.emit('test', mockMochaResult)
runner.emit('fail', mockMochaResult, {
name: 'AssertionError',
message: 'Big trouble.',
showDiff: false,
actual: 1,
expected: 2
})
runner.emit('fail', mockMochaResult, {message: 'Another fail.'})
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should report suites', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.suite).to.deep.eq(['desc1', 'desc2'])
})
var mockMochaResult = {
parent: {title: 'desc2', parent: {title: 'desc1', parent: {root: true}, root: false}, root: false},
title: 'should do something'
}
runner.emit('test', mockMochaResult)
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should report mocha properties through the `expose` option', function () {
tc.config = {
mocha: {
expose: ['body', 'hello']
}
}
sandbox.stub(tc, 'result', function (result) {
expect(result.mocha.body).to.eq('function(){ expect(false).to.be(true) }')
expect(result.mocha.hello).to.eq('world')
})
var mockMochaResult = {
parent: {title: 'desc2', root: true},
title: 'should do something',
body: 'function(){ expect(false).to.be(true) }',
hello: 'world'
}
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should not report mocha properties if `expose` is not configured', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.mocha).to.not.exist
})
var mockMochaResult = {
parent: {title: 'desc2', root: true},
title: 'should do something',
body: 'function(){ expect(false).to.be(true) }',
hello: 'world'
}
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
})
describe('fail', function () {
it('should end test on hook failure', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.success).to.to.eql(false)
expect(result.skipped).to.to.eql(false)
expect(result.log).to.deep.eq(['hook failed'])
})
var mockMochaHook = {
type: 'hook',
title: 'scenario "before each" hook',
parent: {title: 'desc1', root: true}
}
runner.emit('hook', mockMochaHook)
runner.emit('fail', mockMochaHook, {message: 'hook failed'})
expect(tc.result.called).to.eq(true)
})
it('should end the test only once on uncaught exceptions', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.success).to.to.eql(false)
expect(result.skipped).to.to.eql(false)
expect(result.log).to.deep.eq(['Uncaught error.'])
})
var mockMochaResult = {
parent: {title: 'desc2', root: true},
state: 'failed',
title: 'should do something'
}
runner.emit('test', mockMochaResult)
runner.emit('fail', mockMochaResult, {message: 'Uncaught error.', uncaught: true})
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should remove mocha stack entries', function () {
sandbox.stub(tc, 'result', function (result) {
var log = result.log[0]
expect(log).to.not.contain('/mocha/mocha.js')
expect(log).to.contain('/spec/controllers/list/formCtrlSpec.js')
})
var mockMochaResult = {
parent: {root: true}
}
var stack =
'at $httpBackend (http://localhost:8080/base/app/bower_components/angular-mocks/angular-mocks.js?506e0a37bcd764ec63da3fd7005bf56592b3df32:1149)\n' +
'at sendReq (http://localhost:8080/base/app/bower_components/angular/angular.js?7deca05396a4331b08f812e4962ef9df1d9de0b5:8408)\n' +
'at http://localhost:8080/base/app/bower_components/angular/angular.js?7deca05396a4331b08f812e4962ef9df1d9de0b5:8125\n' +
'at http://localhost:8080/base/test/client/spec/controllers/list/formCtrlSpec.js?67eaca0f801cf45a86802a262618a6cfdc6a47be:110\n' +
'at invoke (http://localhost:8080/base/app/bower_components/angular/angular.js?7deca05396a4331b08f812e4962ef9df1d9de0b5:4068)\n' +
'at workFn (http://localhost:8080/base/app/bower_components/angular-mocks/angular-mocks.js?506e0a37bcd764ec63da3fd7005bf56592b3df32:2194)\n' +
'at callFn (http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4338)\n' +
'at http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4331\n' +
'at next (http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4653)\n' +
'at http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4663\n' +
'at next (http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4601)\n'
runner.emit('test', mockMochaResult)
runner.emit('fail', mockMochaResult, {message: 'Another fail.', stack: stack})
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
it('should not remove escaped strings containing mocha stack entries', function () {
sandbox.stub(tc, 'result', function (result) {
var log = result.log[0]
expect(log).to.contain('something important that contains an escaped mocha stack trace')
})
var mockMochaResult = {
parent: {root: true}
}
var stack =
'something important that contains an escaped mocha stack trace at workFn (http://localhost:8080/base/app/bower_components/angular-mocks/angular-mocks.js?506e0a37bcd764ec63da3fd7005bf56592b3df32:2194)\\n at callFn (http://localhost:8080/base/node_modules/mocha/mocha.js?312499f61e38c4f82b2789b388ced378202a1e75:4471:21)\\n at Hook.Runnable.run (http://localhost:8080/base/node_modules/mocha/mocha.js?312499f61e38c4f82b2789b388ced378202a1e75:4464:7)\\n\n' +
'at $httpBackend (http://localhost:8080/base/app/bower_components/angular-mocks/angular-mocks.js?506e0a37bcd764ec63da3fd7005bf56592b3df32:1149)\n' +
'at sendReq (http://localhost:8080/base/app/bower_components/angular/angular.js?7deca05396a4331b08f812e4962ef9df1d9de0b5:8408)\n' +
'at http://localhost:8080/base/app/bower_components/angular/angular.js?7deca05396a4331b08f812e4962ef9df1d9de0b5:8125\n' +
'at http://localhost:8080/base/test/client/spec/controllers/list/formCtrlSpec.js?67eaca0f801cf45a86802a262618a6cfdc6a47be:110\n' +
'at invoke (http://localhost:8080/base/app/bower_components/angular/angular.js?7deca05396a4331b08f812e4962ef9df1d9de0b5:4068)\n' +
'at workFn (http://localhost:8080/base/app/bower_components/angular-mocks/angular-mocks.js?506e0a37bcd764ec63da3fd7005bf56592b3df32:2194)\n' +
'at callFn (http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4338)\n' +
'at http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4331\n' +
'at next (http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4653)\n' +
'at http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4663\n' +
'at next (http://localhost:8080/base/node_modules/mocha/mocha.js?529c1ea3966a13c21efca5afe9a2317dafcd8abc:4601)\n'
runner.emit('test', mockMochaResult)
runner.emit('fail', mockMochaResult, {message: 'Another fail.', stack: stack})
runner.emit('test end', mockMochaResult)
expect(tc.result.called).to.eq(true)
})
})
})
describe('createMochaStartFn', function () {
beforeEach(function () {
this.mockMocha = {
grep: function () {
},
run: function () {
}
}
})
it('should pass grep argument to mocha', function () {
sandbox.spy(this.mockMocha, 'grep')
createMochaStartFn(this.mockMocha)({
args: ['--grep', 'test test']
})
expect(this.mockMocha.grep.getCall(0).args).to.deep.eq([/test test/])
})
it('should pass grep argument to mocha if we called the run with --grep=xxx', function () {
sandbox.spy(this.mockMocha, 'grep')
createMochaStartFn(this.mockMocha)({
args: ['--grep=test test']
})
expect(this.mockMocha.grep.getCall(0).args).to.deep.eq([/test test/])
})
it('should pass grep argument to mocha if config.args contains property grep', function () {
sandbox.spy(this.mockMocha, 'grep')
createMochaStartFn(this.mockMocha)({
args: {
grep: 'test'
}
})
expect(this.mockMocha.grep.getCall(0).args).to.deep.eq(['test'])
})
it('should not require client arguments', function () {
var that = this
expect(function () {
createMochaStartFn(that.mockMocha)({})
}).to.not.throw()
})
})
describe('createConfigObject', function () {
beforeEach(function () {
this.originalMochaConfig = mochaConfig
mochaConfig = {
ui: 'bdd',
globals: ['__cov']
}
this.karma = new Karma(new MockSocket(), null, null, null, {search: ''})
this.karma.config = {}
})
afterEach(function () {
mochaConfig = this.originalMochaConfig
})
it('should return default config if karma does not define client config', function () {
this.karma.config = null
expect(createConfigObject(this.karma)).to.eq(mochaConfig)
})
it('should return default config if the client config havent properties mocha', function () {
expect(createConfigObject(this.karma)).to.eq(mochaConfig)
})
it('should pass client.mocha options to mocha config', function () {
this.karma.config.mocha = {
slow: 10
}
expect(createConfigObject(this.karma).slow).to.eq(10)
})
it('should rewrite ui options from default config', function () {
this.karma.config.mocha = {
ui: 'tdd'
}
expect(createConfigObject(this.karma).ui).to.eq('tdd')
})
it('should ignore propertie reporter from client config', function () {
this.karma.config.mocha = {
reporter: 'test'
}
expect(createConfigObject(this.karma).reporter).not.to.eq('test')
})
it('should ignore property require from client config', function () {
this.karma.config.mocha = {
require: 'test'
}
expect(createConfigObject(this.karma).require).not.to.eq('test')
})
it('should ignore property expose from client config', function () {
this.karma.config.mocha = {
expose: 'body'
}
expect(createConfigObject(this.karma).expose).not.to.eq('body')
})
it('should merge the globals from client config if they exist', function () {
this.karma.config.mocha = {
globals: ['test']
}
expect(createConfigObject(this.karma).globals).to.deep.eq(['__cov', 'test'])
})
})
describe('formatError', function () {
it('should properly format exceptions that contains \n in their message', function () {
var errLines = formatError(new Error('foo\nbar')).split('\n')
expect(errLines[0]).to.contain('foo')
expect(errLines[1]).to.equal('bar')
expect(errLines[2]).to.not.contain('foo')
})
})
describe('processAssertionError', function () {
it('should create object from mocha error', function () {
var err = new Error()
err.name = 'AssertionError'
err.message = 'expected \'something\' to deeply equal \'something else\''
err.showDiff = true
err.actual = {baz: 'baz', foo: null, bar: function () {}}
err.expected = {baz: 42, foo: undefined}
var error = processAssertionError(err)
expect(Object.keys(error)).to.be.eql(['name', 'message', 'showDiff', 'actual', 'expected'])
expect(error.name).to.equal('AssertionError')
expect(error.message).to.equal('expected \'something\' to deeply equal \'something else\'')
expect(error.showDiff).to.be.true
expect(error.actual).to.equal('{\n "bar": [Function]\n "baz": "baz"\n "foo": [null]\n}')
expect(error.expected).to.equal('{\n "baz": 42\n "foo": [undefined]\n}')
})
it('should not create object from simple error', function () {
var err = new Error('Something wrong')
var error = processAssertionError(err)
expect(error).to.be.undefined
})
it('should not pass actual and expected if showDiff is off', function () {
var err = new Error()
err.message = 'expected \'something\' to deeply equal \'something else\''
err.showDiff = false
err.actual = {baz: 'baz', foo: null, bar: function () {}}
err.expected = {baz: 42, foo: undefined}
var error = processAssertionError(err)
expect(Object.keys(error)).to.be.eql(['name', 'message', 'showDiff'])
expect(error.name).to.equal('Error')
expect(error.message).to.equal('expected \'something\' to deeply equal \'something else\'')
expect(error.showDiff).to.be.false
expect(error).to.not.have.property('actual')
expect(error).to.not.have.property('expected')
})
})
})