Skip to content

Commit

Permalink
add example integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
mariovyord committed Jan 8, 2024
1 parent 55037cb commit ea4cf1d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ DB_NAME="express_app"
DB_HOST=host.docker.internal
DB_PORT=6543
DB_USERNAME=postgres
DB_PASSWORD=123123
DB_PASSWORD=123123
DB_LOGGING=""
12 changes: 12 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -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=""
8 changes: 5 additions & 3 deletions src/config/db.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -7,14 +7,16 @@ 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),
username: config.DB_USERNAME,
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());
8 changes: 1 addition & 7 deletions src/config/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import dotenv from "dotenv";

/**
* Load enviroment variables
*/
dotenv.config();

export function getConfig() {
return {
NODE_ENV: process.env.NODE_ENV,
Expand All @@ -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,
};
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions tests/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as dotenv from "dotenv";

dotenv.config({ path: `.env.test`, override: true });
42 changes: 42 additions & 0 deletions tests/sign-up.test.ts
Original file line number Diff line number Diff line change
@@ -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<Test>;

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);
});
});

0 comments on commit ea4cf1d

Please sign in to comment.