Fix issues with parallel tests and self/cloud modes

This commit is contained in:
Adria Navarro Redo 2023-01-26 14:18:56 +00:00 committed by adrinr
parent 8c1e539fc7
commit 33f1628000
8 changed files with 81 additions and 21 deletions

View File

@ -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/(.*)": "<rootDir>/../backend-core/$1",
"@budibase/backend-core": "<rootDir>/../backend-core/src",
"@budibase/types": "<rootDir>/../types/src",
}
// add pro sources if they exist
if (fs.existsSync("../../../budibase-pro")) {
config.moduleNameMapper["@budibase/pro"] =
configSettings.moduleNameMapper["@budibase/pro"] =
"<rootDir>/../../../budibase-pro/packages/pro/src"
}
} else {
console.log("Running tests with compiled dependency sources")
}
const config: Config = {
projects: [
{
...configSettings,
displayName: "sequential test",
testMatch: ["<rootDir>/**/*.seq.spec.[jt]s"],
runner: "jest-serial-runner",
},
{
...configSettings,
displayName: "parallel test",
testMatch: ["<rootDir>/**/!(*.seq).spec.[jt]s"],
},
],
}
export default config

View File

@ -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",

View File

@ -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)

View File

@ -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

View File

@ -31,6 +31,7 @@ describe("/queries", () => {
afterAll(setup.afterAll)
beforeEach(async () => {
config.modeSelf()
await config.init()
datasource = await config.createDatasource()
query = await config.createQuery()

View File

@ -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,

View File

@ -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) {

View File

@ -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"