@@ -265,18 +265,46 @@ class Assertions {
265
265
patterns : [ pattern ]
266
266
} ) ;
267
267
268
+ const checkMessage = ( assertion , message , powerAssert = false ) => {
269
+ if ( typeof message === 'undefined' || typeof message === 'string' ) {
270
+ return true ;
271
+ }
272
+
273
+ const error = new AssertionError ( {
274
+ assertion,
275
+ improperUsage : true ,
276
+ message : 'The assertion message must be a string' ,
277
+ values : [ formatWithLabel ( 'Called with:' , message ) ]
278
+ } ) ;
279
+
280
+ if ( powerAssert ) {
281
+ throw error ;
282
+ }
283
+
284
+ fail ( error ) ;
285
+ return false ;
286
+ } ;
287
+
268
288
this . pass = withSkip ( ( ) => {
269
289
pass ( ) ;
270
290
} ) ;
271
291
272
292
this . fail = withSkip ( message => {
293
+ if ( ! checkMessage ( 'fail' , message ) ) {
294
+ return ;
295
+ }
296
+
273
297
fail ( new AssertionError ( {
274
298
assertion : 'fail' ,
275
299
message : message || 'Test failed via `t.fail()`'
276
300
} ) ) ;
277
301
} ) ;
278
302
279
303
this . is = withSkip ( ( actual , expected , message ) => {
304
+ if ( ! checkMessage ( 'is' , message ) ) {
305
+ return ;
306
+ }
307
+
280
308
if ( Object . is ( actual , expected ) ) {
281
309
pass ( ) ;
282
310
} else {
@@ -303,6 +331,10 @@ class Assertions {
303
331
} ) ;
304
332
305
333
this . not = withSkip ( ( actual , expected , message ) => {
334
+ if ( ! checkMessage ( 'not' , message ) ) {
335
+ return ;
336
+ }
337
+
306
338
if ( Object . is ( actual , expected ) ) {
307
339
fail ( new AssertionError ( {
308
340
assertion : 'not' ,
@@ -316,6 +348,10 @@ class Assertions {
316
348
} ) ;
317
349
318
350
this . deepEqual = withSkip ( ( actual , expected , message ) => {
351
+ if ( ! checkMessage ( 'deepEqual' , message ) ) {
352
+ return ;
353
+ }
354
+
319
355
const result = concordance . compare ( actual , expected , concordanceOptions ) ;
320
356
if ( result . pass ) {
321
357
pass ( ) ;
@@ -332,6 +368,10 @@ class Assertions {
332
368
} ) ;
333
369
334
370
this . notDeepEqual = withSkip ( ( actual , expected , message ) => {
371
+ if ( ! checkMessage ( 'notDeepEqual' , message ) ) {
372
+ return ;
373
+ }
374
+
335
375
const result = concordance . compare ( actual , expected , concordanceOptions ) ;
336
376
if ( result . pass ) {
337
377
const actualDescriptor = result . actual || concordance . describe ( actual , concordanceOptions ) ;
@@ -351,6 +391,11 @@ class Assertions {
351
391
// operator, so we can determine the total number of arguments passed
352
392
// to the function.
353
393
let [ fn , expectations , message ] = args ;
394
+
395
+ if ( ! checkMessage ( 'throws' , message ) ) {
396
+ return ;
397
+ }
398
+
354
399
if ( typeof fn !== 'function' ) {
355
400
fail ( new AssertionError ( {
356
401
assertion : 'throws' ,
@@ -412,6 +457,11 @@ class Assertions {
412
457
413
458
this . throwsAsync = withSkip ( ( ...args ) => {
414
459
let [ thrower , expectations , message ] = args ;
460
+
461
+ if ( ! checkMessage ( 'throwsAsync' , message ) ) {
462
+ return Promise . resolve ( ) ;
463
+ }
464
+
415
465
if ( typeof thrower !== 'function' && ! isPromise ( thrower ) ) {
416
466
fail ( new AssertionError ( {
417
467
assertion : 'throwsAsync' ,
@@ -492,6 +542,10 @@ class Assertions {
492
542
} ) ;
493
543
494
544
this . notThrows = withSkip ( ( fn , message ) => {
545
+ if ( ! checkMessage ( 'notThrows' , message ) ) {
546
+ return ;
547
+ }
548
+
495
549
if ( typeof fn !== 'function' ) {
496
550
fail ( new AssertionError ( {
497
551
assertion : 'notThrows' ,
@@ -518,6 +572,10 @@ class Assertions {
518
572
} ) ;
519
573
520
574
this . notThrowsAsync = withSkip ( ( nonThrower , message ) => {
575
+ if ( ! checkMessage ( 'notThrowsAsync' , message ) ) {
576
+ return Promise . resolve ( ) ;
577
+ }
578
+
521
579
if ( typeof nonThrower !== 'function' && ! isPromise ( nonThrower ) ) {
522
580
fail ( new AssertionError ( {
523
581
assertion : 'notThrowsAsync' ,
@@ -574,20 +632,31 @@ class Assertions {
574
632
return handlePromise ( retval , true ) ;
575
633
} ) ;
576
634
577
- this . snapshot = withSkip ( ( expected , optionsOrMessage , message ) => {
578
- const options = { } ;
579
- if ( typeof optionsOrMessage === 'string' ) {
580
- message = optionsOrMessage ;
581
- } else if ( optionsOrMessage ) {
582
- options . id = optionsOrMessage . id ;
635
+ this . snapshot = withSkip ( ( expected , ...rest ) => {
636
+ let message ;
637
+ let snapshotOptions ;
638
+ if ( rest . length > 1 ) {
639
+ [ snapshotOptions , message ] = rest ;
640
+ } else {
641
+ const [ optionsOrMessage ] = rest ;
642
+ if ( typeof optionsOrMessage === 'object' ) {
643
+ snapshotOptions = optionsOrMessage ;
644
+ } else {
645
+ message = optionsOrMessage ;
646
+ }
583
647
}
584
648
585
- options . expected = expected ;
586
- options . message = message ;
649
+ if ( ! checkMessage ( 'snapshot' , message ) ) {
650
+ return ;
651
+ }
587
652
588
653
let result ;
589
654
try {
590
- result = compareWithSnapshot ( options ) ;
655
+ result = compareWithSnapshot ( {
656
+ expected,
657
+ id : snapshotOptions ? snapshotOptions . id : undefined ,
658
+ message
659
+ } ) ;
591
660
} catch ( error ) {
592
661
if ( ! ( error instanceof snapshotManager . SnapshotError ) ) {
593
662
throw error ;
@@ -625,6 +694,10 @@ class Assertions {
625
694
} ) ;
626
695
627
696
this . truthy = withSkip ( ( actual , message ) => {
697
+ if ( ! checkMessage ( 'truthy' , message ) ) {
698
+ return ;
699
+ }
700
+
628
701
if ( actual ) {
629
702
pass ( ) ;
630
703
} else {
@@ -638,6 +711,10 @@ class Assertions {
638
711
} ) ;
639
712
640
713
this . falsy = withSkip ( ( actual , message ) => {
714
+ if ( ! checkMessage ( 'falsy' , message ) ) {
715
+ return ;
716
+ }
717
+
641
718
if ( actual ) {
642
719
fail ( new AssertionError ( {
643
720
assertion : 'falsy' ,
@@ -651,6 +728,10 @@ class Assertions {
651
728
} ) ;
652
729
653
730
this . true = withSkip ( ( actual , message ) => {
731
+ if ( ! checkMessage ( 'true' , message ) ) {
732
+ return ;
733
+ }
734
+
654
735
if ( actual === true ) {
655
736
pass ( ) ;
656
737
} else {
@@ -663,6 +744,10 @@ class Assertions {
663
744
} ) ;
664
745
665
746
this . false = withSkip ( ( actual , message ) => {
747
+ if ( ! checkMessage ( 'false' , message ) ) {
748
+ return ;
749
+ }
750
+
666
751
if ( actual === false ) {
667
752
pass ( ) ;
668
753
} else {
@@ -675,6 +760,10 @@ class Assertions {
675
760
} ) ;
676
761
677
762
this . regex = withSkip ( ( string , regex , message ) => {
763
+ if ( ! checkMessage ( 'regex' , message ) ) {
764
+ return ;
765
+ }
766
+
678
767
if ( typeof string !== 'string' ) {
679
768
fail ( new AssertionError ( {
680
769
assertion : 'regex' ,
@@ -711,6 +800,10 @@ class Assertions {
711
800
} ) ;
712
801
713
802
this . notRegex = withSkip ( ( string , regex , message ) => {
803
+ if ( ! checkMessage ( 'notRegex' , message ) ) {
804
+ return ;
805
+ }
806
+
714
807
if ( typeof string !== 'string' ) {
715
808
fail ( new AssertionError ( {
716
809
assertion : 'notRegex' ,
@@ -749,6 +842,8 @@ class Assertions {
749
842
this . assert = withSkip ( withPowerAssert (
750
843
'assert(value, [message])' ,
751
844
( actual , message ) => {
845
+ checkMessage ( 'assert' , message , true ) ;
846
+
752
847
if ( ! actual ) {
753
848
throw new AssertionError ( {
754
849
assertion : 'assert' ,
0 commit comments