From 6259ffad3bc363999eedc9daad615a1e4d9becda Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 24 Jan 2023 18:51:15 +0000 Subject: [PATCH] Wipe db before tests --- .../src/tests/utilities/TestConfiguration.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 35535d5aaa..b59e3591fc 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -38,6 +38,8 @@ import { cleanup } from "../../utilities/fileSystem" import newid from "../../db/newid" import { generateUserMetadataID } from "../../db/utils" import { startup } from "../../startup" +import { db } from "@budibase/backend-core" +import Nano from "@budibase/nano" const supertest = require("supertest") const GLOBAL_USER_ID = "us_uuid1" @@ -127,6 +129,7 @@ class TestConfiguration { // use a new id as the name to avoid name collisions async init(appName = newid()) { + await this.cleanAllDbs() if (!this.started) { await startup() } @@ -137,23 +140,34 @@ class TestConfiguration { return this.createApp(appName) } - async end() { + async cleanAllDbs() { + 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) + } + + end() { if (!this) { return } - if (this.allApps) { - cleanup(this.allApps.map(app => app.appId)) - - await this._req( - null, - { appId: this.prodApp.appId }, - controllers.app.destroy - ) - } - if (this.server) { this.server.close() } + if (this.allApps) { + cleanup(this.allApps.map(app => app.appId)) + } } // UTILS