Skip to content

Commit

Permalink
Merge pull request #6 from SprintCV-S24/jg-code-coverage
Browse files Browse the repository at this point in the history
Adds tests and adjusts code to achieve code coverage
  • Loading branch information
jackgarritano authored Apr 1, 2024
2 parents 48be06e + 3db63bd commit 4fc0755
Show file tree
Hide file tree
Showing 25 changed files with 632 additions and 222 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist/
*.env
config/serviceAccountKey.json
node_modules
coverage
696 changes: 491 additions & 205 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start": "nodemon src/index.ts",
"build": "tsc",
"test": "vitest run",
"coverage": "vitest run --coverage",
"lint": "eslint --ext .ts . --fix",
"typecheck": "tsc --noEmit"
},
Expand All @@ -28,6 +29,7 @@
"@types/node": "^20.2.5",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"@vitest/coverage-v8": "^1.4.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-standard": "^17.1.0",
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import dotenv from "dotenv";
import cors from "cors";
import morgan from "morgan";
import helmet from "helmet";
import { exampleRoute } from "./routers/exampleRoute";
import { router } from "./routers/root.router";
import { verifyToken } from "./middlewares/verifyToken";
import { notFound, errorHandler } from "./middlewares/errors";
Expand All @@ -26,7 +25,6 @@ app.use(helmet());
* Use the verifyToken to protect all the routes that require authentication
*/
app.use("/api", verifyToken, router);
app.use("/example", verifyToken, exampleRoute);

// Default route: Unprotected
app.get("/", (_req: Request, res: Response) => {
Expand Down
7 changes: 6 additions & 1 deletion src/models/folder.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* v8 ignore start */
//folder has not yet been implemented

//mongoose object for defining structure of all the documents in a mongodb collection
import mongoose from "mongoose";

Expand All @@ -18,4 +21,6 @@ const Folder = new Schema<folderType>({
folderIds: { type: [Schema.Types.ObjectId], required: true, ref: 'FolderModel' },
});

export const FolderModel = mongoose.model("Folder", Folder);
export const FolderModel = mongoose.model("Folder", Folder);

/* v8 ignore stop */
2 changes: 2 additions & 0 deletions src/routers/activities.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createActivity,
Expand Down Expand Up @@ -106,3 +107,4 @@ activitiesRouter.delete(
}
},
);
/* v8 ignore stop */
2 changes: 2 additions & 0 deletions src/routers/education.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createEducation,
Expand Down Expand Up @@ -116,3 +117,4 @@ educationRouter.delete(
}
},
);
/* v8 ignore stop */
10 changes: 0 additions & 10 deletions src/routers/exampleRoute.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/routers/experience.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createExperience,
Expand Down Expand Up @@ -116,3 +117,4 @@ experienceRouter.delete(
}
},
);
/* v8 ignore stop */
2 changes: 2 additions & 0 deletions src/routers/heading.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createHeading,
Expand Down Expand Up @@ -116,3 +117,4 @@ headingRouter.delete(
}
},
);
/* v8 ignore stop */
2 changes: 2 additions & 0 deletions src/routers/project.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createProject,
Expand Down Expand Up @@ -116,3 +117,4 @@ projectRouter.delete(
}
},
);
/* v8 ignore stop */
3 changes: 2 additions & 1 deletion src/routers/resume.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createResume,
Expand Down Expand Up @@ -116,4 +117,4 @@ resumeRouter.delete(
}
},
);

/* v8 ignore stop */
3 changes: 2 additions & 1 deletion src/routers/sectionHeading.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createSectionHeading,
Expand Down Expand Up @@ -116,4 +117,4 @@ sectionHeadingRouter.delete(
}
},
);

/* v8 ignore stop */
3 changes: 2 additions & 1 deletion src/routers/skills.router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* v8 ignore start */
import { Router, type Request, type Response } from "express";
import {
createSkill,
Expand Down Expand Up @@ -116,4 +117,4 @@ skillRouter.delete(
}
},
);

