Skip to content

Commit

Permalink
feat: add test cases for health check and todo app
Browse files Browse the repository at this point in the history
  • Loading branch information
shrey-dadhaniya committed Feb 2, 2024
1 parent 6889dd2 commit 94359c7
Show file tree
Hide file tree
Showing 8 changed files with 1,159 additions and 987 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ logs

/tmp
/.vscode
/.idea

# ignore the image files uploaded in the public/images folder
/public/images/*
Expand Down
139 changes: 139 additions & 0 deletions e2e/apps/todos.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { test, expect, request } from "@playwright/test";
import { getApiContext } from "../common.js";
let apiContext;

let todoId = null;

test.beforeAll(async ({ playwright }) => {
apiContext = await getApiContext(playwright);
});

test.afterAll(async ({}) => {
await apiContext.dispose();
});

test.describe("GET:/api/v1/todos - Get All Todos", () => {
test("should return all todos", async () => {
const res = await apiContext.get(`/api/v1/todos`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.data.length).toEqual(0);
});
});

test.describe("POST:/api/v1/todos - Create Todo", () => {
test("should create todo with valid data", async () => {
const todo = {
title: "test-todo-title",
description: "test-todo-description",
};
const res = await apiContext.post(`/api/v1/todos`, {
data: todo,
});
const json = await res.json();
expect(res.status()).toEqual(201);
expect(json.statusCode).toEqual(201);
expect(json.data).toMatchObject(todo);
todoId = json.data._id;
});

test("should return a 422 with title error when `title` is not provided", async () => {
const todo = {};
const res = await apiContext.post(`/api/v1/todos`, {
data: todo,
});
const json = await res.json();
expect(res.status()).toEqual(422);
expect(json.statusCode).toEqual(422);
expect(json.errors).toContainEqual(
expect.objectContaining({ title: expect.anything() })
);
});

test("should return a 422 with title and description error when `title` and `description` is empty", async () => {
const todo = { title: "", description: "" };
const res = await apiContext.post(`/api/v1/todos`, {
data: todo,
});
const json = await res.json();
expect(res.status()).toEqual(422);
expect(json.statusCode).toEqual(422);
expect(json.errors).toContainEqual(
expect.objectContaining({ title: expect.anything() })
);
expect(json.errors).toContainEqual(
expect.objectContaining({ description: expect.anything() })
);
});
});

test.describe("PATCH:/api/v1/todos/:id - Update Todo", () => {
const todo = {
title: "update-test-todo-title",
description: "update-test-todo-description",
};

test("should update todo with valid data", async () => {
const res = await apiContext.patch(`/api/v1/todos/${todoId}`, {
data: todo,
});
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(200);
expect(json.data).toMatchObject(todo);
});

test("should return a 422 with title and description error when `title` and `description` is empty", async () => {
const todo = { title: "", description: "" };
const res = await apiContext.patch(`/api/v1/todos/${todoId}`, {
data: todo,
});
const json = await res.json();
expect(res.status()).toEqual(422);
expect(json.statusCode).toEqual(422);
expect(json.errors).toContainEqual(
expect.objectContaining({ title: expect.anything() })
);
expect(json.errors).toContainEqual(
expect.objectContaining({ description: expect.anything() })
);
});
});

test.describe("PATCH:/api/v1/todos/toggle/status/:id - Toggle todo status", () => {
test("should toggle todo status", async () => {
const res = await apiContext.patch(`/api/v1/todos/toggle/status/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
expect(json.data.isComplete).toBeTruthy();
});
});

test.describe("GET:/api/v1/todos - Get All Todos", () => {
test("should return todo when valid id passed", async () => {
const res = await apiContext.get(`/api/v1/todos/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
});

test("should return 422 with todoId error when invalid id passed", async () => {
const res = await apiContext.get(`/api/v1/todos/__1`);
const json = await res.json();
expect(res.status()).toEqual(422);
expect(json.statusCode).toEqual(res.status());
expect(json.errors).toContainEqual(
expect.objectContaining({ todoId: expect.anything() })
);
});
});

test.describe("DELETE:/api/v1/todos/:id - Delete Todo", () => {
test("should delete todo", async () => {
const res = await apiContext.delete(`/api/v1/todos/${todoId}`);
const json = await res.json();
expect(res.status()).toEqual(200);
expect(json.statusCode).toEqual(res.status());
});
});
12 changes: 12 additions & 0 deletions e2e/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dotenv from "dotenv";

dotenv.config({
path: "../.env",
});

export const URL = `http://localhost:${process.env.PORT || 8080}`;

export const getApiContext = async (playwright) =>
playwright.request.newContext({
baseURL: URL,
});
17 changes: 17 additions & 0 deletions e2e/healthcheck.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test, expect } from "@playwright/test";
import { getApiContext, URL } from "./common.js";

let apiContext;

test.beforeAll(async ({ playwright }) => {
apiContext = await getApiContext(playwright);
});

test.afterAll(async ({}) => {
await apiContext.dispose();
});

test("should return ok", async ({ page }) => {
const res = await apiContext.get(`/api/v1/healthcheck`);
expect(res.ok()).toBeTruthy();
});
62 changes: 62 additions & 0 deletions e2e/test-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import dotenv from "dotenv";
import { httpServer } from "../src/app.js";
import mongoose from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";

dotenv.config({
path: "../.env",
});

let mongoServer = null;
let dbInstance = undefined;
const PORT = process.env.PORT || 8080;

const connectDB = async () => {
try {
await mongoose.disconnect();
mongoServer = await MongoMemoryServer.create();
dbInstance = await mongoose.connect(`${mongoServer.getUri()}`);
await clearDB();
} catch (error) {
console.log("MongoDB connection error: ", error);
process.exit(1);
}
};
export const clearDB = async () => {
const collections = mongoose.connection.collections;

for (const key in collections) {
const collection = collections[key];
await collection.deleteMany({});
}
};

/**
* Starting from Node.js v14 top-level await is available and it is only available in ES modules.
* This means you can not use it with common js modules or Node version < 14.
*/
const majorNodeVersion = +process.env.NODE_VERSION?.split(".")[0] || 0;

