-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hardcode bun version for github action * Bun scripting seems to work, start fleshing out compose file for the test postgres instance * Set up of postgres instance for testingi runnning on docker complete with its tear up and tear down * Start working on base server test * Add Github Action for running tests * Ensure to run test script * Fix initial error with tests starting much faster than postgres initialization * Get tests to pass by breaking cyclic dependancy on createApplicationServer * Purge unused dependencies from index.ts * Fix workflows * Setup deliberately failing test * Use exit code on running tests to fail github action on failed tests * Remove failing test as assertions on failed tests correctly failing action
- Loading branch information
1 parent
1be3449
commit 40ee068
Showing
12 changed files
with
102 additions
and
23 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,8 @@ | ||
APPLICATION_PORT=3000 | ||
BASE_URL="http://localhost:3000" | ||
JWT_SECRET="jwt_secret" | ||
POSTGRES_USER="testing_user" | ||
POSTGRES_PASSWORD="testing_password" | ||
POSTGRES_HOST="localhost" | ||
POSTGRES_PORT="5000" | ||
POSTGRES_DATABASE_NAME="testing_db" |
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,20 @@ | ||
name: Run Server Tests | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: oven-sh/setup-bun@v1 | ||
with: | ||
bun-version: 1.0.30 | ||
- name: bun install | ||
run: bun install | ||
- name: bun run test_script | ||
run: bun run test_script.ts |
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,2 @@ | ||
[test] | ||
coverage = true # always enable coverage |
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,9 @@ | ||
services: | ||
postgres: | ||
image: "postgres:14-alpine" | ||
ports: | ||
- "5000:5432" | ||
environment: | ||
- POSTGRES_DB=testing_db | ||
- POSTGRES_USER=testing_user | ||
- POSTGRES_PASSWORD=testing_password |
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
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
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,13 @@ | ||
import { logger } from "."; | ||
import { PostgresDataSourceSingleton } from "./postgres"; | ||
import { createApplicationServer } from "./server"; | ||
|
||
export const startServer = async () => { | ||
const dataSource = await PostgresDataSourceSingleton.getInstance(); | ||
|
||
const app = createApplicationServer(dataSource); | ||
|
||
logger.info( | ||
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port} with NODE_ENV ${process.env.NODE_ENV}`, | ||
); | ||
}; |
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,19 @@ | ||
import "reflect-metadata"; // required for TypeORM | ||
|
||
import { describe, expect, it } from "bun:test"; | ||
|
||
import { createApplicationServer } from "../src/server.ts"; | ||
import { PostgresDataSourceSingleton } from "../src/postgres/index.ts"; | ||
|
||
describe("Server Testing", async () => { | ||
console.log("NODE_ENV: ", process.env.NODE_ENV); | ||
const dataSource = await PostgresDataSourceSingleton.getInstance(); | ||
console.log("Fetched postgres datasource"); | ||
const app = createApplicationServer(dataSource); | ||
it("Correcly loads up the test server", async () => { | ||
const response = await app | ||
.handle(new Request("http://localhost:3000")) | ||
.then((res) => res.status); | ||
expect(response).toBe(200); | ||
}); | ||
}); |
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,22 @@ | ||
import { $ } from "bun"; | ||
import { setTimeout } from "timers/promises"; | ||
|
||
// Use docker compose to set up a postgres database as specified in compose.yaml | ||
await $`docker compose up -d`; | ||
|
||
// We might be running tests much faster than when our postgres container is ready to handle | ||
// them. Set up a 5 second delay before starting testing. | ||
await setTimeout(5000); | ||
|
||
// Run tests | ||
const testResult = await $`bun run test`; | ||
if (testResult.exitCode !== 0) { | ||
console.error( | ||
"An error occured while running bun tests", | ||
testResult.stderr.toString(), | ||
); | ||
throw new Error(testResult.stderr.toString()); | ||
} | ||
|
||
// Destroy the postgres instance and ensure any created volumes are removed as well | ||
await $`docker compose down --volumes`; |