@@ -21,33 +21,73 @@ ruleTester.run('prefer-expect-assertions', rule, {
21
21
it("it1", function() {
22
22
expect.assertions(1);
23
23
expect(someValue).toBe(true)
24
- })
24
+ });
25
25
` ,
26
26
'test("it1")' ,
27
27
'itHappensToStartWithIt("foo", function() {})' ,
28
28
'testSomething("bar", function() {})' ,
29
29
'it(async () => {expect.assertions(0);})' ,
30
+ dedent `
31
+ it("returns numbers that are greater than four", function() {
32
+ expect.assertions(2);
33
+
34
+ for(let thing in things) {
35
+ expect(number).toBeGreaterThan(4);
36
+ }
37
+ });
38
+ ` ,
39
+ dedent `
40
+ it("returns numbers that are greater than four", function() {
41
+ expect.hasAssertions();
42
+
43
+ for (let i = 0; i < things.length; i++) {
44
+ expect(number).toBeGreaterThan(4);
45
+ }
46
+ });
47
+ ` ,
30
48
{
31
49
code : dedent `
32
50
it("it1", async () => {
33
51
expect.assertions(1);
34
52
expect(someValue).toBe(true)
35
- })
53
+ });
36
54
` ,
37
55
options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
38
56
} ,
39
57
{
40
58
code : dedent `
41
59
it("it1", function() {
42
60
expect(someValue).toBe(true)
43
- })
61
+ });
44
62
` ,
45
63
options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
46
64
} ,
47
65
{
48
66
code : 'it("it1", () => {})' ,
49
67
options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
50
68
} ,
69
+ {
70
+ code : dedent `
71
+ it("returns numbers that are greater than four", async () => {
72
+ expect.assertions(2);
73
+
74
+ for(let thing in things) {
75
+ expect(number).toBeGreaterThan(4);
76
+ }
77
+ });
78
+ ` ,
79
+ options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
80
+ } ,
81
+ {
82
+ code : dedent `
83
+ it("returns numbers that are greater than four", () => {
84
+ for(let thing in things) {
85
+ expect(number).toBeGreaterThan(4);
86
+ }
87
+ });
88
+ ` ,
89
+ options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
90
+ } ,
51
91
] ,
52
92
invalid : [
53
93
{
@@ -95,7 +135,7 @@ ruleTester.run('prefer-expect-assertions', rule, {
95
135
it("it1", function() {
96
136
someFunctionToDo();
97
137
someFunctionToDo2();
98
- })
138
+ });
99
139
` ,
100
140
errors : [
101
141
{
@@ -109,7 +149,7 @@ ruleTester.run('prefer-expect-assertions', rule, {
109
149
it("it1", function() {
110
150
expect.hasAssertions();someFunctionToDo();
111
151
someFunctionToDo2();
112
- })
152
+ });
113
153
` ,
114
154
} ,
115
155
{
@@ -118,7 +158,7 @@ ruleTester.run('prefer-expect-assertions', rule, {
118
158
it("it1", function() {
119
159
expect.assertions();someFunctionToDo();
120
160
someFunctionToDo2();
121
- })
161
+ });
122
162
` ,
123
163
} ,
124
164
] ,
@@ -223,7 +263,7 @@ ruleTester.run('prefer-expect-assertions', rule, {
223
263
someFunctionToDo();
224
264
someFunctionToDo2();
225
265
});
226
- })
266
+ });
227
267
` ,
228
268
errors : [
229
269
{
@@ -236,7 +276,7 @@ ruleTester.run('prefer-expect-assertions', rule, {
236
276
output : dedent `
237
277
it("it1", function() {
238
278
expect.hasAssertions();
239
- })
279
+ });
240
280
` ,
241
281
} ,
242
282
] ,
@@ -247,7 +287,64 @@ ruleTester.run('prefer-expect-assertions', rule, {
247
287
code : dedent `
248
288
it("it1", async function() {
249
289
expect(someValue).toBe(true);
250
- })
290
+ });
291
+ ` ,
292
+ options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
293
+ errors : [
294
+ {
295
+ messageId : 'haveExpectAssertions' ,
296
+ column : 1 ,
297
+ line : 1 ,
298
+ } ,
299
+ ] ,
300
+ } ,
301
+ {
302
+ code : dedent `
303
+ it("returns numbers that are greater than four", async () => {
304
+ for(let thing in things) {
305
+ expect(number).toBeGreaterThan(4);
306
+ }
307
+ });
308
+ ` ,
309
+ options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
310
+ errors : [
311
+ {
312
+ messageId : 'haveExpectAssertions' ,
313
+ column : 1 ,
314
+ line : 1 ,
315
+ } ,
316
+ ] ,
317
+ } ,
318
+ {
319
+ code : dedent `
320
+ it("returns numbers that are greater than four", async () => {
321
+ for (const number of getNumbers()) {
322
+ expect(number).toBeGreaterThan(4);
323
+ }
324
+ });
325
+ ` ,
326
+ options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
327
+ errors : [
328
+ {
329
+ messageId : 'haveExpectAssertions' ,
330
+ column : 1 ,
331
+ line : 1 ,
332
+ } ,
333
+ ] ,
334
+ } ,
335
+ {
336
+ code : dedent `
337
+ it("returns numbers that are greater than four", async () => {
338
+ for (const number of getNumbers()) {
339
+ expect(number).toBeGreaterThan(4);
340
+ }
341
+ });
342
+
343
+ it("returns numbers that are greater than five", () => {
344
+ for (const number of getNumbers()) {
345
+ expect(number).toBeGreaterThan(5);
346
+ }
347
+ });
251
348
` ,
252
349
options : [ { onlyFunctionsWithAsyncKeyword : true } ] ,
253
350
errors : [
@@ -261,6 +358,497 @@ ruleTester.run('prefer-expect-assertions', rule, {
261
358
] ,
262
359
} ) ;
263
360
361
+ ruleTester . run ( 'prefer-expect-assertions (loops)' , rule , {
362
+ valid : [
363
+ {
364
+ code : dedent `
365
+ const expectNumbersToBeGreaterThan = (numbers, value) => {
366
+ for (let number of numbers) {
367
+ expect(number).toBeGreaterThan(value);
368
+ }
369
+ };
370
+
371
+ it('returns numbers that are greater than two', function () {
372
+ expectNumbersToBeGreaterThan(getNumbers(), 2);
373
+ });
374
+ ` ,
375
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
376
+ } ,
377
+ {
378
+ code : dedent `
379
+ it("returns numbers that are greater than five", function () {
380
+ expect.assertions(2);
381
+
382
+ for (const number of getNumbers()) {
383
+ expect(number).toBeGreaterThan(5);
384
+ }
385
+ });
386
+ ` ,
387
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
388
+ } ,
389
+ {
390
+ code : dedent `
391
+ it("returns things that are less than ten", function () {
392
+ expect.hasAssertions();
393
+
394
+ for (const thing in things) {
395
+ expect(thing).toBeLessThan(10);
396
+ }
397
+ });
398
+ ` ,
399
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
400
+ } ,
401
+ ] ,
402
+ invalid : [
403
+ {
404
+ code : dedent `
405
+ it('only returns numbers that are greater than six', () => {
406
+ for (const number of getNumbers()) {
407
+ expect(number).toBeGreaterThan(6);
408
+ }
409
+ });
410
+ ` ,
411
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
412
+ errors : [
413
+ {
414
+ messageId : 'haveExpectAssertions' ,
415
+ column : 1 ,
416
+ line : 1 ,
417
+ } ,
418
+ ] ,
419
+ } ,
420
+ {
421
+ code : dedent `
422
+ it('returns numbers that are greater than two', function () {
423
+ const expectNumbersToBeGreaterThan = (numbers, value) => {
424
+ for (let number of numbers) {
425
+ expect(number).toBeGreaterThan(value);
426
+ }
427
+ };
428
+
429
+ expectNumbersToBeGreaterThan(getNumbers(), 2);
430
+ });
431
+ ` ,
432
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
433
+ errors : [
434
+ {
435
+ messageId : 'haveExpectAssertions' ,
436
+ column : 1 ,
437
+ line : 1 ,
438
+ } ,
439
+ ] ,
440
+ } ,
441
+ {
442
+ code : dedent `
443
+ it("only returns numbers that are greater than seven", function () {
444
+ const numbers = getNumbers();
445
+
446
+ for (let i = 0; i < numbers.length; i++) {
447
+ expect(numbers[i]).toBeGreaterThan(7);
448
+ }
449
+ });
450
+ ` ,
451
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
452
+ errors : [
453
+ {
454
+ messageId : 'haveExpectAssertions' ,
455
+ column : 1 ,
456
+ line : 1 ,
457
+ } ,
458
+ ] ,
459
+ } ,
460
+ {
461
+ code : dedent `
462
+ it('has the number two', () => {
463
+ expect(number).toBe(2);
464
+ });
465
+
466
+ it('only returns numbers that are less than twenty', () => {
467
+ for (const number of getNumbers()) {
468
+ expect(number).toBeLessThan(20);
469
+ }
470
+ });
471
+ ` ,
472
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
473
+ errors : [
474
+ {
475
+ messageId : 'haveExpectAssertions' ,
476
+ column : 1 ,
477
+ line : 5 ,
478
+ } ,
479
+ ] ,
480
+ } ,
481
+ {
482
+ code : dedent `
483
+ it("is wrong");
484
+
485
+ it("is a test", () => {
486
+ for (const number of getNumbers()) {
487
+ expect(number).toBeGreaterThan(4);
488
+ }
489
+ });
490
+ ` ,
491
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
492
+ errors : [
493
+ {
494
+ messageId : 'haveExpectAssertions' ,
495
+ column : 1 ,
496
+ line : 3 ,
497
+ } ,
498
+ ] ,
499
+ } ,
500
+ {
501
+ code : dedent `
502
+ it("is a number that is greater than four", () => {
503
+ expect(number).toBeGreaterThan(4);
504
+ });
505
+
506
+ it("returns numbers that are greater than four", () => {
507
+ for (const number of getNumbers()) {
508
+ expect(number).toBeGreaterThan(4);
509
+ }
510
+ });
511
+
512
+ it("returns numbers that are greater than five", () => {
513
+ expect(number).toBeGreaterThan(5);
514
+ });
515
+ ` ,
516
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
517
+ errors : [
518
+ {
519
+ messageId : 'haveExpectAssertions' ,
520
+ column : 1 ,
521
+ line : 5 ,
522
+ } ,
523
+ ] ,
524
+ } ,
525
+ {
526
+ code : dedent `
527
+ it.each([1, 2, 3])("returns numbers that are greater than four", () => {
528
+ for (const number of getNumbers()) {
529
+ expect(number).toBeGreaterThan(4);
530
+ }
531
+ });
532
+
533
+ it("is a number that is greater than four", () => {
534
+ expect(number).toBeGreaterThan(4);
535
+ });
536
+ ` ,
537
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
538
+ errors : [
539
+ {
540
+ messageId : 'haveExpectAssertions' ,
541
+ column : 1 ,
542
+ line : 1 ,
543
+ } ,
544
+ ] ,
545
+ } ,
546
+ {
547
+ code : dedent `
548
+ it("returns numbers that are greater than four", () => {
549
+ for (const number of getNumbers()) {
550
+ expect(number).toBeGreaterThan(4);
551
+ }
552
+ });
553
+
554
+ it("is a number that is greater than four", () => {
555
+ expect(number).toBeGreaterThan(4);
556
+ });
557
+ ` ,
558
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
559
+ errors : [
560
+ {
561
+ messageId : 'haveExpectAssertions' ,
562
+ column : 1 ,
563
+ line : 1 ,
564
+ } ,
565
+ ] ,
566
+ } ,
567
+ {
568
+ code : dedent `
569
+ it("returns numbers that are greater than four", () => {
570
+ for (const number of getNumbers()) {
571
+ expect(number).toBeGreaterThan(4);
572
+ }
573
+ });
574
+
575
+ it("is a number that is greater than four", () => {
576
+ expect.hasAssertions();
577
+
578
+ expect(number).toBeGreaterThan(4);
579
+ });
580
+ ` ,
581
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
582
+ errors : [
583
+ {
584
+ messageId : 'haveExpectAssertions' ,
585
+ column : 1 ,
586
+ line : 1 ,
587
+ } ,
588
+ ] ,
589
+ } ,
590
+ {
591
+ code : dedent `
592
+ it("it1", () => {
593
+ expect.hasAssertions();
594
+
595
+ for (const number of getNumbers()) {
596
+ expect(number).toBeGreaterThan(0);
597
+ }
598
+ });
599
+
600
+ it("it1", () => {
601
+ for (const number of getNumbers()) {
602
+ expect(number).toBeGreaterThan(0);
603
+ }
604
+ });
605
+ ` ,
606
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
607
+ errors : [
608
+ {
609
+ messageId : 'haveExpectAssertions' ,
610
+ column : 1 ,
611
+ line : 9 ,
612
+ } ,
613
+ ] ,
614
+ } ,
615
+ {
616
+ code : dedent `
617
+ it("returns numbers that are greater than four", async () => {
618
+ for (const number of await getNumbers()) {
619
+ expect(number).toBeGreaterThan(4);
620
+ }
621
+ });
622
+
623
+ it("returns numbers that are greater than five", () => {
624
+ for (const number of getNumbers()) {
625
+ expect(number).toBeGreaterThan(5);
626
+ }
627
+ });
628
+ ` ,
629
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
630
+ errors : [
631
+ {
632
+ messageId : 'haveExpectAssertions' ,
633
+ column : 1 ,
634
+ line : 1 ,
635
+ } ,
636
+ {
637
+ messageId : 'haveExpectAssertions' ,
638
+ column : 1 ,
639
+ line : 7 ,
640
+ } ,
641
+ ] ,
642
+ } ,
643
+ {
644
+ code : dedent `
645
+ it("it1", async () => {
646
+ expect.hasAssertions();
647
+
648
+ for (const number of getNumbers()) {
649
+ expect(number).toBeGreaterThan(4);
650
+ }
651
+ });
652
+
653
+ it("it1", () => {
654
+ for (const number of getNumbers()) {
655
+ expect(number).toBeGreaterThan(4);
656
+ }
657
+ });
658
+ ` ,
659
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
660
+ errors : [
661
+ {
662
+ messageId : 'haveExpectAssertions' ,
663
+ column : 1 ,
664
+ line : 9 ,
665
+ } ,
666
+ ] ,
667
+ } ,
668
+ {
669
+ code : dedent `
670
+ it.skip.each\`\`("it1", async () => {
671
+ expect.hasAssertions();
672
+
673
+ for (const number of getNumbers()) {
674
+ expect(number).toBeGreaterThan(4);
675
+ }
676
+ });
677
+
678
+ it("it1", () => {
679
+ for (const number of getNumbers()) {
680
+ expect(number).toBeGreaterThan(4);
681
+ }
682
+ });
683
+ ` ,
684
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
685
+ errors : [
686
+ {
687
+ messageId : 'haveExpectAssertions' ,
688
+ column : 1 ,
689
+ line : 9 ,
690
+ } ,
691
+ ] ,
692
+ } ,
693
+ {
694
+ code : dedent `
695
+ it("it1", async () => {
696
+ for (const number of getNumbers()) {
697
+ expect(number).toBeGreaterThan(4);
698
+ }
699
+ });
700
+
701
+ it("it1", () => {
702
+ expect.hasAssertions();
703
+
704
+ for (const number of getNumbers()) {
705
+ expect(number).toBeGreaterThan(4);
706
+ }
707
+ });
708
+ ` ,
709
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
710
+ errors : [
711
+ {
712
+ messageId : 'haveExpectAssertions' ,
713
+ column : 1 ,
714
+ line : 1 ,
715
+ } ,
716
+ ] ,
717
+ } ,
718
+ ] ,
719
+ } ) ;
720
+
721
+ ruleTester . run ( 'prefer-expect-assertions (mixed)' , rule , {
722
+ valid : [
723
+ {
724
+ code : dedent `
725
+ it('only returns numbers that are greater than zero', async () => {
726
+ expect.hasAssertions();
727
+
728
+ for (const number of await getNumbers()) {
729
+ expect(number).toBeGreaterThan(0);
730
+ }
731
+ });
732
+ ` ,
733
+ options : [
734
+ {
735
+ onlyFunctionsWithAsyncKeyword : true ,
736
+ onlyFunctionsWithExpectInLoop : true ,
737
+ } ,
738
+ ] ,
739
+ } ,
740
+ {
741
+ code : dedent `
742
+ it('only returns numbers that are greater than zero', async () => {
743
+ expect.assertions(2);
744
+
745
+ for (const number of await getNumbers()) {
746
+ expect(number).toBeGreaterThan(0);
747
+ }
748
+ });
749
+ ` ,
750
+ options : [
751
+ {
752
+ onlyFunctionsWithAsyncKeyword : true ,
753
+ onlyFunctionsWithExpectInLoop : true ,
754
+ } ,
755
+ ] ,
756
+ } ,
757
+ ] ,
758
+ invalid : [
759
+ {
760
+ code : dedent `
761
+ it('only returns numbers that are greater than zero', () => {
762
+ for (const number of getNumbers()) {
763
+ expect(number).toBeGreaterThan(0);
764
+ }
765
+ });
766
+
767
+ it("is zero", () => {
768
+ expect.hasAssertions();
769
+
770
+ expect(0).toBe(0);
771
+ });
772
+ ` ,
773
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
774
+ errors : [
775
+ {
776
+ messageId : 'haveExpectAssertions' ,
777
+ column : 1 ,
778
+ line : 1 ,
779
+ } ,
780
+ ] ,
781
+ } ,
782
+ {
783
+ code : dedent `
784
+ it('only returns numbers that are greater than zero', () => {
785
+ expect.hasAssertions();
786
+
787
+ for (const number of getNumbers()) {
788
+ expect(number).toBeGreaterThan(0);
789
+ }
790
+ });
791
+
792
+ it('only returns numbers that are less than 100', () => {
793
+ for (const number of getNumbers()) {
794
+ expect(number).toBeLessThan(0);
795
+ }
796
+ });
797
+ ` ,
798
+ options : [ { onlyFunctionsWithExpectInLoop : true } ] ,
799
+ errors : [
800
+ {
801
+ messageId : 'haveExpectAssertions' ,
802
+ column : 1 ,
803
+ line : 9 ,
804
+ } ,
805
+ ] ,
806
+ } ,
807
+ {
808
+ code : dedent `
809
+ it("to be true", async function() {
810
+ expect(someValue).toBe(true);
811
+ });
812
+ ` ,
813
+ options : [
814
+ {
815
+ onlyFunctionsWithAsyncKeyword : true ,
816
+ onlyFunctionsWithExpectInLoop : true ,
817
+ } ,
818
+ ] ,
819
+ errors : [
820
+ {
821
+ messageId : 'haveExpectAssertions' ,
822
+ column : 1 ,
823
+ line : 1 ,
824
+ } ,
825
+ ] ,
826
+ } ,
827
+ {
828
+ code : dedent `
829
+ it('only returns numbers that are greater than zero', async () => {
830
+ for (const number of getNumbers()) {
831
+ expect(number).toBeGreaterThan(0);
832
+ }
833
+ });
834
+ ` ,
835
+ options : [
836
+ {
837
+ onlyFunctionsWithAsyncKeyword : true ,
838
+ onlyFunctionsWithExpectInLoop : true ,
839
+ } ,
840
+ ] ,
841
+ errors : [
842
+ {
843
+ messageId : 'haveExpectAssertions' ,
844
+ column : 1 ,
845
+ line : 1 ,
846
+ } ,
847
+ ] ,
848
+ } ,
849
+ ] ,
850
+ } ) ;
851
+
264
852
ruleTester . run ( '.each support' , rule , {
265
853
valid : [
266
854
'test.each()("is fine", () => { expect.assertions(0); })' ,
0 commit comments