diff --git a/server/commands/accountProvisioner.test.ts b/server/commands/accountProvisioner.test.ts index 6060914fa..f1043f2a3 100644 --- a/server/commands/accountProvisioner.test.ts +++ b/server/commands/accountProvisioner.test.ts @@ -4,14 +4,10 @@ import { TeamDomain } from "@server/models"; import Collection from "@server/models/Collection"; import UserAuthentication from "@server/models/UserAuthentication"; import { buildUser, buildTeam } from "@server/test/factories"; -import { getTestDatabase, seed } from "@server/test/support"; +import { setupTestDatabase, seed } from "@server/test/support"; import accountProvisioner from "./accountProvisioner"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("accountProvisioner", () => { const ip = "127.0.0.1"; diff --git a/server/commands/documentImporter.test.ts b/server/commands/documentImporter.test.ts index 79b48752b..d0c2810d2 100644 --- a/server/commands/documentImporter.test.ts +++ b/server/commands/documentImporter.test.ts @@ -2,16 +2,12 @@ import path from "path"; import fs from "fs-extra"; import Attachment from "@server/models/Attachment"; import { buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import documentImporter from "./documentImporter"; jest.mock("../utils/s3"); -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("documentImporter", () => { const ip = "127.0.0.1"; diff --git a/server/commands/documentMover.test.ts b/server/commands/documentMover.test.ts index 28a2abb81..39ea6c4f8 100644 --- a/server/commands/documentMover.test.ts +++ b/server/commands/documentMover.test.ts @@ -5,14 +5,10 @@ import { buildCollection, buildUser, } from "@server/test/factories"; -import { getTestDatabase, seed } from "@server/test/support"; +import { setupTestDatabase, seed } from "@server/test/support"; import documentMover from "./documentMover"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("documentMover", () => { const ip = "127.0.0.1"; diff --git a/server/commands/documentPermanentDeleter.test.ts b/server/commands/documentPermanentDeleter.test.ts index af4c8897f..0a37d8480 100644 --- a/server/commands/documentPermanentDeleter.test.ts +++ b/server/commands/documentPermanentDeleter.test.ts @@ -1,14 +1,10 @@ import { subDays } from "date-fns"; import { Attachment, Document } from "@server/models"; import { buildAttachment, buildDocument } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import documentPermanentDeleter from "./documentPermanentDeleter"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("documentPermanentDeleter", () => { it("should destroy documents", async () => { diff --git a/server/commands/documentUpdater.test.ts b/server/commands/documentUpdater.test.ts index 4b45a2518..669d89cf5 100644 --- a/server/commands/documentUpdater.test.ts +++ b/server/commands/documentUpdater.test.ts @@ -1,14 +1,10 @@ import { sequelize } from "@server/database/sequelize"; import { Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import documentUpdater from "./documentUpdater"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("documentUpdater", () => { const ip = "127.0.0.1"; diff --git a/server/commands/fileOperationDeleter.test.ts b/server/commands/fileOperationDeleter.test.ts index 1f045af91..b4c55e427 100644 --- a/server/commands/fileOperationDeleter.test.ts +++ b/server/commands/fileOperationDeleter.test.ts @@ -1,13 +1,9 @@ import { FileOperation } from "@server/models"; import { buildAdmin, buildFileOperation } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import fileOperationDeleter from "./fileOperationDeleter"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("fileOperationDeleter", () => { const ip = "127.0.0.1"; diff --git a/server/commands/pinCreator.test.ts b/server/commands/pinCreator.test.ts index 5c239af61..27a6e540c 100644 --- a/server/commands/pinCreator.test.ts +++ b/server/commands/pinCreator.test.ts @@ -1,13 +1,9 @@ import { Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import pinCreator from "./pinCreator"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("pinCreator", () => { const ip = "127.0.0.1"; diff --git a/server/commands/pinDestroyer.test.ts b/server/commands/pinDestroyer.test.ts index f505c4770..48381ac6d 100644 --- a/server/commands/pinDestroyer.test.ts +++ b/server/commands/pinDestroyer.test.ts @@ -1,13 +1,9 @@ import { Pin, Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import pinDestroyer from "./pinDestroyer"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("pinCreator", () => { const ip = "127.0.0.1"; diff --git a/server/commands/revisionCreator.test.ts b/server/commands/revisionCreator.test.ts index 0bab15be0..236974de0 100644 --- a/server/commands/revisionCreator.test.ts +++ b/server/commands/revisionCreator.test.ts @@ -1,13 +1,9 @@ import { Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import revisionCreator from "./revisionCreator"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("revisionCreator", () => { const ip = "127.0.0.1"; diff --git a/server/commands/starCreator.test.ts b/server/commands/starCreator.test.ts index aa3f5ce4f..21e121716 100644 --- a/server/commands/starCreator.test.ts +++ b/server/commands/starCreator.test.ts @@ -1,14 +1,10 @@ import { sequelize } from "@server/database/sequelize"; import { Star, Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import starCreator from "./starCreator"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("starCreator", () => { const ip = "127.0.0.1"; diff --git a/server/commands/starDestroyer.test.ts b/server/commands/starDestroyer.test.ts index 3043fa354..d2b51bbfc 100644 --- a/server/commands/starDestroyer.test.ts +++ b/server/commands/starDestroyer.test.ts @@ -1,13 +1,9 @@ import { Star, Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import starDestroyer from "./starDestroyer"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("starDestroyer", () => { const ip = "127.0.0.1"; diff --git a/server/commands/starUpdater.test.ts b/server/commands/starUpdater.test.ts index 518f44ab6..05cd7818a 100644 --- a/server/commands/starUpdater.test.ts +++ b/server/commands/starUpdater.test.ts @@ -1,13 +1,9 @@ import { Star, Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import starUpdater from "./starUpdater"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("starUpdater", () => { const ip = "127.0.0.1"; diff --git a/server/commands/subscriptionCreator.test.ts b/server/commands/subscriptionCreator.test.ts index c5871a078..2cbeaf0db 100644 --- a/server/commands/subscriptionCreator.test.ts +++ b/server/commands/subscriptionCreator.test.ts @@ -1,14 +1,11 @@ import { sequelize } from "@server/database/sequelize"; import { Subscription, Event } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import subscriptionCreator from "./subscriptionCreator"; import subscriptionDestroyer from "./subscriptionDestroyer"; -const db = getTestDatabase(); - -beforeEach(db.flush); -afterAll(db.disconnect); +setupTestDatabase(); describe("subscriptionCreator", () => { const ip = "127.0.0.1"; diff --git a/server/commands/subscriptionDestroyer.test.ts b/server/commands/subscriptionDestroyer.test.ts index 0618e72be..172994550 100644 --- a/server/commands/subscriptionDestroyer.test.ts +++ b/server/commands/subscriptionDestroyer.test.ts @@ -5,13 +5,10 @@ import { buildSubscription, buildUser, } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import subscriptionDestroyer from "./subscriptionDestroyer"; -const db = getTestDatabase(); - -beforeEach(db.flush); -afterAll(db.disconnect); +setupTestDatabase(); describe("subscriptionDestroyer", () => { const ip = "127.0.0.1"; diff --git a/server/commands/teamPermanentDeleter.test.ts b/server/commands/teamPermanentDeleter.test.ts index 79e58d804..f16d93475 100644 --- a/server/commands/teamPermanentDeleter.test.ts +++ b/server/commands/teamPermanentDeleter.test.ts @@ -6,14 +6,10 @@ import { buildTeam, buildDocument, } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import teamPermanentDeleter from "./teamPermanentDeleter"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("teamPermanentDeleter", () => { it("should destroy related data", async () => { diff --git a/server/commands/teamProvisioner.test.ts b/server/commands/teamProvisioner.test.ts index c2b0ac849..c9bc9ea08 100644 --- a/server/commands/teamProvisioner.test.ts +++ b/server/commands/teamProvisioner.test.ts @@ -1,14 +1,10 @@ import env from "@server/env"; import TeamDomain from "@server/models/TeamDomain"; import { buildTeam, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import teamProvisioner from "./teamProvisioner"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("teamProvisioner", () => { const ip = "127.0.0.1"; diff --git a/server/commands/userDemoter.test.ts b/server/commands/userDemoter.test.ts index fb0407fe0..e984828f9 100644 --- a/server/commands/userDemoter.test.ts +++ b/server/commands/userDemoter.test.ts @@ -2,14 +2,10 @@ import { CollectionPermission } from "@shared/types"; import { CollectionUser } from "@server/models"; import { UserRole } from "@server/models/User"; import { buildUser, buildAdmin, buildCollection } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import userDemoter from "./userDemoter"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("userDemoter", () => { const ip = "127.0.0.1"; diff --git a/server/commands/userDestroyer.test.ts b/server/commands/userDestroyer.test.ts index 5dcc0587a..3fd60acb9 100644 --- a/server/commands/userDestroyer.test.ts +++ b/server/commands/userDestroyer.test.ts @@ -1,12 +1,8 @@ import { buildUser, buildAdmin } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import userDestroyer from "./userDestroyer"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("userDestroyer", () => { const ip = "127.0.0.1"; diff --git a/server/commands/userInviter.test.ts b/server/commands/userInviter.test.ts index cc8b9ce06..b7b9073b3 100644 --- a/server/commands/userInviter.test.ts +++ b/server/commands/userInviter.test.ts @@ -1,12 +1,8 @@ import { buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import userInviter from "./userInviter"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("userInviter", () => { const ip = "127.0.0.1"; diff --git a/server/commands/userProvisioner.test.ts b/server/commands/userProvisioner.test.ts index c0547e326..3b8cdcfa2 100644 --- a/server/commands/userProvisioner.test.ts +++ b/server/commands/userProvisioner.test.ts @@ -1,14 +1,10 @@ import { v4 as uuidv4 } from "uuid"; import { TeamDomain } from "@server/models"; import { buildUser, buildTeam, buildInvite } from "@server/test/factories"; -import { getTestDatabase, seed } from "@server/test/support"; +import { setupTestDatabase, seed } from "@server/test/support"; import userProvisioner from "./userProvisioner"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("userProvisioner", () => { const ip = "127.0.0.1"; diff --git a/server/commands/userSuspender.test.ts b/server/commands/userSuspender.test.ts index 3de13aec0..df2c2a4b5 100644 --- a/server/commands/userSuspender.test.ts +++ b/server/commands/userSuspender.test.ts @@ -1,13 +1,9 @@ import GroupUser from "@server/models/GroupUser"; import { buildGroup, buildAdmin, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import userSuspender from "./userSuspender"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("userSuspender", () => { const ip = "127.0.0.1"; diff --git a/server/commands/userUnsuspender.test.ts b/server/commands/userUnsuspender.test.ts index 973ce4bf8..6269f6b7a 100644 --- a/server/commands/userUnsuspender.test.ts +++ b/server/commands/userUnsuspender.test.ts @@ -1,12 +1,8 @@ import { buildAdmin, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import userUnsuspender from "./userUnsuspender"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("userUnsuspender", () => { const ip = "127.0.0.1"; diff --git a/server/middlewares/authentication.test.ts b/server/middlewares/authentication.test.ts index dc954022a..81e61fdf4 100644 --- a/server/middlewares/authentication.test.ts +++ b/server/middlewares/authentication.test.ts @@ -1,14 +1,10 @@ import randomstring from "randomstring"; import ApiKey from "@server/models/ApiKey"; import { buildUser, buildTeam } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import auth from "./authentication"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("Authentication middleware", () => { describe("with JWT", () => { diff --git a/server/models/Collection.test.ts b/server/models/Collection.test.ts index 11db67c88..38a159137 100644 --- a/server/models/Collection.test.ts +++ b/server/models/Collection.test.ts @@ -7,17 +7,14 @@ import { buildTeam, buildDocument, } from "@server/test/factories"; -import { getTestDatabase, seed } from "@server/test/support"; +import { setupTestDatabase, seed } from "@server/test/support"; import slugify from "@server/utils/slugify"; import Collection from "./Collection"; import Document from "./Document"; -const db = getTestDatabase(); +setupTestDatabase(); -afterAll(db.disconnect); - -beforeEach(async () => { - await db.flush(); +beforeEach(() => { jest.resetAllMocks(); }); diff --git a/server/models/Document.test.ts b/server/models/Document.test.ts index d524ab41f..2cd41f271 100644 --- a/server/models/Document.test.ts +++ b/server/models/Document.test.ts @@ -6,15 +6,12 @@ import { buildUser, buildShare, } from "@server/test/factories"; -import { getTestDatabase, seed } from "@server/test/support"; +import { setupTestDatabase, seed } from "@server/test/support"; import slugify from "@server/utils/slugify"; -const db = getTestDatabase(); +setupTestDatabase(); -afterAll(db.disconnect); - -beforeEach(async () => { - await db.flush(); +beforeEach(() => { jest.resetAllMocks(); }); diff --git a/server/models/Group.test.ts b/server/models/Group.test.ts index 2c116c45b..e9c33fb88 100644 --- a/server/models/Group.test.ts +++ b/server/models/Group.test.ts @@ -1,14 +1,11 @@ import { buildUser, buildGroup, buildCollection } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import CollectionGroup from "./CollectionGroup"; import GroupUser from "./GroupUser"; -const db = getTestDatabase(); - -afterAll(db.disconnect); +setupTestDatabase(); beforeEach(async () => { - await db.flush(); jest.resetAllMocks(); }); diff --git a/server/models/Revision.test.ts b/server/models/Revision.test.ts index 9e62f7d52..97659fc48 100644 --- a/server/models/Revision.test.ts +++ b/server/models/Revision.test.ts @@ -1,15 +1,8 @@ import { buildDocument } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import Revision from "./Revision"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(async () => { - await db.flush(); - jest.resetAllMocks(); -}); +setupTestDatabase(); describe("#findLatest", () => { test("should return latest revision", async () => { diff --git a/server/models/Team.test.ts b/server/models/Team.test.ts index a43b0b900..2ed05fdb5 100644 --- a/server/models/Team.test.ts +++ b/server/models/Team.test.ts @@ -1,14 +1,7 @@ import { buildTeam, buildCollection } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(async () => { - await db.flush(); - jest.resetAllMocks(); -}); +setupTestDatabase(); describe("collectionIds", () => { it("should return non-private collection ids", async () => { diff --git a/server/models/TeamDomain.test.ts b/server/models/TeamDomain.test.ts index bef4cf5ad..1f5ff5a09 100644 --- a/server/models/TeamDomain.test.ts +++ b/server/models/TeamDomain.test.ts @@ -1,16 +1,9 @@ import env from "@server/env"; import { buildAdmin, buildTeam } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import TeamDomain from "./TeamDomain"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(async () => { - await db.flush(); - jest.resetAllMocks(); -}); +setupTestDatabase(); describe("team domain model", () => { describe("create", () => { diff --git a/server/models/User.test.ts b/server/models/User.test.ts index 362abc924..93b5c62aa 100644 --- a/server/models/User.test.ts +++ b/server/models/User.test.ts @@ -1,10 +1,10 @@ import { CollectionPermission } from "@shared/types"; import { buildUser, buildTeam, buildCollection } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import CollectionUser from "./CollectionUser"; import UserAuthentication from "./UserAuthentication"; -const db = getTestDatabase(); +setupTestDatabase(); beforeAll(() => { jest.useFakeTimers().setSystemTime(new Date("2018-01-02T00:00:00.000Z")); @@ -12,11 +12,8 @@ beforeAll(() => { afterAll(() => { jest.useRealTimers(); - db.disconnect(); }); -beforeEach(db.flush); - describe("user model", () => { describe("destroy", () => { it("should delete user authentications", async () => { diff --git a/server/policies/collection.test.ts b/server/policies/collection.test.ts index 49d605671..dcac2e92d 100644 --- a/server/policies/collection.test.ts +++ b/server/policies/collection.test.ts @@ -1,14 +1,10 @@ import { CollectionPermission } from "@shared/types"; import { CollectionUser, Collection } from "@server/models"; import { buildUser, buildTeam, buildCollection } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import { serialize } from "./index"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("member", () => { describe("read_write permission", () => { diff --git a/server/policies/document.test.ts b/server/policies/document.test.ts index ad0533f45..015e3b703 100644 --- a/server/policies/document.test.ts +++ b/server/policies/document.test.ts @@ -5,14 +5,10 @@ import { buildDocument, buildCollection, } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import { serialize } from "./index"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("read_write collection", () => { it("should allow read write permissions for member", async () => { diff --git a/server/policies/index.test.ts b/server/policies/index.test.ts index 86b9de99c..0a2b92669 100644 --- a/server/policies/index.test.ts +++ b/server/policies/index.test.ts @@ -1,12 +1,8 @@ import { buildUser, buildTeam } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import { serialize } from "./index"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); it("should serialize policy", async () => { const user = await buildUser(); diff --git a/server/policies/team.test.ts b/server/policies/team.test.ts index f997da3a4..7b8b7dc3e 100644 --- a/server/policies/team.test.ts +++ b/server/policies/team.test.ts @@ -1,12 +1,8 @@ import { buildUser, buildTeam, buildAdmin } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import { serialize } from "./index"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); it("should allow reading only", async () => { const team = await buildTeam(); diff --git a/server/queues/processors/BacklinksProcessor.test.ts b/server/queues/processors/BacklinksProcessor.test.ts index c533669b7..4c88207c9 100644 --- a/server/queues/processors/BacklinksProcessor.test.ts +++ b/server/queues/processors/BacklinksProcessor.test.ts @@ -1,18 +1,11 @@ import { Backlink } from "@server/models"; import { buildDocument } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import BacklinksProcessor from "./BacklinksProcessor"; const ip = "127.0.0.1"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(async () => { - await db.flush(); - jest.resetAllMocks(); -}); +setupTestDatabase(); describe("documents.publish", () => { test("should create new backlink records", async () => { diff --git a/server/queues/processors/NotificationsProcessor.test.ts b/server/queues/processors/NotificationsProcessor.test.ts index f9650c497..e383e74c3 100644 --- a/server/queues/processors/NotificationsProcessor.test.ts +++ b/server/queues/processors/NotificationsProcessor.test.ts @@ -12,18 +12,15 @@ import { buildCollection, buildUser, } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import NotificationsProcessor from "./NotificationsProcessor"; jest.mock("@server/emails/templates/DocumentNotificationEmail"); const ip = "127.0.0.1"; -const db = getTestDatabase(); - -afterAll(db.disconnect); +setupTestDatabase(); beforeEach(async () => { - await db.flush(); jest.resetAllMocks(); }); diff --git a/server/queues/processors/RevisionsProcessor.test.ts b/server/queues/processors/RevisionsProcessor.test.ts index a0934cf3c..bc0d7666e 100644 --- a/server/queues/processors/RevisionsProcessor.test.ts +++ b/server/queues/processors/RevisionsProcessor.test.ts @@ -1,18 +1,11 @@ import { Revision } from "@server/models"; import { buildDocument } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import RevisionsProcessor from "./RevisionsProcessor"; const ip = "127.0.0.1"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(async () => { - await db.flush(); - jest.resetAllMocks(); -}); +setupTestDatabase(); describe("documents.update.debounced", () => { test("should create a revision", async () => { diff --git a/server/queues/processors/WebhookProcessor.test.ts b/server/queues/processors/WebhookProcessor.test.ts index fd2d62f83..9c9fd8510 100644 --- a/server/queues/processors/WebhookProcessor.test.ts +++ b/server/queues/processors/WebhookProcessor.test.ts @@ -1,5 +1,5 @@ import { buildUser, buildWebhookSubscription } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import { UserEvent } from "@server/types"; import DeliverWebhookTask from "../tasks/DeliverWebhookTask"; import WebhookProcessor from "./WebhookProcessor"; @@ -7,12 +7,9 @@ import WebhookProcessor from "./WebhookProcessor"; jest.mock("@server/queues/tasks/DeliverWebhookTask"); const ip = "127.0.0.1"; -const db = getTestDatabase(); - -afterAll(db.disconnect); +setupTestDatabase(); beforeEach(async () => { - await db.flush(); jest.resetAllMocks(); }); diff --git a/server/queues/tasks/CleanupDeletedDocumentsTask.test.ts b/server/queues/tasks/CleanupDeletedDocumentsTask.test.ts index ffa62f32a..7a7a561ae 100644 --- a/server/queues/tasks/CleanupDeletedDocumentsTask.test.ts +++ b/server/queues/tasks/CleanupDeletedDocumentsTask.test.ts @@ -1,14 +1,10 @@ import { subDays } from "date-fns"; import { Document } from "@server/models"; import { buildDocument } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import CleanupDeletedDocumentsTask from "./CleanupDeletedDocumentsTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("CleanupDeletedDocumentsTask", () => { it("should not destroy documents not deleted", async () => { diff --git a/server/queues/tasks/CleanupDemotedUserTask.test.ts b/server/queues/tasks/CleanupDemotedUserTask.test.ts index 5d2ca9aac..ca8aaa7c4 100644 --- a/server/queues/tasks/CleanupDemotedUserTask.test.ts +++ b/server/queues/tasks/CleanupDemotedUserTask.test.ts @@ -6,14 +6,10 @@ import { buildWebhookSubscription, buildViewer, } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import CleanupDemotedUserTask from "./CleanupDemotedUserTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("CleanupDemotedUserTask", () => { it("should delete api keys for suspended user", async () => { diff --git a/server/queues/tasks/CleanupExpiredFileOperationsTask.test.ts b/server/queues/tasks/CleanupExpiredFileOperationsTask.test.ts index 2a6ca74b5..034eda0c1 100644 --- a/server/queues/tasks/CleanupExpiredFileOperationsTask.test.ts +++ b/server/queues/tasks/CleanupExpiredFileOperationsTask.test.ts @@ -5,14 +5,10 @@ import { FileOperationType, } from "@server/models/FileOperation"; import { buildFileOperation } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import CleanupExpiredFileOperationsTask from "./CleanupExpiredFileOperationsTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("CleanupExpiredFileOperationsTask", () => { it("should expire exports older than 15 days ago", async () => { diff --git a/server/queues/tasks/CleanupWebhookDeliveriesTask.test.ts b/server/queues/tasks/CleanupWebhookDeliveriesTask.test.ts index 3ed4d6ce6..7dd82526a 100644 --- a/server/queues/tasks/CleanupWebhookDeliveriesTask.test.ts +++ b/server/queues/tasks/CleanupWebhookDeliveriesTask.test.ts @@ -1,14 +1,10 @@ import { subDays } from "date-fns"; import { WebhookDelivery } from "@server/models"; import { buildWebhookDelivery } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import CleanupWebhookDeliveriesTask from "./CleanupWebhookDeliveriesTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); const deliveryExists = async (delivery: WebhookDelivery) => { const results = await WebhookDelivery.findOne({ where: { id: delivery.id } }); diff --git a/server/queues/tasks/DeliverWebhookTask.test.ts b/server/queues/tasks/DeliverWebhookTask.test.ts index f22214892..50a11bb2b 100644 --- a/server/queues/tasks/DeliverWebhookTask.test.ts +++ b/server/queues/tasks/DeliverWebhookTask.test.ts @@ -6,16 +6,13 @@ import { buildWebhookDelivery, buildWebhookSubscription, } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import { UserEvent } from "@server/types"; import DeliverWebhookTask from "./DeliverWebhookTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); +setupTestDatabase(); beforeEach(async () => { - await db.flush(); jest.resetAllMocks(); fetchMock.resetMocks(); fetchMock.doMock(); diff --git a/server/queues/tasks/ImportMarkdownZipTask.test.ts b/server/queues/tasks/ImportMarkdownZipTask.test.ts index a0c162ef4..0039500bd 100644 --- a/server/queues/tasks/ImportMarkdownZipTask.test.ts +++ b/server/queues/tasks/ImportMarkdownZipTask.test.ts @@ -2,14 +2,10 @@ import fs from "fs"; import path from "path"; import { FileOperation } from "@server/models"; import { buildFileOperation } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import ImportMarkdownZipTask from "./ImportMarkdownZipTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("ImportMarkdownZipTask", () => { it("should import the documents, attachments", async () => { diff --git a/server/queues/tasks/ImportNotionTask.test.ts b/server/queues/tasks/ImportNotionTask.test.ts index e664e2589..082092e40 100644 --- a/server/queues/tasks/ImportNotionTask.test.ts +++ b/server/queues/tasks/ImportNotionTask.test.ts @@ -2,14 +2,10 @@ import fs from "fs"; import path from "path"; import { FileOperation } from "@server/models"; import { buildFileOperation } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import ImportNotionTask from "./ImportNotionTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("ImportNotionTask", () => { it("should import successfully from a Markdown export", async () => { diff --git a/server/queues/tasks/InviteReminderTask.test.ts b/server/queues/tasks/InviteReminderTask.test.ts index 39f8529bd..aaa1d1ee2 100644 --- a/server/queues/tasks/InviteReminderTask.test.ts +++ b/server/queues/tasks/InviteReminderTask.test.ts @@ -1,14 +1,10 @@ import { subDays } from "date-fns"; import InviteReminderEmail from "@server/emails/templates/InviteReminderEmail"; import { buildInvite } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import InviteReminderTask from "./InviteReminderTask"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("InviteReminderTask", () => { it("should not destroy documents not deleted", async () => { diff --git a/server/routes/api/attachments.test.ts b/server/routes/api/attachments.test.ts index ebf7baa7a..dd9e3541c 100644 --- a/server/routes/api/attachments.test.ts +++ b/server/routes/api/attachments.test.ts @@ -6,17 +6,12 @@ import { buildAttachment, buildDocument, } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; jest.mock("@server/utils/s3"); -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#attachments.create", () => { it("should require authentication", async () => { const res = await server.post("/api/attachments.create"); diff --git a/server/routes/api/auth.test.ts b/server/routes/api/auth.test.ts index f84b070d5..90c1b32df 100644 --- a/server/routes/api/auth.test.ts +++ b/server/routes/api/auth.test.ts @@ -1,7 +1,7 @@ import sharedEnv from "@shared/env"; import env from "@server/env"; import { buildUser, buildTeam } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; const mockTeamInSessionId = "1e023d05-951c-41c6-9012-c9fa0402e1c3"; @@ -13,13 +13,8 @@ jest.mock("@server/utils/authentication", () => { }; }); -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#auth.info", () => { it("should return current authentication", async () => { const team = await buildTeam(); diff --git a/server/routes/api/authenticationProviders.test.ts b/server/routes/api/authenticationProviders.test.ts index e7fd3af0a..2e05ae9f3 100644 --- a/server/routes/api/authenticationProviders.test.ts +++ b/server/routes/api/authenticationProviders.test.ts @@ -1,14 +1,9 @@ import { v4 as uuidv4 } from "uuid"; import { buildUser, buildAdmin, buildTeam } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#authenticationProviders.info", () => { it("should return auth provider", async () => { const team = await buildTeam(); diff --git a/server/routes/api/collections.test.ts b/server/routes/api/collections.test.ts index 2c404a2d4..a370c65e5 100644 --- a/server/routes/api/collections.test.ts +++ b/server/routes/api/collections.test.ts @@ -8,15 +8,10 @@ import { buildCollection, buildDocument, } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#collections.list", () => { it("should require authentication", async () => { const res = await server.post("/api/collections.list"); diff --git a/server/routes/api/cron.test.ts b/server/routes/api/cron.test.ts index 72162f261..a49411933 100644 --- a/server/routes/api/cron.test.ts +++ b/server/routes/api/cron.test.ts @@ -1,12 +1,7 @@ -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#cron.daily", () => { it("should require authentication", async () => { const res = await server.post("/api/cron.daily"); diff --git a/server/routes/api/documents.test.ts b/server/routes/api/documents.test.ts index ef477e14d..29770437d 100644 --- a/server/routes/api/documents.test.ts +++ b/server/routes/api/documents.test.ts @@ -16,15 +16,10 @@ import { buildDocument, buildViewer, } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#documents.info", () => { it("should return published document", async () => { const { user, document } = await seed(); diff --git a/server/routes/api/events.test.ts b/server/routes/api/events.test.ts index c2bc6b889..6b0497228 100644 --- a/server/routes/api/events.test.ts +++ b/server/routes/api/events.test.ts @@ -1,13 +1,8 @@ import { buildEvent, buildUser } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#events.list", () => { it("should only return activity events", async () => { const { user, admin, document, collection } = await seed(); diff --git a/server/routes/api/fileOperations.test.ts b/server/routes/api/fileOperations.test.ts index 667ca9e9f..3fd1f4c5c 100644 --- a/server/routes/api/fileOperations.test.ts +++ b/server/routes/api/fileOperations.test.ts @@ -11,17 +11,12 @@ import { buildUser, } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); jest.mock("@server/utils/s3"); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#fileOperations.info", () => { it("should return fileOperation", async () => { const team = await buildTeam(); diff --git a/server/routes/api/groups.test.ts b/server/routes/api/groups.test.ts index f98576d22..d45018660 100644 --- a/server/routes/api/groups.test.ts +++ b/server/routes/api/groups.test.ts @@ -1,14 +1,9 @@ import { Event } from "@server/models"; import { buildUser, buildAdmin, buildGroup } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#groups.create", () => { it("should create a group", async () => { const name = "hello I am a group"; diff --git a/server/routes/api/hooks.test.ts b/server/routes/api/hooks.test.ts index 0ba6332dd..5f6fe1609 100644 --- a/server/routes/api/hooks.test.ts +++ b/server/routes/api/hooks.test.ts @@ -1,20 +1,15 @@ import env from "@server/env"; import { IntegrationAuthentication, SearchQuery } from "@server/models"; import { buildDocument, buildIntegration } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; import * as Slack from "@server/utils/slack"; jest.mock("../../utils/slack", () => ({ post: jest.fn(), })); -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#hooks.unfurl", () => { it("should return documents", async () => { const { user, document } = await seed(); diff --git a/server/routes/api/index.test.ts b/server/routes/api/index.test.ts index 857c3031e..72e5af9be 100644 --- a/server/routes/api/index.test.ts +++ b/server/routes/api/index.test.ts @@ -2,8 +2,6 @@ import { getTestServer } from "@server/test/support"; const server = getTestServer(); -afterAll(server.disconnect); - describe("POST unknown endpoint", () => { it("should be not found", async () => { const res = await server.post("/api/blah"); diff --git a/server/routes/api/integrations.test.ts b/server/routes/api/integrations.test.ts index 546b637dc..85724ce19 100644 --- a/server/routes/api/integrations.test.ts +++ b/server/routes/api/integrations.test.ts @@ -4,16 +4,10 @@ import { buildUser, buildIntegration, } from "@server/test/factories"; +import { getTestServer } from "@server/test/support"; -import { getTestDatabase, getTestServer } from "@server/test/support"; - -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#integrations.update", () => { it("should allow updating integration events", async () => { const team = await buildTeam(); diff --git a/server/routes/api/middlewares/pagination.test.ts b/server/routes/api/middlewares/pagination.test.ts index 08c5667dd..4ee8b685b 100644 --- a/server/routes/api/middlewares/pagination.test.ts +++ b/server/routes/api/middlewares/pagination.test.ts @@ -1,12 +1,7 @@ -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#pagination", () => { it("should allow offset and limit", async () => { const { user } = await seed(); diff --git a/server/routes/api/revisions.test.ts b/server/routes/api/revisions.test.ts index 7d0b36648..521848131 100644 --- a/server/routes/api/revisions.test.ts +++ b/server/routes/api/revisions.test.ts @@ -1,14 +1,9 @@ import { Revision } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#revisions.info", () => { it("should return a document revision", async () => { const { user, document } = await seed(); diff --git a/server/routes/api/shares.test.ts b/server/routes/api/shares.test.ts index 747003bdf..f551217ef 100644 --- a/server/routes/api/shares.test.ts +++ b/server/routes/api/shares.test.ts @@ -8,15 +8,10 @@ import { buildCollection, } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#shares.list", () => { it("should only return shares created by user", async () => { const { user, admin, document } = await seed(); diff --git a/server/routes/api/stars.test.ts b/server/routes/api/stars.test.ts index bececcbd2..d5a5dc1bd 100644 --- a/server/routes/api/stars.test.ts +++ b/server/routes/api/stars.test.ts @@ -1,13 +1,8 @@ import { buildUser, buildStar, buildDocument } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#stars.create", () => { it("should create a star", async () => { const user = await buildUser(); diff --git a/server/routes/api/subscriptions.test.ts b/server/routes/api/subscriptions.test.ts index fc4cc9bd8..c4bfb42b4 100644 --- a/server/routes/api/subscriptions.test.ts +++ b/server/routes/api/subscriptions.test.ts @@ -4,15 +4,10 @@ import { buildSubscription, buildDocument, } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#subscriptions.create", () => { it("should create a subscription", async () => { const user = await buildUser(); diff --git a/server/routes/api/team.test.ts b/server/routes/api/team.test.ts index dfa9d7343..57118f7f0 100644 --- a/server/routes/api/team.test.ts +++ b/server/routes/api/team.test.ts @@ -1,14 +1,9 @@ import { TeamDomain } from "@server/models"; import { buildAdmin, buildCollection, buildTeam } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#team.update", () => { it("should update team details", async () => { const { admin } = await seed(); diff --git a/server/routes/api/users.test.ts b/server/routes/api/users.test.ts index b5d87c64f..e5cb30374 100644 --- a/server/routes/api/users.test.ts +++ b/server/routes/api/users.test.ts @@ -1,7 +1,6 @@ import { buildTeam, buildAdmin, buildUser } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); beforeAll(() => { @@ -9,11 +8,8 @@ beforeAll(() => { }); afterAll(() => { jest.useRealTimers(); - server.disconnect(); }); -beforeEach(db.flush); - describe("#users.list", () => { it("should allow filtering by user name", async () => { const user = await buildUser({ diff --git a/server/routes/api/views.test.ts b/server/routes/api/views.test.ts index bed41ebcb..3fd81f072 100644 --- a/server/routes/api/views.test.ts +++ b/server/routes/api/views.test.ts @@ -1,15 +1,10 @@ import { CollectionPermission } from "@shared/types"; import { View, CollectionUser } from "@server/models"; import { buildUser } from "@server/test/factories"; -import { seed, getTestDatabase, getTestServer } from "@server/test/support"; +import { seed, getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("#views.list", () => { it("should return views for a document", async () => { const { user, document } = await seed(); diff --git a/server/routes/auth/index.test.ts b/server/routes/auth/index.test.ts index 9cc208139..fef9f124b 100644 --- a/server/routes/auth/index.test.ts +++ b/server/routes/auth/index.test.ts @@ -1,13 +1,8 @@ import { buildUser, buildCollection } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("auth/redirect", () => { it("should redirect to home", async () => { const user = await buildUser(); diff --git a/server/routes/auth/providers/email.test.ts b/server/routes/auth/providers/email.test.ts index b4391c447..f659a8fd4 100644 --- a/server/routes/auth/providers/email.test.ts +++ b/server/routes/auth/providers/email.test.ts @@ -3,15 +3,10 @@ import SigninEmail from "@server/emails/templates/SigninEmail"; import WelcomeEmail from "@server/emails/templates/WelcomeEmail"; import env from "@server/env"; import { buildUser, buildGuestUser, buildTeam } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("email", () => { it("should require email param", async () => { const res = await server.post("/auth/email", { diff --git a/server/routes/index.test.ts b/server/routes/index.test.ts index abd9a037c..865c71349 100644 --- a/server/routes/index.test.ts +++ b/server/routes/index.test.ts @@ -1,13 +1,8 @@ import { buildShare, buildDocument } from "@server/test/factories"; -import { getTestDatabase, getTestServer } from "@server/test/support"; +import { getTestServer } from "@server/test/support"; -const db = getTestDatabase(); const server = getTestServer(); -afterAll(server.disconnect); - -beforeEach(db.flush); - describe("/s/:id", () => { it("should return standard title in html when loading unpublished share", async () => { const share = await buildShare({ diff --git a/server/scripts/20210716000000-backfill-revisions.test.ts b/server/scripts/20210716000000-backfill-revisions.test.ts index 1bde5b58a..675be3082 100644 --- a/server/scripts/20210716000000-backfill-revisions.test.ts +++ b/server/scripts/20210716000000-backfill-revisions.test.ts @@ -1,13 +1,9 @@ import { Revision, Event } from "@server/models"; import { buildDocument } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import script from "./20210716000000-backfill-revisions"; -const db = getTestDatabase(); - -afterAll(db.disconnect); - -beforeEach(db.flush); +setupTestDatabase(); describe("#work", () => { it("should create events for revisions", async () => { diff --git a/server/scripts/20220722000000-backfill-subscriptions.test.ts b/server/scripts/20220722000000-backfill-subscriptions.test.ts index a157a2da0..c73e8d5ec 100644 --- a/server/scripts/20220722000000-backfill-subscriptions.test.ts +++ b/server/scripts/20220722000000-backfill-subscriptions.test.ts @@ -1,12 +1,9 @@ import { Subscription } from "@server/models"; import { buildDocument, buildUser } from "@server/test/factories"; -import { getTestDatabase } from "@server/test/support"; +import { setupTestDatabase } from "@server/test/support"; import script from "./20220722000000-backfill-subscriptions"; -const db = getTestDatabase(); - -beforeEach(db.flush); -afterAll(db.disconnect); +setupTestDatabase(); describe("#work", () => { it("should create subscriptions and subscriptions for document creator and collaborators", async () => { diff --git a/server/test/support.ts b/server/test/support.ts index e75dcfdb0..d4fa10d31 100644 --- a/server/test/support.ts +++ b/server/test/support.ts @@ -1,12 +1,12 @@ import TestServer from "fetch-test-server"; import { v4 as uuidv4 } from "uuid"; import { CollectionPermission } from "@shared/types"; -import { sequelize as db } from "@server/database/sequelize"; +import { sequelize } from "@server/database/sequelize"; import { User, Document, Collection, Team } from "@server/models"; import webService from "@server/services/web"; export const seed = async () => { - return db.transaction(async (transaction) => { + return sequelize.transaction(async (transaction) => { const team = await Team.create( { name: "Team", @@ -105,30 +105,34 @@ export function getTestServer() { const server = new TestServer(app.callback()); server.disconnect = async () => { - await db.close(); + await sequelize.close(); server.close(); }; + setupTestDatabase(); + afterAll(server.disconnect); + return server; } -export function getTestDatabase() { +export function setupTestDatabase() { const flush = async () => { - const sql = db.getQueryInterface(); - const tables = Object.keys(db.models).map((model) => { - const n = db.models[model].getTableName(); + const sql = sequelize.getQueryInterface(); + const tables = Object.keys(sequelize.models).map((model) => { + const n = sequelize.models[model].getTableName(); return (sql.queryGenerator as any).quoteTable( typeof n === "string" ? n : n.tableName ); }); const flushQuery = `TRUNCATE ${tables.join(", ")} CASCADE`; - await db.query(flushQuery); + await sequelize.query(flushQuery); }; const disconnect = async () => { - await db.close(); + await sequelize.close(); }; - return { flush, disconnect }; + afterAll(disconnect); + beforeEach(flush); }