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.1with a random port. - Issues a
fetchwith the givenmethod,path(leading/optional), optionalheaders, and JSONbodyfor mutating methods (setscontent-type: application/jsonwhen body is set and not overridden). - Parses JSON response bodies when possible; otherwise returns raw text in
bodyJsonas a string. - Always closes the server in a
finallyblock.
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
- HTTP bridge — map RPC to synthetic HTTP for QA