Testing
Run your app's tests with Node's test runner.
Node's built-in test runner runs your tests, so there is nothing to install. A test boots your API and sends it real requests.
Given a route api/hello.get.ts that returns { message: 'Hello' }, this file tests it:
import type { AddressInfo } from 'node:net';
import { deepStrictEqual } from 'node:assert';
import { after, before, test } from 'node:test';
import { serveAPI, useEnv, useShutdown } from 'ohnejs';
let port: number;
before(async () => {
useEnv().set('SILENT', true);
useEnv().set('SKIP_CODEGEN', true);
useEnv().set('DATABASE', ':memory:');
useEnv().set('PORT', 0);
const { server } = await serveAPI();
port = (server.address() as AddressInfo).port;
});
after(() => useShutdown().run());
test('GET /hello greets', async () => {
const response = await fetch(`http://127.0.0.1:${port}/hello`);
deepStrictEqual(await response.json(), { message: 'Hello' });
});Run it with npm test.
The test script
The scaffold's test script prepares the project, then runs every test file:
ohne prepare && node --test --import ohnejs/register "tests/**/*.test.ts"ohne preparefirst brings.ohne/up to date, so the tests see your current routes and collections.--import ohnejs/registerlets Node run the.tsfiles ohne ships insidenode_modules. Without it, every test that importsohnejsfails withERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING.
Booting the API
serveAPI boots the app the way ohne serve api does. Before it, the test sets the environment it needs:
SILENTkeeps the test output clean.SKIP_CODEGENleaves.ohne/alone. The script already generated it, and each test file runs in its own process, so the files would otherwise all rewrite it at once.DATABASE=':memory:'gives each file a fresh database.PORT=0picks a free port, which you read fromserver.address().useShutdown().run()inaftercloses the server and the database.
After the boot, query works in a test, to seed records or assert on them.
Dashboard modules need a browser, so keep the dashboard logic you want to test in plain functions and test those.