From fc60dfd04279745508bf6f5cf5890acb2590a05b Mon Sep 17 00:00:00 2001 From: roma <84147465+xretic@users.noreply.github.com> Date: Mon, 9 Feb 2026 03:07:58 +0100 Subject: [PATCH] refactor adapter tests for improved structure reduced test duplication by extracting a small helper to strip connection from configs --- app/__test__/adapter/adapter.test.ts | 104 +++++++++++++++------------ 1 file changed, 57 insertions(+), 47 deletions(-) diff --git a/app/__test__/adapter/adapter.test.ts b/app/__test__/adapter/adapter.test.ts index 8527b5d..8e8eb99 100644 --- a/app/__test__/adapter/adapter.test.ts +++ b/app/__test__/adapter/adapter.test.ts @@ -1,66 +1,76 @@ import { expect, describe, it, beforeAll, afterAll, mock } from "bun:test"; import * as adapter from "adapter"; -import { disableConsoleLog, enableConsoleLog } from "core/utils"; +import { disableConsoleLog, enableConsoleLog, omitKeys } from "core/utils"; import { adapterTestSuite } from "adapter/adapter-test-suite"; import { bunTestRunner } from "adapter/bun/test"; -import { omitKeys } from "core/utils"; + +const stripConnection = >(cfg: T) => + omitKeys(cfg, ["connection"]); beforeAll(disableConsoleLog); afterAll(enableConsoleLog); describe("adapter", () => { - it("makes config", async () => { - expect(omitKeys(await adapter.makeConfig({}), ["connection"])).toEqual({}); - expect( - omitKeys(await adapter.makeConfig({}, { env: { TEST: "test" } }), ["connection"]), - ).toEqual({}); + describe("makeConfig", () => { + it("returns empty config for empty inputs", async () => { + const cases: Array> = [ + [{}], + [{}, { env: { TEST: "test" } }], + ]; - // merges everything returned from `app` with the config - expect( - omitKeys( - await adapter.makeConfig( - { app: (a) => ({ config: { server: { cors: { origin: a.env.TEST } } } }) }, - { env: { TEST: "test" } }, - ), - ["connection"], - ), - ).toEqual({ - config: { server: { cors: { origin: "test" } } }, - }); - }); + for (const args of cases) { + const cfg = await adapter.makeConfig(...(args as any)); + expect(stripConnection(cfg)).toEqual({}); + } + }); - it("allows all properties in app function", async () => { - const called = mock(() => null); - const config = await adapter.makeConfig( - { - app: (env) => ({ - connection: { url: "test" }, - config: { server: { cors: { origin: "test" } } }, - options: { - mode: "db", - }, - onBuilt: () => { - called(); - expect(env).toEqual({ foo: "bar" }); - }, - }), - }, - { foo: "bar" }, + it("merges app output into config", async () => { + const cfg = await adapter.makeConfig( + { app: (a) => ({ config: { server: { cors: { origin: a.env.TEST } } } }) }, + { env: { TEST: "test" } }, ); - expect(config.connection).toEqual({ url: "test" }); - expect(config.config).toEqual({ server: { cors: { origin: "test" } } }); - expect(config.options).toEqual({ mode: "db" }); - await config.onBuilt?.(null as any); - expect(called).toHaveBeenCalled(); - }); - adapterTestSuite(bunTestRunner, { + expect(stripConnection(cfg)).toEqual({ + config: { server: { cors: { origin: "test" } } }, + }); + }); + + it("allows all properties in app() result", async () => { + const called = mock(() => null); + + const cfg = await adapter.makeConfig( + { + app: (env) => ({ + connection: { url: "test" }, + config: { server: { cors: { origin: "test" } } }, + options: { mode: "db" as const }, + onBuilt: () => { + called(); + expect(env).toEqual({ foo: "bar" }); + }, + }), + }, + { foo: "bar" }, + ); + + expect(cfg.connection).toEqual({ url: "test" }); + expect(cfg.config).toEqual({ server: { cors: { origin: "test" } } }); + expect(cfg.options).toEqual({ mode: "db" }); + + await cfg.onBuilt?.({} as any); + expect(called).toHaveBeenCalledTimes(1); + }); + }); + + describe("adapter test suites", () => { + adapterTestSuite(bunTestRunner, { makeApp: adapter.createFrameworkApp, label: "framework app", - }); + }); - adapterTestSuite(bunTestRunner, { + adapterTestSuite(bunTestRunner, { makeApp: adapter.createRuntimeApp, label: "runtime app", - }); + }); + }); });