chore: Reduce test boilerplate (#4300)

* chore: Reduce test boilerplate

* mo
This commit is contained in:
Tom Moor
2022-10-15 22:40:21 -04:00
committed by GitHub
parent 1915a453db
commit 1fbc000e03
72 changed files with 134 additions and 439 deletions

View File

@@ -4,14 +4,10 @@ import { TeamDomain } from "@server/models";
import Collection from "@server/models/Collection"; import Collection from "@server/models/Collection";
import UserAuthentication from "@server/models/UserAuthentication"; import UserAuthentication from "@server/models/UserAuthentication";
import { buildUser, buildTeam } from "@server/test/factories"; 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"; import accountProvisioner from "./accountProvisioner";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("accountProvisioner", () => { describe("accountProvisioner", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -2,16 +2,12 @@ import path from "path";
import fs from "fs-extra"; import fs from "fs-extra";
import Attachment from "@server/models/Attachment"; import Attachment from "@server/models/Attachment";
import { buildUser } from "@server/test/factories"; import { buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import documentImporter from "./documentImporter"; import documentImporter from "./documentImporter";
jest.mock("../utils/s3"); jest.mock("../utils/s3");
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentImporter", () => { describe("documentImporter", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -5,14 +5,10 @@ import {
buildCollection, buildCollection,
buildUser, buildUser,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase, seed } from "@server/test/support"; import { setupTestDatabase, seed } from "@server/test/support";
import documentMover from "./documentMover"; import documentMover from "./documentMover";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentMover", () => { describe("documentMover", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,14 +1,10 @@
import { subDays } from "date-fns"; import { subDays } from "date-fns";
import { Attachment, Document } from "@server/models"; import { Attachment, Document } from "@server/models";
import { buildAttachment, buildDocument } from "@server/test/factories"; import { buildAttachment, buildDocument } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import documentPermanentDeleter from "./documentPermanentDeleter"; import documentPermanentDeleter from "./documentPermanentDeleter";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentPermanentDeleter", () => { describe("documentPermanentDeleter", () => {
it("should destroy documents", async () => { it("should destroy documents", async () => {

View File

@@ -1,14 +1,10 @@
import { sequelize } from "@server/database/sequelize"; import { sequelize } from "@server/database/sequelize";
import { Event } from "@server/models"; import { Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import documentUpdater from "./documentUpdater"; import documentUpdater from "./documentUpdater";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentUpdater", () => { describe("documentUpdater", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,13 +1,9 @@
import { FileOperation } from "@server/models"; import { FileOperation } from "@server/models";
import { buildAdmin, buildFileOperation } from "@server/test/factories"; import { buildAdmin, buildFileOperation } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import fileOperationDeleter from "./fileOperationDeleter"; import fileOperationDeleter from "./fileOperationDeleter";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("fileOperationDeleter", () => { describe("fileOperationDeleter", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,13 +1,9 @@
import { Event } from "@server/models"; import { Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import pinCreator from "./pinCreator"; import pinCreator from "./pinCreator";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("pinCreator", () => { describe("pinCreator", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,13 +1,9 @@
import { Pin, Event } from "@server/models"; import { Pin, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import pinDestroyer from "./pinDestroyer"; import pinDestroyer from "./pinDestroyer";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("pinCreator", () => { describe("pinCreator", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,13 +1,9 @@
import { Event } from "@server/models"; import { Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import revisionCreator from "./revisionCreator"; import revisionCreator from "./revisionCreator";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("revisionCreator", () => { describe("revisionCreator", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,14 +1,10 @@
import { sequelize } from "@server/database/sequelize"; import { sequelize } from "@server/database/sequelize";
import { Star, Event } from "@server/models"; import { Star, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import starCreator from "./starCreator"; import starCreator from "./starCreator";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("starCreator", () => { describe("starCreator", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,13 +1,9 @@
import { Star, Event } from "@server/models"; import { Star, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import starDestroyer from "./starDestroyer"; import starDestroyer from "./starDestroyer";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("starDestroyer", () => { describe("starDestroyer", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,13 +1,9 @@
import { Star, Event } from "@server/models"; import { Star, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import starUpdater from "./starUpdater"; import starUpdater from "./starUpdater";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("starUpdater", () => { describe("starUpdater", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,14 +1,11 @@
import { sequelize } from "@server/database/sequelize"; import { sequelize } from "@server/database/sequelize";
import { Subscription, Event } from "@server/models"; import { Subscription, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; import { buildDocument, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import subscriptionCreator from "./subscriptionCreator"; import subscriptionCreator from "./subscriptionCreator";
import subscriptionDestroyer from "./subscriptionDestroyer"; import subscriptionDestroyer from "./subscriptionDestroyer";
const db = getTestDatabase(); setupTestDatabase();
beforeEach(db.flush);
afterAll(db.disconnect);
describe("subscriptionCreator", () => { describe("subscriptionCreator", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -5,13 +5,10 @@ import {
buildSubscription, buildSubscription,
buildUser, buildUser,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import subscriptionDestroyer from "./subscriptionDestroyer"; import subscriptionDestroyer from "./subscriptionDestroyer";
const db = getTestDatabase(); setupTestDatabase();
beforeEach(db.flush);
afterAll(db.disconnect);
describe("subscriptionDestroyer", () => { describe("subscriptionDestroyer", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -6,14 +6,10 @@ import {
buildTeam, buildTeam,
buildDocument, buildDocument,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import teamPermanentDeleter from "./teamPermanentDeleter"; import teamPermanentDeleter from "./teamPermanentDeleter";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("teamPermanentDeleter", () => { describe("teamPermanentDeleter", () => {
it("should destroy related data", async () => { it("should destroy related data", async () => {

View File

@@ -1,14 +1,10 @@
import env from "@server/env"; import env from "@server/env";
import TeamDomain from "@server/models/TeamDomain"; import TeamDomain from "@server/models/TeamDomain";
import { buildTeam, buildUser } from "@server/test/factories"; import { buildTeam, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import teamProvisioner from "./teamProvisioner"; import teamProvisioner from "./teamProvisioner";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("teamProvisioner", () => { describe("teamProvisioner", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -2,14 +2,10 @@ import { CollectionPermission } from "@shared/types";
import { CollectionUser } from "@server/models"; import { CollectionUser } from "@server/models";
import { UserRole } from "@server/models/User"; import { UserRole } from "@server/models/User";
import { buildUser, buildAdmin, buildCollection } from "@server/test/factories"; import { buildUser, buildAdmin, buildCollection } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import userDemoter from "./userDemoter"; import userDemoter from "./userDemoter";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userDemoter", () => { describe("userDemoter", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,12 +1,8 @@
import { buildUser, buildAdmin } from "@server/test/factories"; import { buildUser, buildAdmin } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import userDestroyer from "./userDestroyer"; import userDestroyer from "./userDestroyer";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userDestroyer", () => { describe("userDestroyer", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,12 +1,8 @@
import { buildUser } from "@server/test/factories"; import { buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import userInviter from "./userInviter"; import userInviter from "./userInviter";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userInviter", () => { describe("userInviter", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,14 +1,10 @@
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { TeamDomain } from "@server/models"; import { TeamDomain } from "@server/models";
import { buildUser, buildTeam, buildInvite } from "@server/test/factories"; 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"; import userProvisioner from "./userProvisioner";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userProvisioner", () => { describe("userProvisioner", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,13 +1,9 @@
import GroupUser from "@server/models/GroupUser"; import GroupUser from "@server/models/GroupUser";
import { buildGroup, buildAdmin, buildUser } from "@server/test/factories"; import { buildGroup, buildAdmin, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import userSuspender from "./userSuspender"; import userSuspender from "./userSuspender";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userSuspender", () => { describe("userSuspender", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,12 +1,8 @@
import { buildAdmin, buildUser } from "@server/test/factories"; import { buildAdmin, buildUser } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import userUnsuspender from "./userUnsuspender"; import userUnsuspender from "./userUnsuspender";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userUnsuspender", () => { describe("userUnsuspender", () => {
const ip = "127.0.0.1"; const ip = "127.0.0.1";

View File

@@ -1,14 +1,10 @@
import randomstring from "randomstring"; import randomstring from "randomstring";
import ApiKey from "@server/models/ApiKey"; import ApiKey from "@server/models/ApiKey";
import { buildUser, buildTeam } from "@server/test/factories"; import { buildUser, buildTeam } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import auth from "./authentication"; import auth from "./authentication";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("Authentication middleware", () => { describe("Authentication middleware", () => {
describe("with JWT", () => { describe("with JWT", () => {

View File

@@ -7,17 +7,14 @@ import {
buildTeam, buildTeam,
buildDocument, buildDocument,
} from "@server/test/factories"; } 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 slugify from "@server/utils/slugify";
import Collection from "./Collection"; import Collection from "./Collection";
import Document from "./Document"; import Document from "./Document";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect); beforeEach(() => {
beforeEach(async () => {
await db.flush();
jest.resetAllMocks(); jest.resetAllMocks();
}); });

View File

@@ -6,15 +6,12 @@ import {
buildUser, buildUser,
buildShare, buildShare,
} from "@server/test/factories"; } 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 slugify from "@server/utils/slugify";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect); beforeEach(() => {
beforeEach(async () => {
await db.flush();
jest.resetAllMocks(); jest.resetAllMocks();
}); });

View File

@@ -1,14 +1,11 @@
import { buildUser, buildGroup, buildCollection } from "@server/test/factories"; 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 CollectionGroup from "./CollectionGroup";
import GroupUser from "./GroupUser"; import GroupUser from "./GroupUser";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => { beforeEach(async () => {
await db.flush();
jest.resetAllMocks(); jest.resetAllMocks();
}); });

View File

@@ -1,15 +1,8 @@
import { buildDocument } from "@server/test/factories"; import { buildDocument } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import Revision from "./Revision"; import Revision from "./Revision";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("#findLatest", () => { describe("#findLatest", () => {
test("should return latest revision", async () => { test("should return latest revision", async () => {

View File

@@ -1,14 +1,7 @@
import { buildTeam, buildCollection } from "@server/test/factories"; import { buildTeam, buildCollection } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("collectionIds", () => { describe("collectionIds", () => {
it("should return non-private collection ids", async () => { it("should return non-private collection ids", async () => {

View File

@@ -1,16 +1,9 @@
import env from "@server/env"; import env from "@server/env";
import { buildAdmin, buildTeam } from "@server/test/factories"; import { buildAdmin, buildTeam } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import TeamDomain from "./TeamDomain"; import TeamDomain from "./TeamDomain";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("team domain model", () => { describe("team domain model", () => {
describe("create", () => { describe("create", () => {

View File

@@ -1,10 +1,10 @@
import { CollectionPermission } from "@shared/types"; import { CollectionPermission } from "@shared/types";
import { buildUser, buildTeam, buildCollection } from "@server/test/factories"; 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 CollectionUser from "./CollectionUser";
import UserAuthentication from "./UserAuthentication"; import UserAuthentication from "./UserAuthentication";
const db = getTestDatabase(); setupTestDatabase();
beforeAll(() => { beforeAll(() => {
jest.useFakeTimers().setSystemTime(new Date("2018-01-02T00:00:00.000Z")); jest.useFakeTimers().setSystemTime(new Date("2018-01-02T00:00:00.000Z"));
@@ -12,11 +12,8 @@ beforeAll(() => {
afterAll(() => { afterAll(() => {
jest.useRealTimers(); jest.useRealTimers();
db.disconnect();
}); });
beforeEach(db.flush);
describe("user model", () => { describe("user model", () => {
describe("destroy", () => { describe("destroy", () => {
it("should delete user authentications", async () => { it("should delete user authentications", async () => {

View File

@@ -1,14 +1,10 @@
import { CollectionPermission } from "@shared/types"; import { CollectionPermission } from "@shared/types";
import { CollectionUser, Collection } from "@server/models"; import { CollectionUser, Collection } from "@server/models";
import { buildUser, buildTeam, buildCollection } from "@server/test/factories"; import { buildUser, buildTeam, buildCollection } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import { serialize } from "./index"; import { serialize } from "./index";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("member", () => { describe("member", () => {
describe("read_write permission", () => { describe("read_write permission", () => {

View File

@@ -5,14 +5,10 @@ import {
buildDocument, buildDocument,
buildCollection, buildCollection,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import { serialize } from "./index"; import { serialize } from "./index";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("read_write collection", () => { describe("read_write collection", () => {
it("should allow read write permissions for member", async () => { it("should allow read write permissions for member", async () => {

View File

@@ -1,12 +1,8 @@
import { buildUser, buildTeam } from "@server/test/factories"; import { buildUser, buildTeam } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import { serialize } from "./index"; import { serialize } from "./index";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
it("should serialize policy", async () => { it("should serialize policy", async () => {
const user = await buildUser(); const user = await buildUser();

View File

@@ -1,12 +1,8 @@
import { buildUser, buildTeam, buildAdmin } from "@server/test/factories"; import { buildUser, buildTeam, buildAdmin } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import { serialize } from "./index"; import { serialize } from "./index";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
it("should allow reading only", async () => { it("should allow reading only", async () => {
const team = await buildTeam(); const team = await buildTeam();

View File

@@ -1,18 +1,11 @@
import { Backlink } from "@server/models"; import { Backlink } from "@server/models";
import { buildDocument } from "@server/test/factories"; import { buildDocument } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import BacklinksProcessor from "./BacklinksProcessor"; import BacklinksProcessor from "./BacklinksProcessor";
const ip = "127.0.0.1"; const ip = "127.0.0.1";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("documents.publish", () => { describe("documents.publish", () => {
test("should create new backlink records", async () => { test("should create new backlink records", async () => {

View File

@@ -12,18 +12,15 @@ import {
buildCollection, buildCollection,
buildUser, buildUser,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import NotificationsProcessor from "./NotificationsProcessor"; import NotificationsProcessor from "./NotificationsProcessor";
jest.mock("@server/emails/templates/DocumentNotificationEmail"); jest.mock("@server/emails/templates/DocumentNotificationEmail");
const ip = "127.0.0.1"; const ip = "127.0.0.1";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => { beforeEach(async () => {
await db.flush();
jest.resetAllMocks(); jest.resetAllMocks();
}); });

View File

@@ -1,18 +1,11 @@
import { Revision } from "@server/models"; import { Revision } from "@server/models";
import { buildDocument } from "@server/test/factories"; import { buildDocument } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import RevisionsProcessor from "./RevisionsProcessor"; import RevisionsProcessor from "./RevisionsProcessor";
const ip = "127.0.0.1"; const ip = "127.0.0.1";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("documents.update.debounced", () => { describe("documents.update.debounced", () => {
test("should create a revision", async () => { test("should create a revision", async () => {

View File

@@ -1,5 +1,5 @@
import { buildUser, buildWebhookSubscription } from "@server/test/factories"; 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 { UserEvent } from "@server/types";
import DeliverWebhookTask from "../tasks/DeliverWebhookTask"; import DeliverWebhookTask from "../tasks/DeliverWebhookTask";
import WebhookProcessor from "./WebhookProcessor"; import WebhookProcessor from "./WebhookProcessor";
@@ -7,12 +7,9 @@ import WebhookProcessor from "./WebhookProcessor";
jest.mock("@server/queues/tasks/DeliverWebhookTask"); jest.mock("@server/queues/tasks/DeliverWebhookTask");
const ip = "127.0.0.1"; const ip = "127.0.0.1";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => { beforeEach(async () => {
await db.flush();
jest.resetAllMocks(); jest.resetAllMocks();
}); });

View File

@@ -1,14 +1,10 @@
import { subDays } from "date-fns"; import { subDays } from "date-fns";
import { Document } from "@server/models"; import { Document } from "@server/models";
import { buildDocument } from "@server/test/factories"; import { buildDocument } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import CleanupDeletedDocumentsTask from "./CleanupDeletedDocumentsTask"; import CleanupDeletedDocumentsTask from "./CleanupDeletedDocumentsTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupDeletedDocumentsTask", () => { describe("CleanupDeletedDocumentsTask", () => {
it("should not destroy documents not deleted", async () => { it("should not destroy documents not deleted", async () => {

View File

@@ -6,14 +6,10 @@ import {
buildWebhookSubscription, buildWebhookSubscription,
buildViewer, buildViewer,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import CleanupDemotedUserTask from "./CleanupDemotedUserTask"; import CleanupDemotedUserTask from "./CleanupDemotedUserTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupDemotedUserTask", () => { describe("CleanupDemotedUserTask", () => {
it("should delete api keys for suspended user", async () => { it("should delete api keys for suspended user", async () => {

View File

@@ -5,14 +5,10 @@ import {
FileOperationType, FileOperationType,
} from "@server/models/FileOperation"; } from "@server/models/FileOperation";
import { buildFileOperation } from "@server/test/factories"; import { buildFileOperation } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import CleanupExpiredFileOperationsTask from "./CleanupExpiredFileOperationsTask"; import CleanupExpiredFileOperationsTask from "./CleanupExpiredFileOperationsTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupExpiredFileOperationsTask", () => { describe("CleanupExpiredFileOperationsTask", () => {
it("should expire exports older than 15 days ago", async () => { it("should expire exports older than 15 days ago", async () => {

View File

@@ -1,14 +1,10 @@
import { subDays } from "date-fns"; import { subDays } from "date-fns";
import { WebhookDelivery } from "@server/models"; import { WebhookDelivery } from "@server/models";
import { buildWebhookDelivery } from "@server/test/factories"; import { buildWebhookDelivery } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import CleanupWebhookDeliveriesTask from "./CleanupWebhookDeliveriesTask"; import CleanupWebhookDeliveriesTask from "./CleanupWebhookDeliveriesTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
const deliveryExists = async (delivery: WebhookDelivery) => { const deliveryExists = async (delivery: WebhookDelivery) => {
const results = await WebhookDelivery.findOne({ where: { id: delivery.id } }); const results = await WebhookDelivery.findOne({ where: { id: delivery.id } });

View File

@@ -6,16 +6,13 @@ import {
buildWebhookDelivery, buildWebhookDelivery,
buildWebhookSubscription, buildWebhookSubscription,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import { UserEvent } from "@server/types"; import { UserEvent } from "@server/types";
import DeliverWebhookTask from "./DeliverWebhookTask"; import DeliverWebhookTask from "./DeliverWebhookTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => { beforeEach(async () => {
await db.flush();
jest.resetAllMocks(); jest.resetAllMocks();
fetchMock.resetMocks(); fetchMock.resetMocks();
fetchMock.doMock(); fetchMock.doMock();

View File

@@ -2,14 +2,10 @@ import fs from "fs";
import path from "path"; import path from "path";
import { FileOperation } from "@server/models"; import { FileOperation } from "@server/models";
import { buildFileOperation } from "@server/test/factories"; import { buildFileOperation } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import ImportMarkdownZipTask from "./ImportMarkdownZipTask"; import ImportMarkdownZipTask from "./ImportMarkdownZipTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("ImportMarkdownZipTask", () => { describe("ImportMarkdownZipTask", () => {
it("should import the documents, attachments", async () => { it("should import the documents, attachments", async () => {

View File

@@ -2,14 +2,10 @@ import fs from "fs";
import path from "path"; import path from "path";
import { FileOperation } from "@server/models"; import { FileOperation } from "@server/models";
import { buildFileOperation } from "@server/test/factories"; import { buildFileOperation } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import ImportNotionTask from "./ImportNotionTask"; import ImportNotionTask from "./ImportNotionTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("ImportNotionTask", () => { describe("ImportNotionTask", () => {
it("should import successfully from a Markdown export", async () => { it("should import successfully from a Markdown export", async () => {

View File

@@ -1,14 +1,10 @@
import { subDays } from "date-fns"; import { subDays } from "date-fns";
import InviteReminderEmail from "@server/emails/templates/InviteReminderEmail"; import InviteReminderEmail from "@server/emails/templates/InviteReminderEmail";
import { buildInvite } from "@server/test/factories"; import { buildInvite } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import InviteReminderTask from "./InviteReminderTask"; import InviteReminderTask from "./InviteReminderTask";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("InviteReminderTask", () => { describe("InviteReminderTask", () => {
it("should not destroy documents not deleted", async () => { it("should not destroy documents not deleted", async () => {

View File

@@ -6,17 +6,12 @@ import {
buildAttachment, buildAttachment,
buildDocument, buildDocument,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase, getTestServer } from "@server/test/support"; import { getTestServer } from "@server/test/support";
jest.mock("@server/utils/s3"); jest.mock("@server/utils/s3");
const db = getTestDatabase();
const server = getTestServer(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#attachments.create", () => { describe("#attachments.create", () => {
it("should require authentication", async () => { it("should require authentication", async () => {
const res = await server.post("/api/attachments.create"); const res = await server.post("/api/attachments.create");

View File

@@ -1,7 +1,7 @@
import sharedEnv from "@shared/env"; import sharedEnv from "@shared/env";
import env from "@server/env"; import env from "@server/env";
import { buildUser, buildTeam } from "@server/test/factories"; 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"; const mockTeamInSessionId = "1e023d05-951c-41c6-9012-c9fa0402e1c3";
@@ -13,13 +13,8 @@ jest.mock("@server/utils/authentication", () => {
}; };
}); });
const db = getTestDatabase();
const server = getTestServer(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#auth.info", () => { describe("#auth.info", () => {
it("should return current authentication", async () => { it("should return current authentication", async () => {
const team = await buildTeam(); const team = await buildTeam();

View File

@@ -1,14 +1,9 @@
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { buildUser, buildAdmin, buildTeam } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#authenticationProviders.info", () => { describe("#authenticationProviders.info", () => {
it("should return auth provider", async () => { it("should return auth provider", async () => {
const team = await buildTeam(); const team = await buildTeam();

View File

@@ -8,15 +8,10 @@ import {
buildCollection, buildCollection,
buildDocument, buildDocument,
} from "@server/test/factories"; } 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#collections.list", () => { describe("#collections.list", () => {
it("should require authentication", async () => { it("should require authentication", async () => {
const res = await server.post("/api/collections.list"); const res = await server.post("/api/collections.list");

View File

@@ -1,12 +1,7 @@
import { getTestDatabase, getTestServer } from "@server/test/support"; import { getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#cron.daily", () => { describe("#cron.daily", () => {
it("should require authentication", async () => { it("should require authentication", async () => {
const res = await server.post("/api/cron.daily"); const res = await server.post("/api/cron.daily");

View File

@@ -16,15 +16,10 @@ import {
buildDocument, buildDocument,
buildViewer, buildViewer,
} from "@server/test/factories"; } 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#documents.info", () => { describe("#documents.info", () => {
it("should return published document", async () => { it("should return published document", async () => {
const { user, document } = await seed(); const { user, document } = await seed();

View File

@@ -1,13 +1,8 @@
import { buildEvent, buildUser } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#events.list", () => { describe("#events.list", () => {
it("should only return activity events", async () => { it("should only return activity events", async () => {
const { user, admin, document, collection } = await seed(); const { user, admin, document, collection } = await seed();

View File

@@ -11,17 +11,12 @@ import {
buildUser, buildUser,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase, getTestServer } from "@server/test/support"; import { getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer(); const server = getTestServer();
jest.mock("@server/utils/s3"); jest.mock("@server/utils/s3");
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#fileOperations.info", () => { describe("#fileOperations.info", () => {
it("should return fileOperation", async () => { it("should return fileOperation", async () => {
const team = await buildTeam(); const team = await buildTeam();

View File

@@ -1,14 +1,9 @@
import { Event } from "@server/models"; import { Event } from "@server/models";
import { buildUser, buildAdmin, buildGroup } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#groups.create", () => { describe("#groups.create", () => {
it("should create a group", async () => { it("should create a group", async () => {
const name = "hello I am a group"; const name = "hello I am a group";

View File

@@ -1,20 +1,15 @@
import env from "@server/env"; import env from "@server/env";
import { IntegrationAuthentication, SearchQuery } from "@server/models"; import { IntegrationAuthentication, SearchQuery } from "@server/models";
import { buildDocument, buildIntegration } from "@server/test/factories"; 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"; import * as Slack from "@server/utils/slack";
jest.mock("../../utils/slack", () => ({ jest.mock("../../utils/slack", () => ({
post: jest.fn(), post: jest.fn(),
})); }));
const db = getTestDatabase();
const server = getTestServer(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#hooks.unfurl", () => { describe("#hooks.unfurl", () => {
it("should return documents", async () => { it("should return documents", async () => {
const { user, document } = await seed(); const { user, document } = await seed();

View File

@@ -2,8 +2,6 @@ import { getTestServer } from "@server/test/support";
const server = getTestServer(); const server = getTestServer();
afterAll(server.disconnect);
describe("POST unknown endpoint", () => { describe("POST unknown endpoint", () => {
it("should be not found", async () => { it("should be not found", async () => {
const res = await server.post("/api/blah"); const res = await server.post("/api/blah");

View File

@@ -4,16 +4,10 @@ import {
buildUser, buildUser,
buildIntegration, buildIntegration,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#integrations.update", () => { describe("#integrations.update", () => {
it("should allow updating integration events", async () => { it("should allow updating integration events", async () => {
const team = await buildTeam(); const team = await buildTeam();

View File

@@ -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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#pagination", () => { describe("#pagination", () => {
it("should allow offset and limit", async () => { it("should allow offset and limit", async () => {
const { user } = await seed(); const { user } = await seed();

View File

@@ -1,14 +1,9 @@
import { Revision } from "@server/models"; import { Revision } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#revisions.info", () => { describe("#revisions.info", () => {
it("should return a document revision", async () => { it("should return a document revision", async () => {
const { user, document } = await seed(); const { user, document } = await seed();

View File

@@ -8,15 +8,10 @@ import {
buildCollection, buildCollection,
} from "@server/test/factories"; } 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#shares.list", () => { describe("#shares.list", () => {
it("should only return shares created by user", async () => { it("should only return shares created by user", async () => {
const { user, admin, document } = await seed(); const { user, admin, document } = await seed();

View File

@@ -1,13 +1,8 @@
import { buildUser, buildStar, buildDocument } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#stars.create", () => { describe("#stars.create", () => {
it("should create a star", async () => { it("should create a star", async () => {
const user = await buildUser(); const user = await buildUser();

View File

@@ -4,15 +4,10 @@ import {
buildSubscription, buildSubscription,
buildDocument, buildDocument,
} from "@server/test/factories"; } from "@server/test/factories";
import { getTestDatabase, getTestServer } from "@server/test/support"; import { getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#subscriptions.create", () => { describe("#subscriptions.create", () => {
it("should create a subscription", async () => { it("should create a subscription", async () => {
const user = await buildUser(); const user = await buildUser();

View File

@@ -1,14 +1,9 @@
import { TeamDomain } from "@server/models"; import { TeamDomain } from "@server/models";
import { buildAdmin, buildCollection, buildTeam } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#team.update", () => { describe("#team.update", () => {
it("should update team details", async () => { it("should update team details", async () => {
const { admin } = await seed(); const { admin } = await seed();

View File

@@ -1,7 +1,6 @@
import { buildTeam, buildAdmin, buildUser } from "@server/test/factories"; 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(); const server = getTestServer();
beforeAll(() => { beforeAll(() => {
@@ -9,11 +8,8 @@ beforeAll(() => {
}); });
afterAll(() => { afterAll(() => {
jest.useRealTimers(); jest.useRealTimers();
server.disconnect();
}); });
beforeEach(db.flush);
describe("#users.list", () => { describe("#users.list", () => {
it("should allow filtering by user name", async () => { it("should allow filtering by user name", async () => {
const user = await buildUser({ const user = await buildUser({

View File

@@ -1,15 +1,10 @@
import { CollectionPermission } from "@shared/types"; import { CollectionPermission } from "@shared/types";
import { View, CollectionUser } from "@server/models"; import { View, CollectionUser } from "@server/models";
import { buildUser } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#views.list", () => { describe("#views.list", () => {
it("should return views for a document", async () => { it("should return views for a document", async () => {
const { user, document } = await seed(); const { user, document } = await seed();

View File

@@ -1,13 +1,8 @@
import { buildUser, buildCollection } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("auth/redirect", () => { describe("auth/redirect", () => {
it("should redirect to home", async () => { it("should redirect to home", async () => {
const user = await buildUser(); const user = await buildUser();

View File

@@ -3,15 +3,10 @@ import SigninEmail from "@server/emails/templates/SigninEmail";
import WelcomeEmail from "@server/emails/templates/WelcomeEmail"; import WelcomeEmail from "@server/emails/templates/WelcomeEmail";
import env from "@server/env"; import env from "@server/env";
import { buildUser, buildGuestUser, buildTeam } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("email", () => { describe("email", () => {
it("should require email param", async () => { it("should require email param", async () => {
const res = await server.post("/auth/email", { const res = await server.post("/auth/email", {

View File

@@ -1,13 +1,8 @@
import { buildShare, buildDocument } from "@server/test/factories"; 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(); const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("/s/:id", () => { describe("/s/:id", () => {
it("should return standard title in html when loading unpublished share", async () => { it("should return standard title in html when loading unpublished share", async () => {
const share = await buildShare({ const share = await buildShare({

View File

@@ -1,13 +1,9 @@
import { Revision, Event } from "@server/models"; import { Revision, Event } from "@server/models";
import { buildDocument } from "@server/test/factories"; import { buildDocument } from "@server/test/factories";
import { getTestDatabase } from "@server/test/support"; import { setupTestDatabase } from "@server/test/support";
import script from "./20210716000000-backfill-revisions"; import script from "./20210716000000-backfill-revisions";
const db = getTestDatabase(); setupTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("#work", () => { describe("#work", () => {
it("should create events for revisions", async () => { it("should create events for revisions", async () => {

View File

@@ -1,12 +1,9 @@
import { Subscription } from "@server/models"; import { Subscription } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories"; 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"; import script from "./20220722000000-backfill-subscriptions";
const db = getTestDatabase(); setupTestDatabase();
beforeEach(db.flush);
afterAll(db.disconnect);
describe("#work", () => { describe("#work", () => {
it("should create subscriptions and subscriptions for document creator and collaborators", async () => { it("should create subscriptions and subscriptions for document creator and collaborators", async () => {

View File

@@ -1,12 +1,12 @@
import TestServer from "fetch-test-server"; import TestServer from "fetch-test-server";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { CollectionPermission } from "@shared/types"; 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 { User, Document, Collection, Team } from "@server/models";
import webService from "@server/services/web"; import webService from "@server/services/web";
export const seed = async () => { export const seed = async () => {
return db.transaction(async (transaction) => { return sequelize.transaction(async (transaction) => {
const team = await Team.create( const team = await Team.create(
{ {
name: "Team", name: "Team",
@@ -105,30 +105,34 @@ export function getTestServer() {
const server = new TestServer(app.callback()); const server = new TestServer(app.callback());
server.disconnect = async () => { server.disconnect = async () => {
await db.close(); await sequelize.close();
server.close(); server.close();
}; };
setupTestDatabase();
afterAll(server.disconnect);
return server; return server;
} }
export function getTestDatabase() { export function setupTestDatabase() {
const flush = async () => { const flush = async () => {
const sql = db.getQueryInterface(); const sql = sequelize.getQueryInterface();
const tables = Object.keys(db.models).map((model) => { const tables = Object.keys(sequelize.models).map((model) => {
const n = db.models[model].getTableName(); const n = sequelize.models[model].getTableName();
return (sql.queryGenerator as any).quoteTable( return (sql.queryGenerator as any).quoteTable(
typeof n === "string" ? n : n.tableName typeof n === "string" ? n : n.tableName
); );
}); });
const flushQuery = `TRUNCATE ${tables.join(", ")} CASCADE`; const flushQuery = `TRUNCATE ${tables.join(", ")} CASCADE`;
await db.query(flushQuery); await sequelize.query(flushQuery);
}; };
const disconnect = async () => { const disconnect = async () => {
await db.close(); await sequelize.close();
}; };
return { flush, disconnect }; afterAll(disconnect);
beforeEach(flush);
} }