@setu-ts/testing — exercise applications and middleware
Add it: setu add testing adds a dev dependency; it registers no plugin and provides no
capability.
Test the scaffold’s real composition
Pass the unstarted application from your config to this test helper:
import { createTestApp } from '@setu-ts/testing';
import type { IKernelApplication } from '@setu-ts/testing';
export async function checkGreeting(app: IKernelApplication): Promise<void> {
const testApp = await createTestApp({ app });
try {
const response = await testApp.inject({ method: 'GET', url: '/greeting' });
if (response.statusCode !== 200) throw new Error(response.body ?? 'Request failed');
} finally {
await testApp.stop();
}
}
Test a middleware without starting an application
import type { MiddlewareFunction } from '@setu-ts/common';
import { createTestContext } from '@setu-ts/testing';
export async function checkMiddleware(middleware: MiddlewareFunction): Promise<void> {
const ctx = createTestContext({ request: { method: 'GET', path: '/private' } });
let continued = false;
await middleware(ctx, async () => { continued = true; });
if (continued) throw new Error('Expected middleware to refuse');
if (ctx.response.snapshot().status !== 401) throw new Error('Expected 401');
}
Behaviour the types do not show
autoStartdefaults to true and starts without binding a socket. The plugins arm must include a runtime provider; no plugins are installed implicitly.createTestAppreturnsIKernelApplication;injectresolvesstatusCode,headers,body(string | null) andjson<T>().stop()stops plugin workers.- Use
autoStart: falseto add global middleware or more plugins before starting. withoutchecks every exact plugin name before removing anything; an unknown name throws.overrideCapabilitycannot undo the real provider’s registration side effects, and refuses missing or multi-provider capabilities during initialization.createMockPluginprovides a missing capability; alongside a real provider it causes a duplicate-provider startup error.
Other options
createTestApp({ plugins })builds an isolated composition instead of adoptingapp.overrideProviderreplaces a DI provider;MockServiceRegistryandMockResponseare doubles.injectaccepts a URL, request options or web request;collectStreamreads streaming bodies.