Testing@rhythmjs/testing
API reference
Every export of the four subpaths: @rhythmjs/testing/rhythm, /router, /cli, and /ws. There is no root barrel export.
On this page
@rhythmjs/testing/rhythm#
function mockModule<TExports extends object>(
values: TExports,
dispose?: (values: TExports) => void | Promise<void>,
): Rhythm<{}, TExports, TExports>;
function runMiddleware<TContext extends object>(
middleware: Middleware<TContext>,
ctx: TContext,
): Promise<MiddlewareRun<TContext>>;mockModule(values, dispose?)#- A Rhythm module (type
module, namemock) providingvaluesverbatim.dispose, when given, runs on the host app's teardown with those same values. runMiddleware(middleware, ctx)#- Invokes the middleware with your context and a
next()that records being called and resolves to the context. MiddlewareRun<TContext>#- { ctx: TContext; nextCalled: boolean }: the mutated context and whether the chain continued.
@rhythmjs/testing/router#
function createTestClient(
app: Rhythm<RhythmHttpContext, …> | RhythmRouter,
options?: TestClientOptions,
): TestClient;
function runHttpMiddleware<TExtras extends object = {}>(
middleware: Middleware<RhythmHttpContext & TExtras>,
request?: string | Request,
extras?: TExtras,
): Promise<HttpMiddlewareRun<RhythmHttpContext & TExtras>>;createTestClient(app, options?)#- Accepts a Rhythm HTTP app or a bare
RhythmRouter(mounted into a fresh app for you) and returns verb helpers overtoFetchHandler.options.baseUrlresolves relative paths (defaulthttp://localhost). TestClient#fetch(input, init?)plusget/head/post/put/patch/delete, each(path, options?) => Promise<Response>, andteardown()which disposes the app's providers.TestRequestOptions#- { headers?; body?; json? };
jsonserializes the value and setscontent-type: application/jsonunless you set one yourself.TestRequestHeaders,TestRequestBody, andTestRequestInitalias the corresponding fetch constructor parameter types. runHttpMiddleware(middleware, request?, extras?)#- Builds a real HTTP context with
createHttpContextfrom a path orRequest(defaulthttp://localhost/), assignsextrasonto it, runs the middleware, and returnsHttpMiddlewareRun. HttpMiddlewareRun#- { ctx; nextCalled: boolean; response: Response } -
responseis the context's response rendered withtoResponse.
@rhythmjs/testing/cli#
function createCliRunner(
app: Rhythm<RhythmCliContext, …> | RhythmCli,
): CliRunner;createCliRunner(app)#- Accepts a Rhythm CLI app or a bare
RhythmCli(mounted for you). The runner parses flags exactly like the real entry point, viaparseArgv. CliRunner.run(argv, options?)#argvis a command string (whitespace-split) or an argv array.options.stdinbecomes a webReadableStreamon the context; omitted,ctx.stdinis null, matching a TTY. Resolves toCliRunResult.CliRunResult#- { ctx; stdout: string[]; stderr: string[]; exitCode: number } - copies of the response buffers, nothing written to real stdio.
CliRunner.teardown()#- Disposes the underlying app's providers.
@rhythmjs/testing/ws#
function upgradeWs<Data extends object = Record<string, string>>(
ws: RhythmWs,
request: string | Request,
): Promise<WsUpgradeResult<Data>>;
function mockWs<Data extends object = Record<string, string>>(data: Data): MockWs<Data>;
function fireOpen(ws: RhythmWs, peer: MockWs): void | Promise<void>;
function fireMessage(ws: RhythmWs, peer: MockWs, data: string | Uint8Array | object): void | Promise<void>;
function fireClose(ws: RhythmWs, peer: MockWs, code = 1000, reason = ""): void | Promise<void>;
function fireDrain(ws: RhythmWs, peer: MockWs): void | Promise<void>;upgradeWs(ws, request)#- Drives
RhythmWs.upgradeagainst a mock server whoseupgrade()always accepts and records its options. A string becomes a request with theupgrade: websocketheader implied. Guards, the route'supgrade, andheadersall run for real. WsUpgradeResult<Data>#- { matched; upgraded; response; data; headers } -
matchedis false whenupgrade()returned null (not a websocket request, or no route);responsecarries a rejection; on successdatais the exactws.dataobject andheaderswhatever the route contributed to the 101. mockWs(data)#- A recording
ServerWebSocketstand-in:sent,published,topics,closed, andterminatedcapture everything handlers do;send/publishreturn byte counts;close/terminateflipreadyStateto 3.data,readyState,remoteAddress(127.0.0.1), andisSubscribedround out the surface handlers touch. fireOpen / fireMessage / fireClose / fireDrain#- Dispatch through the instance's real
websocketbehavior, so routing byws.dataidentity is exercised too.fireMessagepasses strings through, wrapsUint8Arrayin aBuffer, and JSON-serializes any other object.