Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Add test setup for dev-server-api (#67)
Browse files Browse the repository at this point in the history
* add test suite

* add workflow for testing server
  • Loading branch information
seveibar authored Jul 6, 2024
1 parent 88aa2b7 commit a157e51
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 2 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/server-tests.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Bun Test
name: CLI Tests

on:
push:
branches:
- main
pull_request:
branches:
- main
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dist
*.__tmp_entrypoint.tsx
*.zip

.vscode
.vscode
.aider*
Binary file modified dev-server-api/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions dev-server-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
29 changes: 29 additions & 0 deletions dev-server-api/tests/fixtures/get-test-server.ts
Original file line number Diff line number Diff line change
@@ -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<TestFixture> => {
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,
}
}
20 changes: 20 additions & 0 deletions dev-server-api/tests/fixtures/start-server.ts
Original file line number Diff line number Diff line change
@@ -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,
})
10 changes: 10 additions & 0 deletions dev-server-api/tests/routes/health.test.ts
Original file line number Diff line number Diff line change
@@ -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,
})
})

0 comments on commit a157e51

Please sign in to comment.