Skip to Content

invokeExpressSynthetic

function invokeExpressSynthetic( app: Express, opts: { method: HttpMethod path: string headers?: Record<string, string> body?: unknown }, ): Promise<{ statusCode: number headers: Record<string, string> bodyText: string bodyJson: unknown }>

Behavior

  • Binds an ephemeral HTTP server on 127.0.0.1 with a random port.
  • Issues a fetch with the given method, path (leading / optional), optional headers, and JSON body for mutating methods (sets content-type: application/json when body is set and not overridden).
  • Parses JSON response bodies when possible; otherwise returns raw text in bodyJson as a string.
  • Always closes the server in a finally block.

When to use

  • Integration tests that need real req / res / middleware behavior without deploying.
  • Tooling that drives an Express app the same way external HTTP would.

See also