@@ -97,3 +97,50 @@ process.on("exit", () => {
9797 rmSync ( dir , { recursive : true , force : true } ) ;
9898 }
9999} ) ;
100+
101+ /**
102+ * Runs a callback with a fake Sentry server running on an auto-allocated port.
103+ * The server returns 503 errors for all requests.
104+ * Automatically starts and stops the server.
105+ * The allocated port is passed to the callback.
106+ */
107+ export async function withFakeSentryServer (
108+ callback : ( port : string ) => void | Promise < void >
109+ ) : Promise < void > {
110+ const { createServer } = await import ( "node:http" ) ;
111+
112+ const server = createServer ( ( req , res ) => {
113+ if ( DEBUG ) {
114+ // eslint-disable-next-line no-console
115+ console . log ( "[FAKE SENTRY] incoming request" , req . url ) ;
116+ }
117+ res . statusCode = 503 ;
118+ res . end ( "Error: Sentry unreachable" ) ;
119+ } ) ;
120+
121+ // Listen on port 0 to get an auto-allocated port
122+ await new Promise < void > ( ( resolve ) => {
123+ server . listen ( 0 , ( ) => {
124+ resolve ( ) ;
125+ } ) ;
126+ } ) ;
127+
128+ const address = server . address ( ) ;
129+ if ( ! address || typeof address === "string" ) {
130+ throw new Error ( "Failed to get server port" ) ;
131+ }
132+ const port = address . port . toString ( ) ;
133+
134+ if ( DEBUG ) {
135+ // eslint-disable-next-line no-console
136+ console . log ( `[FAKE SENTRY] running on http://localhost:${ port } /` ) ;
137+ }
138+
139+ try {
140+ await callback ( port ) ;
141+ } finally {
142+ await new Promise < void > ( ( resolve ) => {
143+ server . close ( ( ) => resolve ( ) ) ;
144+ } ) ;
145+ }
146+ }
0 commit comments