Skip to content

Commit

Permalink
Basic test setup seems to be working now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 28, 2024
1 parent 79b6638 commit 50231e2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import type { Config } from "jest";

const config: Config = {
verbose: true,
silent: false,
preset: "ts-jest",
testEnvironment: "node",
coveragePathIgnorePatterns: [
"/node_modules/",
"/dist",
]
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"scripts": {
"build": "tsc",
"lint": "node_modules/.bin/eslint src --ext .ts",
"serve": "node dist/main.js",
"serve": "node dist/src/main.js",
"start": "npm run serve",
"test": "jest"
"test": "jest --config jest.config.ts --forceExit --verbose"
},
"dependencies": {
"@effect/schema": "^0.63.2",
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function setupApp(app: Express, db: Sequelize) {
secure: PRODUCTION
}
}));
store.sync();
// store.sync();

app.use(apiKeyMiddleware);

Expand Down
6 changes: 3 additions & 3 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export enum UserType {
}

export interface DBConnectionOptions {
dbName: string;
username: string;
password: string;
dbName?: string;
username?: string;
password?: string;
}

export function getDatabaseConnection(options?: DBConnectionOptions) {
Expand Down
13 changes: 9 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import { getDatabaseConnection } from "./database";
const STORIES_DIR = join(__dirname, "stories");
const MAIN_FILE = "main.js";

const db = getDatabaseConnection();
const app = createApp(db);
// const db = getDatabaseConnection();
// const app = createApp(db);

import { createTestApp, runApp, setupTestDatabase } from "../tests/utils";
const app = createTestApp();
const db = setupTestDatabase();

promises.readdir(STORIES_DIR, { withFileTypes: true }).then(entries => {
entries.forEach(async (entry) => {
if (entry.isDirectory()) {
Expand All @@ -26,7 +31,7 @@ promises.readdir(STORIES_DIR, { withFileTypes: true }).then(entries => {
});

// set port, listen for requests
const PORT = process.env.PORT || 8081;
app.listen(PORT, () => {
const PORT = Number(process.env.PORT) || 8081;
runApp(app, PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
36 changes: 30 additions & 6 deletions tests/root.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import { describe, it } from "@jest/globals";
/* eslint-disable @typescript-eslint/no-floating-promises */

import testApp, { authorizedRequest, runApp } from "./utils";
import { afterAll, beforeAll, describe, it } from "@jest/globals";
import type { Express } from "express";
import type { Sequelize } from "sequelize";
import request from "supertest";

import { authorize, createMySQLConnection, createTestApp, setupTestDatabase } from "./utils";
import { setupApp } from "../src/app";

let testDB: Sequelize;
let testApp: Express;
beforeAll(async () => {
testDB = await setupTestDatabase();
testApp = createTestApp(testDB);
setupApp(testApp, testDB);
}, 100_000);

afterAll(async () => {
const conn = await createMySQLConnection();
await conn.query("DROP DATABASE test;");
});

describe("Test root route", () => {
runApp(testApp);

it("Should show a welcome message + warning", async () => {
authorize(request(testApp).get("/"))
.expect(200)
.expect("Content-Type", /json/)
.expect({ message: "Welcome to the CosmicDS server! You'll need to include a valid API key with your requests in order to access other endpoints." });
});

it("Should show a welcome message", async () => {
void authorizedRequest(testApp)
.get("/")
authorize(request(testApp).get("/"))
.expect(200)
.expect("Content-Type", /json/)
.expect({ message: "Welcome to the CosmicDS server!" });
});
}, 10_000);
});

0 comments on commit 50231e2

Please sign in to comment.