From 690ca9e89a209360680ccd4a7208e7a2c437aff9 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 24 Aug 2022 07:29:53 -0300 Subject: [PATCH] chore(integration-tests): Normalize DB config + use single process (#2077) --- .github/workflows/action.yml | 4 ++-- integration-tests/{.env.template => .env.test} | 1 + .../api/__tests__/admin/discount.js | 1 - .../__tests__/line-item-adjustments/index.js | 4 ---- integration-tests/api/jest.config.js | 1 + integration-tests/api/medusa-config.js | 9 +++++---- integration-tests/helpers/use-db.js | 18 ++++++++---------- integration-tests/helpers/use-template-db.js | 11 +++++++---- integration-tests/jest.config.js | 1 + integration-tests/plugins/jest.config.js | 1 + integration-tests/plugins/medusa-config.js | 13 +++++++------ integration-tests/plugins/package.json | 2 +- integration-tests/setup-env.js | 8 ++++++++ integration-tests/setup.js | 16 ++++++---------- package.json | 8 ++++---- 15 files changed, 52 insertions(+), 46 deletions(-) rename integration-tests/{.env.template => .env.test} (78%) create mode 100644 integration-tests/setup-env.js diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index ed2eeeac6f..0e1423b25c 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -143,7 +143,7 @@ jobs: working-directory: integration-tests/api - name: Run integration tests - run: yarn test --maxWorkers=50% + run: yarn test --runInBand working-directory: integration-tests/api env: DB_PASSWORD: postgres @@ -215,6 +215,6 @@ jobs: working-directory: integration-tests/plugins - name: Run plugin integration tests - run: yarn test:integration:plugins --maxWorkers=50% --silent=false + run: yarn test:integration:plugins --silent=false env: DB_PASSWORD: postgres diff --git a/integration-tests/.env.template b/integration-tests/.env.test similarity index 78% rename from integration-tests/.env.template rename to integration-tests/.env.test index b8fc5610ab..ef7d4eb129 100644 --- a/integration-tests/.env.template +++ b/integration-tests/.env.test @@ -1,3 +1,4 @@ # Default postgres credentials +DB_HOST=localhost DB_USERNAME=postgres DB_PASSWORD='' \ No newline at end of file diff --git a/integration-tests/api/__tests__/admin/discount.js b/integration-tests/api/__tests__/admin/discount.js index 5a56698f7d..7b3982bc1c 100644 --- a/integration-tests/api/__tests__/admin/discount.js +++ b/integration-tests/api/__tests__/admin/discount.js @@ -2233,7 +2233,6 @@ describe("/admin/discounts", () => { }) const cond = discountCondition.data.discount_condition - console.log(cond.products) expect(discountCondition.status).toEqual(200) expect(cond).toMatchSnapshot({ diff --git a/integration-tests/api/__tests__/line-item-adjustments/index.js b/integration-tests/api/__tests__/line-item-adjustments/index.js index 31ccf99527..5a9d4cc601 100644 --- a/integration-tests/api/__tests__/line-item-adjustments/index.js +++ b/integration-tests/api/__tests__/line-item-adjustments/index.js @@ -101,10 +101,6 @@ describe("Line Item Adjustments", () => { await doAfterEach() }) - afterEach(async () => { - await doAfterEach() - }) - describe("Given an existing line item, a discount, and a line item adjustment for both", () => { describe("When creating an adjustment for another line item w. same discount", () => { test("Then should create an adjustment", async () => { diff --git a/integration-tests/api/jest.config.js b/integration-tests/api/jest.config.js index f91a72a144..862113b1b4 100644 --- a/integration-tests/api/jest.config.js +++ b/integration-tests/api/jest.config.js @@ -17,6 +17,7 @@ module.exports = { ], transformIgnorePatterns: [`/dist`], transform: { "^.+\\.[jt]s$": `../../jest-transformer.js` }, + setupFiles: ["../setup-env.js"], setupFilesAfterEnv: ["../setup.js"], globalSetup: "../globalSetup.js", globalTeardown: "../globalTeardown.js", diff --git a/integration-tests/api/medusa-config.js b/integration-tests/api/medusa-config.js index 5d076946b6..ef42b43153 100644 --- a/integration-tests/api/medusa-config.js +++ b/integration-tests/api/medusa-config.js @@ -1,12 +1,13 @@ -const DB_USERNAME = process.env.DB_USERNAME || "postgres" -const DB_PASSWORD = process.env.DB_PASSWORD || "" -const workerId = parseInt(process.env.JEST_WORKER_ID || "1") +const DB_HOST = process.env.DB_HOST +const DB_USERNAME = process.env.DB_USERNAME +const DB_PASSWORD = process.env.DB_PASSWORD +const DB_NAME = process.env.DB_TEMP_NAME module.exports = { plugins: [], projectConfig: { redis_url: process.env.REDIS_URL, - database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration-${workerId}`, + database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`, database_type: "postgres", jwt_secret: "test", cookie_secret: "test", diff --git a/integration-tests/helpers/use-db.js b/integration-tests/helpers/use-db.js index 36b301c33b..fb8c10f661 100644 --- a/integration-tests/helpers/use-db.js +++ b/integration-tests/helpers/use-db.js @@ -1,18 +1,19 @@ const path = require("path") -require("dotenv").config({ path: path.join(__dirname, "../.env") }) const { dropDatabase } = require("pg-god") const { createConnection } = require("typeorm") const dbFactory = require("./use-template-db") -const workerId = parseInt(process.env.JEST_WORKER_ID || "1") -const DB_USERNAME = process.env.DB_USERNAME || "postgres" -const DB_PASSWORD = process.env.DB_PASSWORD || "" -const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration-${workerId}` +const DB_HOST = process.env.DB_HOST +const DB_USERNAME = process.env.DB_USERNAME +const DB_PASSWORD = process.env.DB_PASSWORD +const DB_NAME = process.env.DB_TEMP_NAME +const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}` const pgGodCredentials = { user: DB_USERNAME, password: DB_PASSWORD, + host: DB_HOST, } const keepTables = [ @@ -69,8 +70,7 @@ const DbTestUtil = { shutdown: async function () { await this.db_.close() - const databaseName = `medusa-integration-${workerId}` - return await dropDatabase({ databaseName }, pgGodCredentials) + return await dropDatabase({ DB_NAME }, pgGodCredentials) }, } @@ -117,9 +117,7 @@ module.exports = { instance.setDb(dbConnection) return dbConnection } else { - const databaseName = `medusa-integration-${workerId}` - - await dbFactory.createFromTemplate(databaseName) + await dbFactory.createFromTemplate(DB_NAME) // get migraitons with enabled featureflags const migrationDir = path.resolve( diff --git a/integration-tests/helpers/use-template-db.js b/integration-tests/helpers/use-template-db.js index 484acda457..5af2fe9797 100644 --- a/integration-tests/helpers/use-template-db.js +++ b/integration-tests/helpers/use-template-db.js @@ -1,16 +1,19 @@ const path = require("path") -require("dotenv").config({ path: path.join(__dirname, "../.env") }) + +require("dotenv").config({ path: path.join(__dirname, "../.env.test") }) const { createDatabase, dropDatabase } = require("pg-god") const { createConnection, getConnection } = require("typeorm") -const DB_USERNAME = process.env.DB_USERNAME || "postgres" -const DB_PASSWORD = process.env.DB_PASSWORD || "" -const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost` +const DB_HOST = process.env.DB_HOST +const DB_USERNAME = process.env.DB_USERNAME +const DB_PASSWORD = process.env.DB_PASSWORD +const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}` const pgGodCredentials = { user: DB_USERNAME, password: DB_PASSWORD, + host: DB_HOST, } class DatabaseFactory { diff --git a/integration-tests/jest.config.js b/integration-tests/jest.config.js index 9c43ae0e76..f9e2ed225d 100644 --- a/integration-tests/jest.config.js +++ b/integration-tests/jest.config.js @@ -25,5 +25,6 @@ module.exports = { `.cache`, ], transform: { "^.+\\.[jt]s$": `/jest-transformer.js` }, + setupFiles: ["/integration-tests/setup-env.js"], setupFilesAfterEnv: ["/integration-tests/setup.js"], } diff --git a/integration-tests/plugins/jest.config.js b/integration-tests/plugins/jest.config.js index 4350251337..298282f809 100644 --- a/integration-tests/plugins/jest.config.js +++ b/integration-tests/plugins/jest.config.js @@ -15,6 +15,7 @@ module.exports = { ], transformIgnorePatterns: [`/dist`], transform: { "^.+\\.[jt]s$": `../../jest-transformer.js` }, + setupFiles: ["../setup-env.js"], setupFilesAfterEnv: ["../setup.js"], globalSetup: "../globalSetup.js", globalTeardown: "../globalTeardown.js", diff --git a/integration-tests/plugins/medusa-config.js b/integration-tests/plugins/medusa-config.js index 91896bce98..2b16170fa9 100644 --- a/integration-tests/plugins/medusa-config.js +++ b/integration-tests/plugins/medusa-config.js @@ -1,6 +1,7 @@ -const DB_USERNAME = process.env.DB_USERNAME || "postgres" -const DB_PASSWORD = process.env.DB_PASSWORD || "" -const workerId = parseInt(process.env.JEST_WORKER_ID || "1") +const DB_HOST = process.env.DB_HOST +const DB_USERNAME = process.env.DB_USERNAME +const DB_PASSWORD = process.env.DB_PASSWORD +const DB_NAME = process.env.DB_TEMP_NAME module.exports = { plugins: [ @@ -22,9 +23,9 @@ module.exports = { ], projectConfig: { // redis_url: REDIS_URL, - database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost/medusa-integration-${workerId}`, + database_url: `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`, database_type: "postgres", - jwt_secret: 'test', - cookie_secret: 'test' + jwt_secret: "test", + cookie_secret: "test", }, } diff --git a/integration-tests/plugins/package.json b/integration-tests/plugins/package.json index d0869a4edb..21bf58868b 100644 --- a/integration-tests/plugins/package.json +++ b/integration-tests/plugins/package.json @@ -4,7 +4,7 @@ "main": "index.js", "license": "MIT", "scripts": { - "test": "jest --maxWorkers=50% --silent=false", + "test": "jest --runInBand --silent=false", "build": "babel src -d dist --extensions \".ts,.js\"" }, "dependencies": { diff --git a/integration-tests/setup-env.js b/integration-tests/setup-env.js new file mode 100644 index 0000000000..da786853dd --- /dev/null +++ b/integration-tests/setup-env.js @@ -0,0 +1,8 @@ +const path = require("path") + +require("dotenv").config({ path: path.join(__dirname, ".env.test") }) + +if (typeof process.env.DB_TEMP_NAME === "undefined") { + const tempName = parseInt(process.env.JEST_WORKER_ID || "1") + process.env.DB_TEMP_NAME = `medusa-integration-${tempName}` +} diff --git a/integration-tests/setup.js b/integration-tests/setup.js index 54a3d0eb44..484988cd45 100644 --- a/integration-tests/setup.js +++ b/integration-tests/setup.js @@ -1,20 +1,16 @@ -const path = require("path") const { dropDatabase } = require("pg-god") -require("dotenv").config({ path: path.join(__dirname, ".env") }) - -const DB_USERNAME = process.env.DB_USERNAME || "postgres" -const DB_PASSWORD = process.env.DB_PASSWORD || "" +const DB_HOST = process.env.DB_HOST +const DB_USERNAME = process.env.DB_USERNAME +const DB_PASSWORD = process.env.DB_PASSWORD +const DB_NAME = process.env.DB_TEMP_NAME const pgGodCredentials = { user: DB_USERNAME, password: DB_PASSWORD, + host: DB_HOST, } afterAll(async () => { - const workerId = parseInt(process.env.JEST_WORKER_ID || "1") - await dropDatabase( - { databaseName: `medusa-integration-${workerId}` }, - pgGodCredentials - ) + await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials) }) diff --git a/package.json b/package.json index e5b08d1445..df3a6070fe 100644 --- a/package.json +++ b/package.json @@ -64,10 +64,10 @@ "jest": "jest", "test": "turbo run test", "prettier": "prettier", - "test:integration": "jest --config=integration-tests/jest.config.js", - "test:integration:api": "jest --config=integration-tests/jest.config.js --projects=integration-tests/api", - "test:integration:plugins": "jest --config=integration-tests/jest.config.js --projects=integration-tests/plugins", - "test:fixtures": "jest --config=docs-util/jest.config.js --runInBand", + "test:integration": "NODE_ENV=test jest --runInBand --config=integration-tests/jest.config.js", + "test:integration:api": "NODE_ENV=test jest --runInBand --config=integration-tests/jest.config.js --projects=integration-tests/api", + "test:integration:plugins": "NODE_ENV=test jest --runInBand --config=integration-tests/jest.config.js --projects=integration-tests/plugins", + "test:fixtures": "NODE_ENV=test jest --config=docs-util/jest.config.js --runInBand", "openapi:generate": "node ./scripts/build-openapi.js", "generate:services": "typedoc --options typedoc.services.js", "generate:js-client": "typedoc --options typedoc.js-client.js",