import { asyncMerge } from './async-merge' ; function wait ( ms ) { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } describe ( 'wrapIntoAsyncMerge' , ( ) => { it ( 'should work for non-async functions' , ( ) => { const add = ( a , b ) : number => a + b ; const asyncMergedAdd = asyncMerge ( add ) ; expect ( asyncMergedAdd ( 2 , 2 ) ) . toBe ( 4 ) ; const createObject = ( ) : Record < string , unknown > => ( { } ) ; const asyncMergedCreateObject = asyncMerge ( createObject ) ; const object1 = asyncMergedCreateObject ( ) ; const object2 = asyncMergedCreateObject ( ) ; expect ( object1 ) . not . toBe ( object2 ) ; } ) ; it ( 'should work for async functions' , async ( ) => { const asyncMergedWait = asyncMerge ( wait ) ; const firstPromise = asyncMergedWait ( 200 ) ; const promises = Array . from ( { length : 10 } , ( ) => asyncMergedWait ( 200 ) ) ; for ( const promise of promises ) { expect ( promise ) . toBe ( firstPromise ) ; } await firstPromise ; const secondPromise = asyncMergedWait ( 200 ) ; expect ( secondPromise ) . not . toBe ( firstPromise ) ; const thirdPromise = asyncMergedWait ( 300 ) ; expect ( thirdPromise ) . not . toBe ( secondPromise ) ; } ) ; it ( 'should work for independent async functions' , async ( ) => { const asyncMergedWait = asyncMerge ( wait ) ; const wrappedWait = ( ms ) => wait ( ms ) ; const asyncMergedWrappedWait = asyncMerge ( wrappedWait ) ; const firstPromise = asyncMergedWait ( 200 ) ; const secondPromise = asyncMergedWrappedWait ( 200 ) ; expect ( secondPromise ) . not . toBe ( firstPromise ) ; await firstPromise ; await secondPromise ; } ) ; it ( 'should do not catch errors' , async ( done ) => { const fn = ( ) : Promise < never > => Promise . reject ( 'hello' ) ; const wrappedFn = asyncMerge ( fn ) ; try { await wrappedFn ( ) ; done . fail ( 'asyncMerge caught the error' ) ; } catch ( error ) { expect ( error ) . toBe ( 'hello' ) ; } done ( ) ; } ) ; } ) ;