const startServer = () => {
httpServer.listen(PORT, () => {
console.info(`📑 Visit the documentation at: http://localhost:${PORT}`);
console.log("⚙️ Server is running on port: " + PORT);
});
};

if (majorNodeVersion >= 14) {
try {
await connectDB();
startServer();
} catch (err) {
console.log("Mongo db connect error: ", err);
}
} else {
connectDB()
.then(() => {
startServer();
})
.catch((err) => {
console.log("Mongo db connect error: ", err);
});
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"start": "nodemon -r dotenv/config --experimental-json-modules src/index.js",
"pre-commit": "lint-staged",
"prepare": "node prepare.js",
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest"
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
"start:test-server": "node -r dotenv/config --experimental-json-modules e2e/test-server.js",
"test:playwright": "set NODE_OPTIONS=--experimental-vm-modules && npx playwright test"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,6 +43,7 @@
"express-validator": "^7.0.1",
"jsonwebtoken": "^9.0.0",
"mailgen": "^2.0.27",
"mongodb-memory-server": "^9.1.6",
"mongoose": "^7.4.0",
"mongoose-aggregate-paginate-v2": "^1.0.6",
"multer": "^1.4.5-lts.1",
Expand Down
86 changes: 43 additions & 43 deletions playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { defineConfig, devices } from "@playwright/test";
export default defineConfig({
testDir: "./e2e",
/* Run tests in files in parallel */
fullyParallel: true,
// fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
Expand All @@ -25,54 +25,54 @@ export default defineConfig({
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',
// baseURL: `http://127.0.0.1:${process.env.PORT || 8080}`,

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
},

/* Configure projects for major browsers */
projects: [
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},

{
name: "firefox",
use: { ...devices["Desktop Firefox"] },
},

{
name: "webkit",
use: { ...devices["Desktop Safari"] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
// projects: [
// {
// name: "chromium",
// use: { ...devices["Desktop Chrome"] },
// },
//
// {
// name: "firefox",
// use: { ...devices["Desktop Firefox"] },
// },
//
// {
// name: "webkit",
// use: { ...devices["Desktop Safari"] },
// },
//
// /* Test against mobile viewports. */
// // {
// // name: 'Mobile Chrome',
// // use: { ...devices['Pixel 5'] },
// // },
// // {
// // name: 'Mobile Safari',
// // use: { ...devices['iPhone 12'] },
// // },
//
// /* Test against branded browsers. */
// // {
// // name: 'Microsoft Edge',
// // use: { ...devices['Desktop Edge'], channel: 'msedge' },
// // },
// // {
// // name: 'Google Chrome',
// // use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// // },
// ],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
webServer: {
command: "npm run start:test-server",
url: `http://localhost:${process.env.PORT || 8080}`,
reuseExistingServer: !process.env.CI,
},
});
Loading

0 comments on commit 94359c7

Please sign in to comment.