/* v8 ignore stop */
11 changes: 10 additions & 1 deletion src/tests/controllers.tests/activities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ describe("Activities controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createActivity({} as ActivitiesType)).rejects.toThrowError("failed");
await expect(getAllActivities({} as string)).rejects.toThrowError();
await expect(getActivityById({} as string, {} as string)).rejects.toThrowError();
await expect(updateActivity({} as string, {} as string, {} as ActivitiesType)).rejects.toThrowError();
await expect(deleteActivity({} as string, {} as string)).rejects.toThrowError();
await expect(deleteActivity("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves an activity", async () => {
await createActivity(activityDummyData1 as ActivitiesType);
await createActivity(activityDummyData1 as ActivitiesType);
const returnedActivities = await getAllActivities(activityDummyData1.user);

//get back the 1 activity that was added
Expand Down
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/education.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe("Education controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createEducation({} as EducationType)).rejects.toThrowError("failed");
await expect(getAllEducation({} as string)).rejects.toThrowError();
await expect(getEducationById({} as string, {} as string)).rejects.toThrowError();
await expect(updateEducation({} as string, {} as string, {} as EducationType)).rejects.toThrowError();
await expect(deleteEducation({} as string, {} as string)).rejects.toThrowError();
await expect(deleteEducation("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves an education", async () => {
await createEducation(educationDummyData1 as EducationType);
const returnedEducation = await getAllEducation(educationDummyData1.user);
Expand Down
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/experience.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe("Experience controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createExperience({} as ExperienceType)).rejects.toThrowError("failed");
await expect(getAllExperiences({} as string)).rejects.toThrowError();
await expect(getExperienceById({} as string, {} as string)).rejects.toThrowError();
await expect(updateExperience({} as string, {} as string, {} as ExperienceType)).rejects.toThrowError();
await expect(deleteExperience({} as string, {} as string)).rejects.toThrowError();
await expect(deleteExperience("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves an experience", async () => {
await createExperience(experienceDummyData1 as ExperienceType);
const returnedExperience = await getAllExperiences(experienceDummyData1.user);
Expand Down
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/heading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe("Heading controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createHeading({} as HeadingType)).rejects.toThrowError("failed");
await expect(getAllHeadings({} as string)).rejects.toThrowError();
await expect(getHeadingById({} as string, {} as string)).rejects.toThrowError();
await expect(updateHeading({} as string, {} as string, {} as HeadingType)).rejects.toThrowError();
await expect(deleteHeading({} as string, {} as string)).rejects.toThrowError();
await expect(deleteHeading("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves an heading", async () => {
await createHeading(headingDummyData1 as HeadingType);
const returnedHeading = await getAllHeadings(headingDummyData1.user);
Expand Down
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import { describe, test, expect, beforeEach, afterEach } from "vitest";
describe("Project controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createProject({} as ProjectType)).rejects.toThrowError("failed");
await expect(getAllProjects({} as string)).rejects.toThrowError();
await expect(getProjectById({} as string, {} as string)).rejects.toThrowError();
await expect(updateProject({} as string, {} as string, {} as ProjectType)).rejects.toThrowError();
await expect(deleteProject({} as string, {} as string)).rejects.toThrowError();
await expect(deleteProject("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves an project", async () => {
await createProject(projectDummyData1 as ProjectType);
Expand Down
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/resume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ describe("Resume controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createResume({} as resumeType)).rejects.toThrowError("failed");
await expect(getAllResumes({} as string)).rejects.toThrowError();
await expect(getResumeById({} as string, {} as string)).rejects.toThrowError();
await expect(updateResume({} as string, {} as string, {} as resumeType)).rejects.toThrowError();
await expect(deleteResume({} as string, {} as string)).rejects.toThrowError();
await expect(deleteResume("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves a resumes", async () => {
await createResume(resumeDummyData1 as resumeType);
const returnedResumes = await getAllResumes(resumeDummyData1.user);
Expand Down
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/sectionHeading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe("SectionHeadings controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createSectionHeading({} as SectionHeadingType)).rejects.toThrowError("failed");
await expect(getAllSectionHeadings({} as string)).rejects.toThrowError();
await expect(getSectionHeadingById({} as string, {} as string)).rejects.toThrowError();
await expect(updateSectionHeading({} as string, {} as string, {} as SectionHeadingType)).rejects.toThrowError();
await expect(deleteSectionHeading({} as string, {} as string)).rejects.toThrowError();
await expect(deleteSectionHeading("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves an sectionHeadings", async () => {
await createSectionHeading(sectionHeadingDummyData1 as SectionHeadingType);
const returnedSectionHeadings = await getAllSectionHeadings(sectionHeadingDummyData1.user);
Expand Down
9 changes: 9 additions & 0 deletions src/tests/controllers.tests/skills.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe("Skills controller tests", () => {
beforeEach(async () => dbConnect());
afterEach(async () => dbDisconnect());

test("Errors throw correctly", async () => {
await expect(createSkill({} as SkillsType)).rejects.toThrowError("failed");
await expect(getAllSkills({} as string)).rejects.toThrowError();
await expect(getSkillById({} as string, {} as string)).rejects.toThrowError();
await expect(updateSkill({} as string, {} as string, {} as SkillsType)).rejects.toThrowError();
await expect(deleteSkill({} as string, {} as string)).rejects.toThrowError();
await expect(deleteSkill("1234", "65f2268e3dc262b1277ba0e5")).rejects.toThrowError();
});

test("Adds and retrieves an skills", async () => {
await createSkill(skillsDummyData1 as SkillsType);
const returnedSkills = await getAllSkills(skillsDummyData1.user);
Expand Down
28 changes: 28 additions & 0 deletions src/tests/middlewares.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { verifyToken } from "../middlewares/verifyToken";
import { type Request, type Response, type NextFunction } from "express";
import { describe, test, expect } from "vitest";

const mockNext = (e: any) => {
if(e) {
throw e;
} else {
return;
}
}

describe("Verify Token Tests", () => {

test("No token errors", async () => {
const mockReq = {};
await expect(verifyToken(mockReq as Request, {} as Response, mockNext as NextFunction)).rejects.toThrowError("not found");
});

test("Fake token errors", async () => {
const mockReq = {
headers: {
authorization: "Bearer 1234",
},
};
await expect(verifyToken(mockReq as Request, {} as Response, mockNext as NextFunction)).rejects.toThrowError();
})
});
6 changes: 6 additions & 0 deletions src/tests/routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import '../index';
import {test, expect} from "vitest"

test("server startup works", () => {
expect(1+1).toBe(2);
})
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
coverage: {
reporter: ['text', 'json', 'html'],
},
},
})

0 comments on commit 4fc0755

Please sign in to comment.