Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
447011 committed Mar 6, 2022
1 parent a43e956 commit 8a1ec30
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ yarn-debug.log*
yarn-error.log*

# env files
.env
.env.development
.env.test

# vercel
.vercel
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const customJestConfig = {
"^@tests/(.*)$": "<rootDir>/tests/$1",
},
testEnvironment: "jest-environment-jsdom",
collectCoverageFrom: ["pages/api/**", "business-logic/**"],
collectCoverageFrom: ["business-logic/**"],
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"prettier": "prettier --write .",
"prepare": "husky install",
"pre-commit": "lint-staged",
"test": "jest",
"test:coverage": "jest --coverage"
"test": "npm run test:db:reset && node --inspect node_modules/.bin/jest --runInBand --watch --testNamePattern",
"test:coverage": "npm run test:db:reset && node --inspect node_modules/.bin/jest --coverage",
"test:db:reset": "dotenv -e .env.test -- npx prisma migrate reset --force --skip-seed && dotenv -e .env.test -- npx prisma db push"
},
"dependencies": {
"@headlessui/react": "^1.5.0",
Expand All @@ -38,6 +39,7 @@
"@typescript-eslint/parser": "^5.10.0",
"autoprefixer": "^10.4.2",
"babel-jest": "^27.4.6",
"dotenv-cli": "^5.0.0",
"eslint": "^8.7.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
Expand Down
28 changes: 28 additions & 0 deletions tests/business-logic/Job.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import JobEntity from "@business-logic/Job";
import { Job } from "@prisma/client";
import { minimalSetup } from "@tests/helpers/setup";
import { teardown } from "@tests/helpers/teardown";

import prisma from "@helpers/prisma";

beforeEach(async () => {
await teardown();
});

test("creates job successfully", async () => {
const { user } = await minimalSetup();

const requestParams = {
title: "Software Engineer",
description: "You write code.",
};

const entity = new JobEntity();
const result = await entity.create(requestParams, user.id);

const job = (await prisma.job.findUnique({ where: { id: result.id } })) as Job;

expect(job).not.toBeNull();
expect(job.title).toBe(requestParams.title);
expect(job.description).toBe(requestParams.description);
});
19 changes: 19 additions & 0 deletions tests/helpers/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import prisma from "@helpers/prisma";

export async function minimalSetup() {
const company = await prisma.company.create({
data: {
name: "John Inc",
},
});
const user = await prisma.user.create({
data: {
name: "John Doe",
email: "john.doe@example.com",
password: "password",
companyId: company.id,
},
});

return { user, company };
}
5 changes: 5 additions & 0 deletions tests/helpers/teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import prisma from "@helpers/prisma";

export async function teardown() {
return prisma.company.deleteMany({});
}
6 changes: 0 additions & 6 deletions tests/sample.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"@helpers/*": ["helpers/*"],
"@pages/*": ["pages/*"],
"@business-logic/*": ["business-logic/*"],
"@api-contracts/*": ["api-contracts/*"]
"@api-contracts/*": ["api-contracts/*"],
"@tests/*": ["tests/*"]
},
"skipLibCheck": true,
"strict": true,
Expand Down

0 comments on commit 8a1ec30

Please sign in to comment.