This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test setup for dev-server-api (#67)
* add test suite * add workflow for testing server
- Loading branch information
Showing
8 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ dist | |
*.__tmp_entrypoint.tsx | ||
*.zip | ||
|
||
.vscode | ||
.vscode | ||
.aider* |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
}) |