From 050b759cce1369aa1500c90543b1c23463a9a3a6 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 13 Mar 2021 14:24:05 +0700 Subject: [PATCH] Add user tests --- package.json | 2 +- tests/user.test.ts | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 79aeefe..20d9a11 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "scripts": { "dev": "ts-node ./src/main.ts", "migrate": "prisma migrate dev --name add-profile --preview-feature", - "test": "jest" + "test": "jest --detectOpenHandles" }, "dependencies": { "@prisma/client": "2.18.0" diff --git a/tests/user.test.ts b/tests/user.test.ts index ef97403..89b4ba6 100644 --- a/tests/user.test.ts +++ b/tests/user.test.ts @@ -1,3 +1,4 @@ +import { PrismaClient } from "@prisma/client"; import { createUser, findOneByEmail, @@ -5,6 +6,8 @@ import { findOneProfileByEmail, } from "../src/User/UserService"; +const prisma = new PrismaClient(); + // all tests about user describe("user", function () { const email = "sombochea@cubetiqs.com"; @@ -18,7 +21,7 @@ describe("user", function () { email: email, name: name, }) - .then((user) => { + .then(async (user) => { if (user == null) { throw Error("user is null"); } @@ -28,7 +31,7 @@ describe("user", function () { expect(name).toBe(user.name); // create profile - createProfile(user, { + await createProfile(user, { bio: "Software Developer", }) .then((profile) => { @@ -39,9 +42,11 @@ describe("user", function () { expect(profile).not.toBeNull(); expect(bio).toBe(profile.bio); }) - .catch((e) => console.error(e)); + .catch((e) => console.error(e)) + .finally(() => done()); }) - .catch((e) => console.error(e)); + .catch((e) => console.error(e)) + .finally(() => done()); }); // check user test @@ -56,9 +61,8 @@ describe("user", function () { expect(email).toBe(user.email); expect(name).toBe(user.name); }) - .catch((e) => console.error(e)); - - done(); + .catch((e) => console.error(e)) + .finally(() => done()); }); // check profile included user test @@ -74,8 +78,16 @@ describe("user", function () { expect(email).toBe(profile.user.email); expect(name).toBe(profile.user.name); }) - .catch((e) => console.error(e)); + .catch((e) => console.error(e)) + .finally(() => done()); + }); + + // try to finish + afterAll(async (done) => { + await prisma.$disconnect(); done(); }); + + // after all });