11/* eslint-disable deprecation/deprecation */
2- import { describe , expect , test , vi } from 'vitest' ;
2+ import { afterAll , beforeAll , describe , expect , test , vi } from 'vitest' ;
33import { rejectedSyncPromise , resolvedSyncPromise , SyncPromise } from '../../../src/utils/syncpromise' ;
44
55describe ( 'SyncPromise' , ( ) => {
6+ // @ts -expect-error We want to ensure to test this without the native Promise.try
7+ const promiseTry = Promise . try ;
8+
9+ beforeAll ( ( ) => {
10+ // @ts -expect-error We want to ensure to test this without the native Promise.try
11+ Promise . try = undefined ;
12+ } ) ;
13+
14+ afterAll ( ( ) => {
15+ // @ts -expect-error Restore this
16+ Promise . try = promiseTry ;
17+ } ) ;
18+
619 test ( 'simple' , async ( ) => {
720 expect . assertions ( 1 ) ;
821
@@ -13,6 +26,35 @@ describe('SyncPromise', () => {
1326 } ) ;
1427 } ) ;
1528
29+ test ( 'simple error catching' , async ( ) => {
30+ expect . assertions ( 1 ) ;
31+
32+ const error = new Error ( 'test error' ) ;
33+
34+ return new SyncPromise < number > ( ( ) => {
35+ throw error ;
36+ } ) . catch ( val => {
37+ expect ( val ) . toBe ( error ) ;
38+ } ) ;
39+ } ) ;
40+
41+ test ( 'simple error in then' , async ( ) => {
42+ expect . assertions ( 1 ) ;
43+
44+ const error = new Error ( 'test error' ) ;
45+
46+ return new SyncPromise < number > ( ( ) => {
47+ throw error ;
48+ } ) . then (
49+ ( ) => {
50+ throw new Error ( 'THIS SHOULD NOT BE CALLED!' ) ;
51+ } ,
52+ val => {
53+ expect ( val ) . toBe ( error ) ;
54+ } ,
55+ ) ;
56+ } ) ;
57+
1658 test ( 'simple chaining' , async ( ) => {
1759 expect . assertions ( 1 ) ;
1860
@@ -99,6 +141,9 @@ describe('SyncPromise', () => {
99141 expect . assertions ( 1 ) ;
100142
101143 const p = resolvedSyncPromise ( 10 ) ;
144+
145+ console . log ( p ) ;
146+
102147 return p . then ( val => {
103148 expect ( val ) . toBe ( 10 ) ;
104149 } ) ;
@@ -267,3 +312,245 @@ describe('SyncPromise', () => {
267312 } ) ;
268313 } ) ;
269314} ) ;
315+
316+ describe ( 'resolvedSyncPromise' , ( ) => {
317+ test ( 'simple chaining' , async ( ) => {
318+ expect . assertions ( 1 ) ;
319+
320+ return new SyncPromise < number > ( resolve => {
321+ resolve ( 42 ) ;
322+ } )
323+ . then ( _ => resolvedSyncPromise ( 'a' ) )
324+ . then ( _ => resolvedSyncPromise ( 0.1 ) )
325+ . then ( _ => resolvedSyncPromise ( false ) )
326+ . then ( val => {
327+ expect ( val ) . toBe ( false ) ;
328+ } ) ;
329+ } ) ;
330+
331+ test ( 'compare to regular promise' , async ( ) => {
332+ expect . assertions ( 2 ) ;
333+
334+ const ap = new Promise < string > ( resolve => {
335+ resolve ( '1' ) ;
336+ } ) ;
337+
338+ const bp = new Promise < string > ( resolve => {
339+ resolve ( '2' ) ;
340+ } ) ;
341+
342+ const cp = new Promise < string > ( resolve => {
343+ resolve ( '3' ) ;
344+ } ) ;
345+
346+ const fp = async ( s : PromiseLike < string > , prepend : string ) =>
347+ new Promise < string > ( resolve => {
348+ void s
349+ . then ( val => {
350+ resolve ( prepend + val ) ;
351+ } )
352+ . then ( null , _ => {
353+ // bla
354+ } ) ;
355+ } ) ;
356+
357+ const res = await cp
358+ . then ( async val => fp ( Promise . resolve ( 'x' ) , val ) )
359+ . then ( async val => fp ( bp , val ) )
360+ . then ( async val => fp ( ap , val ) ) ;
361+
362+ expect ( res ) . toBe ( '3x21' ) ;
363+
364+ const a = new SyncPromise < string > ( resolve => {
365+ resolve ( '1' ) ;
366+ } ) ;
367+
368+ const b = new SyncPromise < string > ( resolve => {
369+ resolve ( '2' ) ;
370+ } ) ;
371+
372+ const c = new SyncPromise < string > ( resolve => {
373+ resolve ( '3' ) ;
374+ } ) ;
375+
376+ const f = ( s : SyncPromise < string > , prepend : string ) =>
377+ new SyncPromise < string > ( resolve => {
378+ void s
379+ . then ( val => {
380+ resolve ( prepend + val ) ;
381+ } )
382+ . then ( null , ( ) => {
383+ // no-empty
384+ } ) ;
385+ } ) ;
386+
387+ return (
388+ c
389+ // @ts -expect-error Argument of type 'PromiseLike<string>' is not assignable to parameter of type 'SyncPromise<string>'
390+ . then ( val => f ( resolvedSyncPromise ( 'x' ) , val ) )
391+ . then ( val => f ( b , val ) )
392+ . then ( val => f ( a , val ) )
393+ . then ( val => {
394+ expect ( val ) . toBe ( res ) ;
395+ } )
396+ ) ;
397+ } ) ;
398+
399+ test ( 'simple static' , async ( ) => {
400+ expect . assertions ( 1 ) ;
401+
402+ const p = resolvedSyncPromise ( 10 ) ;
403+
404+ return p . then ( val => {
405+ expect ( val ) . toBe ( 10 ) ;
406+ } ) ;
407+ } ) ;
408+
409+ test ( 'calling the callback immediately' , ( ) => {
410+ expect . assertions ( 1 ) ;
411+
412+ let foo : number = 1 ;
413+
414+ new SyncPromise < number > ( _ => {
415+ foo = 2 ;
416+ } ) ;
417+
418+ expect ( foo ) . toEqual ( 2 ) ;
419+ } ) ;
420+
421+ test ( 'calling the callback not immediately' , ( ) => {
422+ vi . useFakeTimers ( ) ;
423+ expect . assertions ( 4 ) ;
424+
425+ const qp = new SyncPromise < number > ( resolve =>
426+ setTimeout ( ( ) => {
427+ resolve ( 2 ) ;
428+ } ) ,
429+ ) ;
430+ void qp
431+ . then ( value => {
432+ expect ( value ) . toEqual ( 2 ) ;
433+ } )
434+ . then ( null , ( ) => {
435+ // no-empty
436+ } ) ;
437+ expect ( qp ) . not . toHaveProperty ( '_value' ) ;
438+ void qp
439+ . then ( value => {
440+ expect ( value ) . toEqual ( 2 ) ;
441+ } )
442+ . then ( null , ( ) => {
443+ // no-empty
444+ } ) ;
445+ vi . runAllTimers ( ) ;
446+ expect ( qp ) . toHaveProperty ( '_value' ) ;
447+ } ) ;
448+
449+ test ( 'multiple then returning undefined' , async ( ) => {
450+ expect . assertions ( 3 ) ;
451+
452+ return resolvedSyncPromise ( 2 )
453+ . then ( result => {
454+ expect ( result ) . toEqual ( 2 ) ;
455+ } )
456+ . then ( result => {
457+ expect ( result ) . toBeUndefined ( ) ;
458+ } )
459+ . then ( result => {
460+ expect ( result ) . toBeUndefined ( ) ;
461+ } ) ;
462+ } ) ;
463+
464+ test ( 'multiple then returning different values' , async ( ) => {
465+ expect . assertions ( 3 ) ;
466+
467+ return resolvedSyncPromise ( 2 )
468+ . then ( result => {
469+ expect ( result ) . toEqual ( 2 ) ;
470+ return 3 ;
471+ } )
472+ . then ( result => {
473+ expect ( result ) . toEqual ( 3 ) ;
474+ return 4 ;
475+ } )
476+ . then ( result => {
477+ expect ( result ) . toEqual ( 4 ) ;
478+ } ) ;
479+ } ) ;
480+
481+ test ( 'multiple then returning different SyncPromise' , async ( ) => {
482+ expect . assertions ( 2 ) ;
483+
484+ return resolvedSyncPromise ( 2 )
485+ . then ( result => {
486+ expect ( result ) . toEqual ( 2 ) ;
487+ return resolvedSyncPromise ( 'yo' ) ;
488+ } )
489+ . then ( result => {
490+ expect ( result ) . toEqual ( 'yo' ) ;
491+ } ) ;
492+ } ) ;
493+ } ) ;
494+
495+ describe ( 'rejectedSyncPromise' , ( ) => {
496+ test ( 'simple' , async ( ) => {
497+ expect . assertions ( 1 ) ;
498+
499+ return new SyncPromise < number > ( resolve => {
500+ resolve ( 42 ) ;
501+ } ) . then ( val => {
502+ expect ( val ) . toBe ( 42 ) ;
503+ } ) ;
504+ } ) ;
505+
506+ test ( 'simple error catching' , async ( ) => {
507+ expect . assertions ( 1 ) ;
508+
509+ const error = new Error ( 'test error' ) ;
510+
511+ return rejectedSyncPromise ( error ) . then (
512+ ( ) => {
513+ throw new Error ( 'THIS SHOULD NOT BE CALLED!' ) ;
514+ } ,
515+ val => {
516+ expect ( val ) . toBe ( error ) ;
517+ } ,
518+ ) ;
519+ } ) ;
520+ test ( 'reject immediately and do not call then' , async ( ) => {
521+ expect . assertions ( 1 ) ;
522+
523+ return new SyncPromise < number > ( ( _ , reject ) => {
524+ reject ( 'test' ) ;
525+ } )
526+ . then ( _ => {
527+ expect ( true ) . toBeFalsy ( ) ;
528+ } )
529+ . then ( null , reason => {
530+ expect ( reason ) . toBe ( 'test' ) ;
531+ } ) ;
532+ } ) ;
533+
534+ test ( 'reject' , async ( ) => {
535+ expect . assertions ( 1 ) ;
536+
537+ return new SyncPromise < number > ( ( _ , reject ) => {
538+ reject ( 'test' ) ;
539+ } ) . then ( null , reason => {
540+ expect ( reason ) . toBe ( 'test' ) ;
541+ } ) ;
542+ } ) ;
543+
544+ test ( 'rejecting after first then' , async ( ) => {
545+ expect . assertions ( 2 ) ;
546+
547+ return resolvedSyncPromise ( 2 )
548+ . then ( value => {
549+ expect ( value ) . toEqual ( 2 ) ;
550+ return rejectedSyncPromise ( 'wat' ) ;
551+ } )
552+ . then ( null , reason => {
553+ expect ( reason ) . toBe ( 'wat' ) ;
554+ } ) ;
555+ } ) ;
556+ } ) ;
0 commit comments