From a157e51a00121e8c306cd57a8cf7458e88321383 Mon Sep 17 00:00:00 2001 From: Severin Ibarluzea Date: Fri, 5 Jul 2024 20:40:14 -0700 Subject: [PATCH] Add test setup for dev-server-api (#67) * add test suite * add workflow for testing server --- .github/workflows/server-tests.yml | 31 ++++++++++++++++++ .github/workflows/test.yml | 5 ++- .gitignore | 3 +- dev-server-api/bun.lockb | Bin 66726 -> 67070 bytes dev-server-api/package.json | 1 + .../tests/fixtures/get-test-server.ts | 29 ++++++++++++++++ dev-server-api/tests/fixtures/start-server.ts | 20 +++++++++++ dev-server-api/tests/routes/health.test.ts | 10 ++++++ 8 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/server-tests.yml create mode 100644 dev-server-api/tests/fixtures/get-test-server.ts create mode 100644 dev-server-api/tests/fixtures/start-server.ts create mode 100644 dev-server-api/tests/routes/health.test.ts diff --git a/.github/workflows/server-tests.yml b/.github/workflows/server-tests.yml new file mode 100644 index 00000000..29c8b1f4 --- /dev/null +++ b/.github/workflows/server-tests.yml @@ -0,0 +1,31 @@ +name: Dev Server API Tests + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup bun + uses: oven-sh/setup-bun@v1 + with: + bun-version: latest + + - name: Install dependencies + run: bun run bootstrap + + - name: Install dependencies + run: bun run build + + - name: Run tests + run: cd dev-server-api && bun test diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f13e67fa..05bf0173 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,9 @@ -name: Bun Test +name: CLI Tests on: + push: + branches: + - main pull_request: branches: - main diff --git a/.gitignore b/.gitignore index f9417b1b..e213d95d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ dist *.__tmp_entrypoint.tsx *.zip -.vscode \ No newline at end of file +.vscode +.aider* diff --git a/dev-server-api/bun.lockb b/dev-server-api/bun.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hu4k~($M;(yDHC|b|#xCtp4HCK5D-mlk zzO^28Sgt{}KsnZV)KRHghw-h$_@LgFG3znD^%&oJk2)b;P`9Bb`aJ5CZ1!P%K8z3Q zw4_&KeAO6VwMU(iPoO@A%H80>LA!PX#f4 zjL&)c@ZmZV;Qg}z%WObCK=T8<$K8yNDZ~ij;lL=wi(q5e#9%1A>=@RJUxwgOXe_|+ zJTWi?AD?)6(dd63U|ByYZVU@)VJoo4W;xV2eK4QHd?vR8Jh#*XA!2KCj*P%nBrp`f z(OioHIC7qLYJoOjzvML~4&IF`0eGs~3G4uBq@gKREt9KF^YHc)(;Tj5NqTc^K^pF* z1AMK(TM%Eij68h@91Dy9MgegE&l^0{yboLjeh#z%mw_t)&r!_xNq}d!?Ev?{NpiV4 z%$JX=`9L1PQ+)=I2}}fB0Mj@IxDGHy9{`*w&J5Q8^Yjdm3*-P#0#5-806xnTfmVRW zu_l1i@hmVM@bGPcFD{D!H^9@yMc_SPCNLFn0@DC4iSGlPatAOP7z->kug8Hom&pLH zd7u0dTZD};4~8L`<<69>;irPAiDEahyL{!gbuEVYSst&yLi$aT9pO!E_ zAvsBd!|>J!HAB){otEx6DQb-v*7H4R>D{%}R(I3W7WKW9grtNde65g%)=2f7TxfNw z#qv3>%S3NmqBcr#n=`8CBUx1JpKlyVets$HB_&K?y$?sr`)w&kl_8?HM=P(ywL7uN zcHj7xtZcWblTr@dEGO{WPwuowM)iD2JLJ4I{lL+?E(A+PC$MS|?6Diw!+JiswSH!s z^W9OGCcwd&kjmB^a{5LfxKnMB!5z-=J>SvVeiP-I9po8g27_TnM0`7Tyn@yB0zb0&OgQS1?1wS$t|8OdZbm$PNVKSjt# z<`fO@l=nKb;M1& diff --git a/dev-server-api/package.json b/dev-server-api/package.json index c515d3a6..f5cb3674 100644 --- a/dev-server-api/package.json +++ b/dev-server-api/package.json @@ -19,6 +19,7 @@ "better-sqlite3": "^11.0.0", "kysely": "^0.27.3", "kysely-bun-sqlite": "^0.3.2", + "redaxios": "^0.5.1", "winterspec": "0.0.81", "zod": "^3.22.4" } diff --git a/dev-server-api/tests/fixtures/get-test-server.ts b/dev-server-api/tests/fixtures/get-test-server.ts new file mode 100644 index 00000000..41870d61 --- /dev/null +++ b/dev-server-api/tests/fixtures/get-test-server.ts @@ -0,0 +1,29 @@ +import { afterEach } from "bun:test" +import defaultAxios from "redaxios" +import { startServer } from "./start-server" + +interface TestFixture { + url: string + server: any + axios: typeof defaultAxios +} + +export const getTestFixture = async (): Promise => { + const port = 3001 + Math.floor(Math.random() * 999) + const server = startServer({ port }) + const url = `http://localhost:${port}` + const axios = defaultAxios.create({ + baseURL: url, + }) + + afterEach(() => { + console.log("closing server") + server.stop() + }) + + return { + url, + server, + axios, + } +} diff --git a/dev-server-api/tests/fixtures/start-server.ts b/dev-server-api/tests/fixtures/start-server.ts new file mode 100644 index 00000000..c3bca20b --- /dev/null +++ b/dev-server-api/tests/fixtures/start-server.ts @@ -0,0 +1,20 @@ +import { createFetchHandlerFromDir } from "winterspec/adapters/node" +import { Request as EdgeRuntimeRequest } from "@edge-runtime/primitives" +import { join } from "node:path" + +const serverFetch = await createFetchHandlerFromDir( + join(import.meta.dir, "../../routes") +) + +export const startServer = ({ port }: { port: number }) => + Bun.serve({ + fetch: (bunReq) => { + const req = new EdgeRuntimeRequest(bunReq.url, { + headers: bunReq.headers, + method: bunReq.method, + body: bunReq.body, + }) + return serverFetch(req as any) + }, + port, + }) diff --git a/dev-server-api/tests/routes/health.test.ts b/dev-server-api/tests/routes/health.test.ts new file mode 100644 index 00000000..10295ba4 --- /dev/null +++ b/dev-server-api/tests/routes/health.test.ts @@ -0,0 +1,10 @@ +import { it, expect } from "bun:test" +import { getTestFixture } from "../fixtures/get-test-server" + +it("GET /health", async () => { + const { axios } = await getTestFixture() + + expect(await axios.get("/health").then((r) => r.data)).toMatchObject({ + ok: true, + }) +})