@gjsify/unit 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,641 @@
1
+ import { describe, it, expect, assert, spy } from '@gjsify/unit';
2
+
3
+ // https://github.com/mysticatea/spy/blob/master/test/index.ts
4
+ export default async () => {
5
+
6
+ await describe("'spy' function", async () => {
7
+ await it("should return a function.", async () => {
8
+ const f = spy()
9
+ f()
10
+ assert.strictEqual(typeof f, "function")
11
+ })
12
+
13
+ await it("should return a function which calls the given function.", async () => {
14
+ let called = false
15
+ const f = spy(() => {
16
+ called = true
17
+ })
18
+ f()
19
+ assert.strictEqual(called, true)
20
+ })
21
+
22
+ await it("should return a function which calls the given method.", async () => {
23
+ const box = {
24
+ value: 0,
25
+ set(value: number): void {
26
+ this.value = value
27
+ },
28
+ }
29
+ box.set = spy(box.set)
30
+
31
+ box.set(1)
32
+ assert.strictEqual(box.value, 1)
33
+ })
34
+
35
+ await it("should return a function which return the return value of the given function.", async () => {
36
+ const f = spy(() => 777)
37
+ const retv = f()
38
+ assert.strictEqual(retv, 777)
39
+ })
40
+
41
+ await it("should return a function which throw the thrown error of the given function.", async () => {
42
+ const f = spy(() => {
43
+ throw 666 //eslint-disable-line no-throw-literal
44
+ })
45
+ let error: any = undefined
46
+ try {
47
+ f()
48
+ } catch (e) {
49
+ error = e
50
+ }
51
+ assert.strictEqual(error, 666)
52
+ })
53
+ });
54
+
55
+ await describe("Spy.calls property", async () => {
56
+ await it("should be an array.", async () => {
57
+ const f = spy()
58
+ assert(Array.isArray(f.calls))
59
+ })
60
+
61
+ await it("should be empty before calling the spy.", async () => {
62
+ const f = spy()
63
+ assert.strictEqual(f.calls.length, 0)
64
+ })
65
+
66
+ await it("should contain call information after called one time.", async () => {
67
+ const f = spy()
68
+ f()
69
+ assert.strictEqual(f.calls.length, 1)
70
+ assert.deepStrictEqual(f.calls[0], {
71
+ type: "return",
72
+ this: undefined,
73
+ arguments: [],
74
+ return: undefined,
75
+ })
76
+ })
77
+
78
+ await it("should contain call information after called one time -- with args.", async () => {
79
+ const f = spy()
80
+ f.call(1, 2, 3)
81
+ assert.strictEqual(f.calls.length, 1)
82
+ assert.deepStrictEqual(f.calls[0], {
83
+ type: "return",
84
+ this: 1,
85
+ arguments: [2, 3],
86
+ return: undefined,
87
+ })
88
+ })
89
+
90
+ await it("should contain call information after called one time -- with args and return value.", async () => {
91
+ const f = spy(() => -1)
92
+ f.call(1, 2, 3)
93
+ assert.strictEqual(f.calls.length, 1)
94
+ assert.deepStrictEqual(f.calls[0], {
95
+ type: "return",
96
+ this: 1,
97
+ arguments: [2, 3],
98
+ return: -1,
99
+ })
100
+ })
101
+
102
+ await it("should contain call information after called two times -- with args and return value.", async () => {
103
+ const f = spy(() => -1)
104
+ f.call(1, 2, 3)
105
+ f.call(4, 5)
106
+ assert.strictEqual(f.calls.length, 2)
107
+ assert.deepStrictEqual(f.calls[0], {
108
+ type: "return",
109
+ this: 1,
110
+ arguments: [2, 3],
111
+ return: -1,
112
+ })
113
+ assert.deepStrictEqual(f.calls[1], {
114
+ type: "return",
115
+ this: 4,
116
+ arguments: [5],
117
+ return: -1,
118
+ })
119
+ })
120
+
121
+ await it("should contain call information regardless returned or thrown.", async () => {
122
+ const f = spy((toThrow: boolean) => {
123
+ if (toThrow) {
124
+ throw -1 //eslint-disable-line no-throw-literal
125
+ }
126
+ return 1
127
+ })
128
+ f(false)
129
+ try {
130
+ f(true)
131
+ } catch {
132
+ // ignore
133
+ }
134
+ assert.strictEqual(f.calls.length, 2)
135
+ assert.deepStrictEqual(f.calls[0], {
136
+ type: "return",
137
+ this: undefined,
138
+ arguments: [false],
139
+ return: 1,
140
+ })
141
+ assert.deepStrictEqual(f.calls[1], {
142
+ type: "throw",
143
+ this: undefined,
144
+ arguments: [true],
145
+ throw: -1,
146
+ })
147
+ })
148
+ });
149
+
150
+ await describe("Spy.firstCall property", async () => {
151
+ await it("should be null before calling the spy.", async () => {
152
+ const f = spy()
153
+ assert.strictEqual(f.firstCall, null)
154
+ })
155
+
156
+ await it("should be 'f.calls[0]' after calling the spy one time.", async () => {
157
+ const f = spy()
158
+ f()
159
+ assert.strictEqual(f.firstCall, f.calls[0])
160
+ })
161
+
162
+ await it("should be 'f.calls[0]' after calling the spy two times.", async () => {
163
+ const f = spy()
164
+ f()
165
+ f()
166
+ assert.strictEqual(f.firstCall, f.calls[0])
167
+ })
168
+ });
169
+
170
+ await describe("Spy.lastCall property", async () => {
171
+ await it("should be null before calling the spy.", async () => {
172
+ const f = spy()
173
+ assert.strictEqual(f.lastCall, null)
174
+ })
175
+
176
+ await it("should be 'f.calls[f.calls.length - 1]' after calling the spy one time.", async () => {
177
+ const f = spy()
178
+ f()
179
+ assert.strictEqual(f.lastCall, f.calls[f.calls.length - 1])
180
+ })
181
+
182
+ await it("should be 'f.calls[f.calls.length - 1]' after calling the spy two times.", async () => {
183
+ const f = spy()
184
+ f()
185
+ f()
186
+ assert.strictEqual(f.lastCall, f.calls[f.calls.length - 1])
187
+ })
188
+ });
189
+
190
+ await describe("Spy.returnedCalls property", async () => {
191
+ await it("should be an array.", async () => {
192
+ const f = spy()
193
+ assert(Array.isArray(f.returnedCalls))
194
+ })
195
+
196
+ await it("should be empty before calling the spy.", async () => {
197
+ const f = spy()
198
+ assert.strictEqual(f.returnedCalls.length, 0)
199
+ })
200
+
201
+ await it("should contain call information that `call.type === 'return'` in `f.calls` after calling the spy one time.", async () => {
202
+ const f = spy()
203
+ f()
204
+ assert.strictEqual(f.returnedCalls.length, 1)
205
+ assert.strictEqual(f.returnedCalls[0], f.calls[0])
206
+ })
207
+
208
+ await it("should contain call information that `call.type === 'return'` in `f.calls` after calling the spy two times.", async () => {
209
+ const f = spy()
210
+ f()
211
+ f()
212
+ assert.strictEqual(f.returnedCalls.length, 2)
213
+ assert.strictEqual(f.returnedCalls[0], f.calls[0])
214
+ assert.strictEqual(f.returnedCalls[1], f.calls[1])
215
+ })
216
+
217
+ await it("should not contain call information that `call.type === 'throw'`.", async () => {
218
+ const f = spy((toThrow: boolean) => {
219
+ if (toThrow) {
220
+ throw -1 //eslint-disable-line no-throw-literal
221
+ }
222
+ return 1
223
+ })
224
+ f(false)
225
+ try {
226
+ f(true)
227
+ } catch {
228
+ // ignore
229
+ }
230
+ f(false)
231
+ assert.strictEqual(f.calls.length, 3)
232
+ assert.strictEqual(f.returnedCalls.length, 2)
233
+ assert.strictEqual(f.returnedCalls[0], f.calls[0])
234
+ assert.strictEqual(f.returnedCalls[1], f.calls[2])
235
+ })
236
+
237
+ await it("should not contain call information that `call.type === 'throw'`. (2)", async () => {
238
+ const f = spy((toThrow: boolean) => {
239
+ if (toThrow) {
240
+ throw -1 //eslint-disable-line no-throw-literal
241
+ }
242
+ return 1
243
+ })
244
+ try {
245
+ f(true)
246
+ } catch {
247
+ // ignore
248
+ }
249
+ f(false)
250
+ try {
251
+ f(true)
252
+ } catch {
253
+ // ignore
254
+ }
255
+ assert.strictEqual(f.calls.length, 3)
256
+ assert.strictEqual(f.returnedCalls.length, 1)
257
+ assert.strictEqual(f.returnedCalls[0], f.calls[1])
258
+ })
259
+ });
260
+
261
+ await describe("Spy.firstReturnedCall property", async () => {
262
+ await it("should be null before calling the spy.", async () => {
263
+ const f = spy()
264
+ assert.strictEqual(f.firstReturnedCall, null)
265
+ })
266
+
267
+ await it("should be 'f.returnedCalls[0]' after calling the spy one time.", async () => {
268
+ const f = spy()
269
+ f()
270
+ assert.strictEqual(f.firstReturnedCall, f.returnedCalls[0])
271
+ })
272
+
273
+ await it("should be 'f.returnedCalls[0]' after calling the spy two times.", async () => {
274
+ const f = spy()
275
+ f()
276
+ f()
277
+ assert.strictEqual(f.firstReturnedCall, f.returnedCalls[0])
278
+ })
279
+
280
+ await it("should be 'f.returnedCalls[0]' after calling the spy even if `f.calls[0]` was thrown.", async () => {
281
+ const f = spy((toThrow: boolean) => {
282
+ if (toThrow) {
283
+ throw -1 //eslint-disable-line no-throw-literal
284
+ }
285
+ return 1
286
+ })
287
+ try {
288
+ f(true)
289
+ } catch {
290
+ // ignore
291
+ }
292
+ f(false)
293
+ assert.strictEqual(f.firstReturnedCall, f.returnedCalls[0])
294
+ })
295
+
296
+ await it("should be null after calling the spy even if all calls were thrown.", async () => {
297
+ const f = spy((toThrow: boolean) => {
298
+ if (toThrow) {
299
+ throw -1 //eslint-disable-line no-throw-literal
300
+ }
301
+ return 1
302
+ })
303
+ try {
304
+ f(true)
305
+ } catch {
306
+ // ignore
307
+ }
308
+ try {
309
+ f(true)
310
+ } catch {
311
+ // ignore
312
+ }
313
+ assert.strictEqual(f.firstReturnedCall, null)
314
+ })
315
+ });
316
+
317
+ await describe("Spy.lastReturnedCall property", async () => {
318
+ await it("should be null before calling the spy.", async () => {
319
+ const f = spy()
320
+ assert.strictEqual(f.lastReturnedCall, null)
321
+ })
322
+
323
+ await it("should be 'f.returnedCalls[f.returnedCalls.length - 1]' after calling the spy one time.", async () => {
324
+ const f = spy()
325
+ f()
326
+ assert.strictEqual(
327
+ f.lastReturnedCall,
328
+ f.returnedCalls[f.returnedCalls.length - 1],
329
+ )
330
+ })
331
+
332
+ await it("should be 'f.returnedCalls[f.returnedCalls.length - 1]' after calling the spy two times.", async () => {
333
+ const f = spy()
334
+ f()
335
+ f()
336
+ assert.strictEqual(
337
+ f.lastReturnedCall,
338
+ f.returnedCalls[f.returnedCalls.length - 1],
339
+ )
340
+ })
341
+
342
+ await it("should be 'f.returnedCalls[f.returnedCalls.length - 1]' after calling the spy even if `f.calls[f.calls.length - 1]` was thrown.", async () => {
343
+ const f = spy((toThrow: boolean) => {
344
+ if (toThrow) {
345
+ throw -1 //eslint-disable-line no-throw-literal
346
+ }
347
+ return 1
348
+ })
349
+ f(false)
350
+ try {
351
+ f(true)
352
+ } catch {
353
+ // ignore
354
+ }
355
+ assert.strictEqual(
356
+ f.lastReturnedCall,
357
+ f.returnedCalls[f.returnedCalls.length - 1],
358
+ )
359
+ })
360
+
361
+ await it("should be null after calling the spy even if all calls were thrown.", async () => {
362
+ const f = spy((toThrow: boolean) => {
363
+ if (toThrow) {
364
+ throw -1 //eslint-disable-line no-throw-literal
365
+ }
366
+ return 1
367
+ })
368
+ try {
369
+ f(true)
370
+ } catch {
371
+ // ignore
372
+ }
373
+ try {
374
+ f(true)
375
+ } catch {
376
+ // ignore
377
+ }
378
+ assert.strictEqual(f.lastReturnedCall, null)
379
+ })
380
+ });
381
+
382
+ await describe("Spy.thrownCalls property", async () => {
383
+ await it("should be an array.", async () => {
384
+ const f = spy()
385
+ assert(Array.isArray(f.thrownCalls))
386
+ })
387
+
388
+ await it("should be empty before calling the spy.", async () => {
389
+ const f = spy()
390
+ assert.strictEqual(f.thrownCalls.length, 0)
391
+ })
392
+
393
+ await it("should contain call information that `call.type === 'throw'` in `f.calls` after calling the spy one time.", async () => {
394
+ const f = spy(() => {
395
+ throw new Error()
396
+ })
397
+ try {
398
+ f()
399
+ } catch {
400
+ // ignore
401
+ }
402
+ assert.strictEqual(f.thrownCalls.length, 1)
403
+ assert.strictEqual(f.thrownCalls[0], f.calls[0])
404
+ })
405
+
406
+ await it("should contain call information that `call.type === 'throw'` in `f.calls` after calling the spy two times.", async () => {
407
+ const f = spy(() => {
408
+ throw new Error()
409
+ })
410
+ try {
411
+ f()
412
+ } catch {
413
+ // ignore
414
+ }
415
+ try {
416
+ f()
417
+ } catch {
418
+ // ignore
419
+ }
420
+ assert.strictEqual(f.thrownCalls.length, 2)
421
+ assert.strictEqual(f.thrownCalls[0], f.calls[0])
422
+ assert.strictEqual(f.thrownCalls[1], f.calls[1])
423
+ })
424
+
425
+ await it("should not contain call information that `call.type === 'return'`.", async () => {
426
+ const f = spy((toThrow: boolean) => {
427
+ if (toThrow) {
428
+ throw -1 //eslint-disable-line no-throw-literal
429
+ }
430
+ return 1
431
+ })
432
+ try {
433
+ f(true)
434
+ } catch {
435
+ // ignore
436
+ }
437
+ f(false)
438
+ try {
439
+ f(true)
440
+ } catch {
441
+ // ignore
442
+ }
443
+ assert.strictEqual(f.calls.length, 3)
444
+ assert.strictEqual(f.thrownCalls.length, 2)
445
+ assert.strictEqual(f.thrownCalls[0], f.calls[0])
446
+ assert.strictEqual(f.thrownCalls[1], f.calls[2])
447
+ })
448
+
449
+ await it("should not contain call information that `call.type === 'return'`. (2)", async () => {
450
+ const f = spy((toThrow: boolean) => {
451
+ if (toThrow) {
452
+ throw -1 //eslint-disable-line no-throw-literal
453
+ }
454
+ return 1
455
+ })
456
+ f(false)
457
+ try {
458
+ f(true)
459
+ } catch {
460
+ // ignore
461
+ }
462
+ f(false)
463
+ assert.strictEqual(f.calls.length, 3)
464
+ assert.strictEqual(f.thrownCalls.length, 1)
465
+ assert.strictEqual(f.thrownCalls[0], f.calls[1])
466
+ })
467
+ });
468
+
469
+ await describe("Spy.firstThrownCall property", async () => {
470
+ await it("should be null before calling the spy.", async () => {
471
+ const f = spy(() => {
472
+ throw new Error()
473
+ })
474
+ assert.strictEqual(f.firstThrownCall, null)
475
+ })
476
+
477
+ await it("should be 'f.thrownCalls[0]' after calling the spy one time.", async () => {
478
+ const f = spy(() => {
479
+ throw new Error()
480
+ })
481
+ try {
482
+ f()
483
+ } catch {
484
+ // ignore
485
+ }
486
+ assert.strictEqual(f.firstThrownCall, f.thrownCalls[0])
487
+ })
488
+
489
+ await it("should be 'f.thrownCalls[0]' after calling the spy two times.", async () => {
490
+ const f = spy(() => {
491
+ throw new Error()
492
+ })
493
+ try {
494
+ f()
495
+ } catch {
496
+ // ignore
497
+ }
498
+ try {
499
+ f()
500
+ } catch {
501
+ // ignore
502
+ }
503
+ assert.strictEqual(f.firstThrownCall, f.thrownCalls[0])
504
+ })
505
+
506
+ await it("should be 'f.thrownCalls[0]' after calling the spy even if `f.calls[0]` was returned.", async () => {
507
+ const f = spy((toThrow: boolean) => {
508
+ if (toThrow) {
509
+ throw -1 //eslint-disable-line no-throw-literal
510
+ }
511
+ return 1
512
+ })
513
+ f(false)
514
+ try {
515
+ f(true)
516
+ } catch {
517
+ // ignore
518
+ }
519
+ assert.strictEqual(f.firstThrownCall, f.thrownCalls[0])
520
+ })
521
+
522
+ await it("should be null after calling the spy even if all calls were returned.", async () => {
523
+ const f = spy()
524
+ f()
525
+ f()
526
+ assert.strictEqual(f.firstThrownCall, null)
527
+ })
528
+ });
529
+
530
+ await describe("Spy.lastThrownCall property", async () => {
531
+ await it("should be null before calling the spy.", async () => {
532
+ const f = spy(() => {
533
+ throw new Error()
534
+ })
535
+ assert.strictEqual(f.lastThrownCall, null)
536
+ })
537
+
538
+ await it("should be 'f.thrownCalls[f.thrownCalls.length - 1]' after calling the spy one time.", async () => {
539
+ const f = spy(() => {
540
+ throw new Error()
541
+ })
542
+ try {
543
+ f()
544
+ } catch {
545
+ // ignore
546
+ }
547
+ assert.strictEqual(
548
+ f.lastThrownCall,
549
+ f.thrownCalls[f.thrownCalls.length - 1],
550
+ )
551
+ })
552
+
553
+ await it("should be 'f.thrownCalls[f.thrownCalls.length - 1]' after calling the spy two times.", async () => {
554
+ const f = spy(() => {
555
+ throw new Error()
556
+ })
557
+ try {
558
+ f()
559
+ } catch {
560
+ // ignore
561
+ }
562
+ try {
563
+ f()
564
+ } catch {
565
+ // ignore
566
+ }
567
+ assert.strictEqual(
568
+ f.lastThrownCall,
569
+ f.thrownCalls[f.thrownCalls.length - 1],
570
+ )
571
+ })
572
+
573
+ await it("should be 'f.thrownCalls[f.thrownCalls.length - 1]' after calling the spy even if `f.calls[f.calls.length - 1]` was returned.", async () => {
574
+ const f = spy((toThrow: boolean) => {
575
+ if (toThrow) {
576
+ throw -1 //eslint-disable-line no-throw-literal
577
+ }
578
+ return 1
579
+ })
580
+ try {
581
+ f(true)
582
+ } catch {
583
+ // ignore
584
+ }
585
+ f(false)
586
+ assert.strictEqual(
587
+ f.lastThrownCall,
588
+ f.thrownCalls[f.thrownCalls.length - 1],
589
+ )
590
+ })
591
+
592
+ await it("should be null after calling the spy even if all calls were returned.", async () => {
593
+ const f = spy()
594
+ f()
595
+ f()
596
+ assert.strictEqual(f.lastThrownCall, null)
597
+ })
598
+ });
599
+
600
+ await describe("Spy.reset method", async () => {
601
+ await it("should be a function.", async () => {
602
+ const f = spy()
603
+ assert.strictEqual(typeof f.reset, "function")
604
+ })
605
+
606
+ await it("should do nothing before calling the spy.", async () => {
607
+ const f = spy()
608
+ f.reset()
609
+ assert.strictEqual(f.calls.length, 0)
610
+ })
611
+
612
+ await it("should clear `f.calls`.", async () => {
613
+ const f = spy()
614
+ f()
615
+ assert.strictEqual(f.calls.length, 1)
616
+ f.reset()
617
+ assert.strictEqual(f.calls.length, 0)
618
+ assert.strictEqual(f.calls[0], undefined)
619
+ })
620
+ });
621
+
622
+ await describe("Spy.toString method", async () => {
623
+ await it("should be a function.", async () => {
624
+ const f = spy()
625
+ assert.strictEqual(typeof f.toString, "function")
626
+ })
627
+
628
+ await it("should return the original function with a comment. (noop)", async () => {
629
+ const f = spy()
630
+ assert.strictEqual(f.toString(), "/* The spy of */ function(){}")
631
+ })
632
+
633
+ await it("should return the original function with a comment. (with f)", async () => {
634
+ const f0 = function original(): number {
635
+ return 777
636
+ }
637
+ const f = spy(f0)
638
+ assert.strictEqual(f.toString(), `/* The spy of */ ${f0}`)
639
+ })
640
+ });
641
+ }