function createTestApp
Since 0.1.0
createTestApp(options?: TestAppOptions): Promise<IKernelApplication>

Creates a started test application that can be exercised via inject() and fetch() without binding a socket.

Two arms. Pass plugins to assemble one by hand, or pass app to build from the project's own composition root and subtract from it — the latter is what keeps a test app's composition honest, since anything the root registers (error handling included) is present in the test too.

Examples

Example 1

// Hand-assembled: unit scope.
import { createTestApp } from '@setu-ts/testing';
import { RuntimePlugin } from '@setu-ts/runtime';

const app = await createTestApp({ plugins: [RuntimePlugin()] });

app.router.get('/users', (ctx) => ctx.response.json([{ id: 1 }]));
const res = await app.inject({ method: 'GET', url: '/users' });
console.log(res.statusCode); // 200

Example 2

// Composition root: the real app, minus the database, plus a fake mailer.
import { createTestApp, overrideCapability } from '@setu-ts/testing';
import { CAPABILITIES } from '@setu-ts/common';
import { createApp } from '../setu.config.ts';

const app = await createTestApp({
  app: createApp(),
  without: ['database'],
  overrides: [overrideCapability(CAPABILITIES.MAIL, fakeMailer)],
});

Example 3

// Adding global middleware requires autoStart: false:
const app = await createTestApp({
  plugins: [RuntimePlugin()],
  autoStart: false,
});

app.middleware.add(async (ctx, next) => { /* ... *\/ await next(); });
await app.start();

Parameters

optional
options: TestAppOptions

Test application options

Return Type

A started (or un-started) kernel application

Throws

Error

If without names a plugin the supplied app does not hold

Usage

import { createTestApp } from "testing/src/index.ts";