import { Assertion , Observable , TestFn , chain , fail , pass } from '../' ; import $$observable from 'symbol-observable' ; export function coerceToPromise < A > ( testFn ) { return new Promise ( ( resolve ) => { try { let result = testFn ( ) ; if ( isObservable ( result ) ) result = observableToPromise ( result ) ; resolve ( result ) ; } catch ( e ) { resolve ( fail ( e ) ) ; } } ) ; } function isObservable ( x ) { return typeof x [ $$observable ] === 'function' ; } function observableToPromise < A > ( obs ) { return new Promise ( ( resolve ) => { let _assertion = pass ( `Test Passed` ) as any as Assertion < A > ; obs . subscribe ( { next : ( assertion ) => { _assertion = chain ( ( ) => assertion , _assertion ) ; } , error : ( err ) => { resolve ( fail ( err ) ) ; } , complete : ( ) => { resolve ( _assertion ) ; } , } ) ; } ) ; }