Skip to content

@ogygia/core

Plugin tests

@ogygia/core/plugin/testing runs a plugin's server half in a unit test, with no server and no build: startPlugin makes its app and runs setup, and gives request contexts to call its hooks with.

On this page

A server half is plain functions over a context, so most of it can be tested without starting an app. startPlugin from @ogygia/core/plugin/testing does what ogygia.handle() does at boot: it makes the plugin’s app, registers its shared hooks and runs setup. Then it hands you request contexts to call the hooks with.

@ogygia/core/plugin, which your server half imports for defineServer, also loads Svelte components when it’s imported, for its region values. So the runner has to compile .svelte files: run the tests in Vitest with svelte() from @sveltejs/vite-plugin-svelte in its config. Plain Node, or a runner without it, fails on the import with an unknown .svelte extension or a parse error (#2314, item 11).

import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  plugins: [svelte()],
  test: { environment: 'node' }
});
import { expect, test } from 'vitest';
import { startPlugin } from '@ogygia/core/plugin/testing';
import server from '../src/server.ts';

test('the response carries the header', async () => {
  const { ctx } = startPlugin(server, {
    name: 'ogygia-plugin-stamp',
    options: { header: 'x-stamp' }
  });
  const res = new Response('<p>hi</p>');
  await server.response?.(
    ctx(new Request('http://localhost/')),
    res
  );
  expect(res.headers.has('x-stamp')).toBe(true);
});

startPlugin

startPlugin(server, { name, options }) takes the server half and returns { app, ctx }. name defaults to test-plugin, and options is what app.options and ctx.options hold.

ctx(request, event?) makes a request context for a Request: path from its URL, its own facts, and a fresh ctx.state from your state(). The event is a bare one, with request, url, route.id set to null, cookies that read nothing and empty locals. Pass event to set any of those, such as cookies that answer or a platform. ctx.renderHole() answers null.

The app is real where it matters for a test:

  • sign and verify use a real HMAC, with a key made once per test process.
  • capability.mint gives the default server island lifetime, ttl an hour, and capability.open renews an expired one up to the default maxPageAge of eight days. Opening charges no budget.
  • deny and a refused capability answer the handle’s own 403, with x-ogygia-deny.
  • notify and ask reach the started plugin’s shared hooks, and any other plugin started in the same process.

inRequest

inRequest(event, fn) runs fn inside a request, the way Kit runs a hook or an endpoint, so code that finds its request through Kit’s request store finds event through every await. Use it for app.keepAlive and app.current():

import { expect, test } from 'vitest';
import {
  inRequest,
  startPlugin
} from '@ogygia/core/plugin/testing';
import server from '../src/server.ts';

test('the send outlives the response', async () => {
  const kept: Promise<unknown>[] = [];
  const { app } = startPlugin(server);
  const platform = {
    context: {
      waitUntil: (work: Promise<unknown>) =>
        void kept.push(work)
    }
  };
  inRequest({ platform }, () =>
    app.keepAlive(Promise.resolve())
  );
  expect(kept).toHaveLength(1);
});

Route config

Three helpers stand in for what the build and a router would register, for a hook that reads ctx.routeConfig(key):

  • setPageRouteConfig({ '/account': { stamp: false } }) sets the Kit pages’ merged config, by route id with groups removed, as the build would.
  • routeConfig(key, routeId, pathname) reads a value the way ctx.routeConfig does.
  • clearRouteConfig() forgets every registered router and page, between tests.

A context made with ctx() has route.id set to null, which matches no page, so pass { route: { id: '/account' } } as its event to read a page’s config.