From 33f162800001fd9d14bb921488ebc322ee30110c Mon Sep 17 00:00:00 2001 From: Adria Navarro Redo Date: Thu, 26 Jan 2023 14:18:56 +0000 Subject: [PATCH] Fix issues with parallel tests and self/cloud modes --- packages/server/jest.config.ts | 24 +++++++++++++++---- packages/server/package.json | 1 + .../src/api/routes/tests/backup.spec.ts | 7 +++--- .../{cloud.spec.ts => cloud.seq.spec.ts} | 22 +++++++---------- .../server/src/api/routes/tests/query.spec.js | 1 + .../routes/tests/utilities/TestFunctions.ts | 20 ++++++++++++++++ .../src/tests/utilities/TestConfiguration.ts | 22 +++++++++++++++++ packages/server/yarn.lock | 5 ++++ 8 files changed, 81 insertions(+), 21 deletions(-) rename packages/server/src/api/routes/tests/{cloud.spec.ts => cloud.seq.spec.ts} (77%) diff --git a/packages/server/jest.config.ts b/packages/server/jest.config.ts index 7d5c202f15..a55044a3dd 100644 --- a/packages/server/jest.config.ts +++ b/packages/server/jest.config.ts @@ -1,8 +1,8 @@ -import { Config } from "@jest/types" +import { Config } from "jest" import * as fs from "fs" const preset = require("ts-jest/jest-preset") -const config: Config.InitialOptions = { +const configSettings = { ...preset, preset: "@trendyol/jest-testcontainers", setupFiles: ["./src/tests/jestEnv.ts"], @@ -20,18 +20,34 @@ const config: Config.InitialOptions = { if (!process.env.CI) { // use sources when not in CI - config.moduleNameMapper = { + configSettings.moduleNameMapper = { "@budibase/backend-core/(.*)": "/../backend-core/$1", "@budibase/backend-core": "/../backend-core/src", "@budibase/types": "/../types/src", } // add pro sources if they exist if (fs.existsSync("../../../budibase-pro")) { - config.moduleNameMapper["@budibase/pro"] = + configSettings.moduleNameMapper["@budibase/pro"] = "/../../../budibase-pro/packages/pro/src" } } else { console.log("Running tests with compiled dependency sources") } +const config: Config = { + projects: [ + { + ...configSettings, + displayName: "sequential test", + testMatch: ["/**/*.seq.spec.[jt]s"], + runner: "jest-serial-runner", + }, + { + ...configSettings, + displayName: "parallel test", + testMatch: ["/**/!(*.seq).spec.[jt]s"], + }, + ], +} + export default config diff --git a/packages/server/package.json b/packages/server/package.json index 95c6ac6250..90ba4bb482 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -153,6 +153,7 @@ "is-wsl": "2.2.0", "jest": "28.1.1", "jest-openapi": "0.14.2", + "jest-serial-runner": "^1.2.1", "nodemon": "2.0.15", "openapi-types": "9.3.1", "openapi-typescript": "5.2.0", diff --git a/packages/server/src/api/routes/tests/backup.spec.ts b/packages/server/src/api/routes/tests/backup.spec.ts index 7ab4f2b531..7b325c080d 100644 --- a/packages/server/src/api/routes/tests/backup.spec.ts +++ b/packages/server/src/api/routes/tests/backup.spec.ts @@ -2,6 +2,7 @@ import * as setup from "./utilities" import { events } from "@budibase/backend-core" import sdk from "../../../sdk" import { checkBuilderEndpoint } from "./utilities/TestFunctions" + describe("/backups", () => { let request = setup.getRequest() let config = setup.getConfig() @@ -34,9 +35,9 @@ describe("/backups", () => { describe("calculateBackupStats", () => { it("should be able to calculate the backup statistics", async () => { - config.createAutomation() - config.createScreen() - let res = await sdk.backups.calculateBackupStats(config.getAppId()) + await config.createAutomation() + await config.createScreen() + let res = await sdk.backups.calculateBackupStats(config.getAppId()!) expect(res.automations).toEqual(1) expect(res.datasources).toEqual(1) expect(res.screens).toEqual(1) diff --git a/packages/server/src/api/routes/tests/cloud.spec.ts b/packages/server/src/api/routes/tests/cloud.seq.spec.ts similarity index 77% rename from packages/server/src/api/routes/tests/cloud.spec.ts rename to packages/server/src/api/routes/tests/cloud.seq.spec.ts index f52bf838ff..3ed9241a51 100644 --- a/packages/server/src/api/routes/tests/cloud.spec.ts +++ b/packages/server/src/api/routes/tests/cloud.seq.spec.ts @@ -1,14 +1,20 @@ -import { db as dbCore } from "@budibase/backend-core" import { AppStatus } from "../../../db/utils" import * as setup from "./utilities" +import { wipeDb } from "./utilities/TestFunctions" + describe("/cloud", () => { let request = setup.getRequest() let config = setup.getConfig() afterAll(setup.afterAll) + beforeAll(() => { + // Importing is only allowed in self hosted environments + config.modeSelf() + }) + beforeEach(async () => { await config.init() }) @@ -22,19 +28,7 @@ describe("/cloud", () => { it("should be able to import apps", async () => { // first we need to delete any existing apps on the system so it looks clean otherwise the // import will not run - await request - .post( - `/api/applications/${dbCore.getProdAppID( - config.getAppId() - )}/unpublish` - ) - .set(config.defaultHeaders()) - .expect(204) - await request - .delete(`/api/applications/${config.getAppId()}`) - .set(config.defaultHeaders()) - .expect("Content-Type", /json/) - .expect(200) + await wipeDb() // get a count of apps before the import const preImportApps = await request diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index f0cfdaa76f..6db6ea69cf 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -31,6 +31,7 @@ describe("/queries", () => { afterAll(setup.afterAll) beforeEach(async () => { + config.modeSelf() await config.init() datasource = await config.createDatasource() query = await config.createQuery() diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts index 1c7998332d..ad65c705ac 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts @@ -4,6 +4,8 @@ import { AppStatus } from "../../../../db/utils" import { roles, tenancy, context } from "@budibase/backend-core" import { TENANT_ID } from "../../../../tests/utilities/structures" import env from "../../../../environment" +import { db } from "@budibase/backend-core" +import Nano from "@budibase/nano" class Request { appId: any @@ -55,6 +57,24 @@ export const clearAllAutomations = async (config: any) => { } } +export const wipeDb = async () => { + const couchInfo = db.getCouchInfo() + const nano = Nano({ + url: couchInfo.url, + requestDefaults: { + headers: { + Authorization: couchInfo.cookie, + }, + }, + parseUrl: false, + }) + let dbs + do { + dbs = await nano.db.list() + await Promise.all(dbs.map(x => nano.db.destroy(x))) + } while (dbs.length) +} + export const createRequest = ( request: any, method: any, diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 15f090035a..d1dba3d2f3 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -11,6 +11,7 @@ mocks.licenses.useUnlimited() import { init as dbInit } from "../../db" dbInit() import env from "../../environment" +import { env as coreEnv } from "@budibase/backend-core" import { basicTable, basicRow, @@ -175,6 +176,27 @@ class TestConfiguration { } } + // MODES + setMultiTenancy = (value: boolean) => { + env._set("MULTI_TENANCY", value) + coreEnv._set("MULTI_TENANCY", value) + } + + setSelfHosted = (value: boolean) => { + env._set("SELF_HOSTED", value) + coreEnv._set("SELF_HOSTED", value) + } + + modeCloud = () => { + this.setSelfHosted(false) + this.setMultiTenancy(true) + } + + modeSelf = () => { + this.setSelfHosted(true) + this.setMultiTenancy(false) + } + // UTILS async _req(body: any, params: any, controlFunc: any) { diff --git a/packages/server/yarn.lock b/packages/server/yarn.lock index 142bdca634..2bf20c70af 100644 --- a/packages/server/yarn.lock +++ b/packages/server/yarn.lock @@ -9393,6 +9393,11 @@ jest-runtime@^28.1.3: slash "^3.0.0" strip-bom "^4.0.0" +jest-serial-runner@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/jest-serial-runner/-/jest-serial-runner-1.2.1.tgz#0f5f8dbe6f077119bd1fdd7e8518f92353c194d5" + integrity sha512-d59fF+7HdjNvQEL7B4WyFE+f8q5tGzlNUqtOnxTrT1ofun7O6/Lgm/j255BBgCY2fmSue/34M7Xy9+VWRByP0Q== + jest-serializer@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-serializer/-/jest-serializer-24.9.0.tgz#e6d7d7ef96d31e8b9079a714754c5d5c58288e73"