import http from "http" ; import { AddressInfo } from "net" ; import { URL } from "url" ; import { ExecutionContext } from "ava" ; import StandardWebSocket , { WebSocketServer } from "ws" ; export async function useServer ( t , listener , webSocketListener ? ) { return new Promise ( ( resolve ) => { const server = http . createServer ( listener ) ; if ( webSocketListener ) { const wss = new WebSocketServer ( { server } ) ; wss . on ( "connection" , webSocketListener ) ; } server . listen ( 0 , ( ) => { t . teardown ( ( ) => server . close ( ) ) ; const port = ( server . address ( ) as AddressInfo ) . port ; resolve ( { http : new URL ( `http://localhost:${ port }` ) , ws : new URL ( `ws://localhost:${ port }` ) , } ) ; } ) ; } ) ; }