From ea4cf1d05ef8a9c2936ac05287abeccd2cc4c70e Mon Sep 17 00:00:00 2001 From: NecroBread Date: Mon, 8 Jan 2024 17:41:08 +0200 Subject: [PATCH] add example integration test --- .env.example | 3 ++- .env.test | 12 ++++++++++++ src/config/db.ts | 8 +++++--- src/config/get-config.ts | 8 +------- src/index.ts | 6 +++++- tests/setup-tests.ts | 3 +++ tests/sign-up.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 .env.test create mode 100644 tests/setup-tests.ts create mode 100644 tests/sign-up.test.ts diff --git a/.env.example b/.env.example index 10806e4..bdf014c 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,5 @@ DB_NAME="express_app" DB_HOST=host.docker.internal DB_PORT=6543 DB_USERNAME=postgres -DB_PASSWORD=123123 \ No newline at end of file +DB_PASSWORD=123123 +DB_LOGGING="" \ No newline at end of file diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..e4477e9 --- /dev/null +++ b/.env.test @@ -0,0 +1,12 @@ +NODE_ENV=development +PORT=5000 +ALLOW_ORIGIN="*" +ALLOW_METHODS="GET,PUT,POST,PATCH,DELETE,HEAD,OPTIONS" +ALLOW_HEADERS="Content-Type,Cache-Control,Expires" +JWT_SECRET=SUPERSECRET +DB_NAME="test_express_app" +DB_HOST=localhost +DB_PORT=5432 +DB_USERNAME=postgres +DB_PASSWORD=password +DB_LOGGING="" \ No newline at end of file diff --git a/src/config/db.ts b/src/config/db.ts index 2947df2..3659341 100644 --- a/src/config/db.ts +++ b/src/config/db.ts @@ -1,4 +1,4 @@ -import { DataSource } from "typeorm"; +import { DataSource, DataSourceOptions } from "typeorm"; import "reflect-metadata"; import { Article } from "../features/article/article-entity"; import { Comment } from "../features/comment/comment-entity"; @@ -7,7 +7,7 @@ import { getConfig } from "./get-config"; const config = getConfig(); -export const AppDataSource = new DataSource({ +export const getDbConfig = (): DataSourceOptions => ({ type: "postgres", host: config.DB_HOST, port: parseInt(config.DB_PORT), @@ -15,6 +15,8 @@ export const AppDataSource = new DataSource({ password: config.DB_PASSWORD, database: config.DB_NAME, synchronize: config.NODE_ENV === "production" ? false : true, - logging: true, + logging: !!config.LOGGING, entities: [Article, Comment, User], }); + +export const AppDataSource = new DataSource(getDbConfig()); diff --git a/src/config/get-config.ts b/src/config/get-config.ts index 53bdf1c..dcf6313 100644 --- a/src/config/get-config.ts +++ b/src/config/get-config.ts @@ -1,10 +1,3 @@ -import dotenv from "dotenv"; - -/** - * Load enviroment variables - */ -dotenv.config(); - export function getConfig() { return { NODE_ENV: process.env.NODE_ENV, @@ -18,5 +11,6 @@ export function getConfig() { DB_PORT: process.env.DB_PORT, DB_PASSWORD: process.env.DB_PASSWORD, DB_USERNAME: process.env.DB_USERNAME, + LOGGING: process.env.DB_LOGGING, }; } diff --git a/src/index.ts b/src/index.ts index 7ad751e..c4b9aae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,11 @@ import app from "./app"; import { AppDataSource } from "./config/db"; import { getConfig } from "./config/get-config"; import { AppError } from "./utils/app-error"; - +import dotenv from "dotenv"; +/** + * Load enviroment variables + */ +dotenv.config(); const config = getConfig(); const port: number = config.PORT ? parseInt(config.PORT) : 5000; diff --git a/tests/setup-tests.ts b/tests/setup-tests.ts new file mode 100644 index 0000000..8eb996b --- /dev/null +++ b/tests/setup-tests.ts @@ -0,0 +1,3 @@ +import * as dotenv from "dotenv"; + +dotenv.config({ path: `.env.test`, override: true }); diff --git a/tests/sign-up.test.ts b/tests/sign-up.test.ts new file mode 100644 index 0000000..e6662a0 --- /dev/null +++ b/tests/sign-up.test.ts @@ -0,0 +1,42 @@ +import supertest, { SuperTest, Test } from "supertest"; +import { DataSource } from "typeorm"; +import app from "../src/app"; +import { AppDataSource, getDbConfig } from "../src/config/db"; +import { createDatabase } from "typeorm-extension"; + +const url = "/api/v1/user/signup"; +let db: DataSource; + +describe(`${url}`, () => { + let agent: SuperTest; + + beforeAll(async () => { + await createDatabase({ options: getDbConfig(), ifNotExist: true }); + db = await AppDataSource.initialize(); + agent = supertest(app); + }); + + afterAll((done) => { + db.dropDatabase().then(() => { + db.destroy(); + done(); + }); + }); + + test("should sign up successfully when user data is valid", async () => { + const test = await agent + .post(url) + .set("Content-Type", "application/json") + .send({ + username: `john1`, + firstName: "John", + lastName: "Johnson", + password: "123123", + }) + .expect(201); + + expect(test.body.message).toBe("Sign up successful"); + expect(test.body.data).toBeTruthy(); + expect(test.body.code).toBe(201); + }); +});