import type { ZWaveError , ZWaveErrorCodes } from "../error/ZWaveError" ; export interface AssertZWaveErrorOptions { messageMatches ? : string | RegExp ; errorCode ? : ZWaveErrorCodes ; context ? : unknown ; } param valueOrFactory param options export function assertZWaveError < T > ( valueOrFactory , options = { } ) { const { messageMatches , errorCode , context } = options ; function _assertZWaveError ( e ) { expect ( e . constructor . name ) . toBe ( "ZWaveError" ) ; expect ( e . code ) . toBeNumber ( ) ; } function handleError ( e ) { _assertZWaveError ( e ) ; if ( messageMatches != undefined ) expect ( e . message ) . toMatch ( messageMatches ) ; if ( errorCode != undefined ) expect ( e . code ) . toBe ( errorCode ) ; if ( context != undefined ) expect ( e . context ) . toBe ( context ) ; } function fail ( ) { throw new Error ( "The factory function did not throw any error!" ) ; } if ( typeof valueOrFactory === "function" ) { try { const result = valueOrFactory ( ) ; if ( result instanceof Promise ) { return result . then ( fail , handleError ) as any ; } } catch ( e ) { return void handleError ( e ) as any ; } fail ( ) ; } else { handleError ( valueOrFactory ) ; } return undefined as any ; }