From 80fe362f33cba69e52418b57e2e2d476923fc510 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Wed, 1 Nov 2023 18:56:12 +0100 Subject: [PATCH] fix(integration): setup (#5511) * fix(integration): setup --- .changeset/tough-cups-breathe.md | 6 ++ integration-tests/api/package.json | 2 +- .../environment-helpers/bootstrap-app.js | 82 +++++++++++++++---- .../environment-helpers/setup-server.js | 17 +--- .../environment-helpers/test-server.js | 19 +---- .../environment-helpers/use-container.js | 5 -- .../environment-helpers/use-db.js | 13 ++- .../environment-helpers/use-template-db.js | 7 +- integration-tests/globalTeardown.js | 25 +++++- .../plugins/__tests__/cart/store/index.ts | 26 +++--- .../plugins/__tests__/inventory/cart/cart.js | 23 +++--- .../inventory-items/ff-many-to-many.js | 40 ++++----- .../inventory/inventory-items/index.js | 27 +++--- .../__tests__/inventory/order/draft-order.js | 31 +++---- .../__tests__/inventory/order/order.js | 24 +++--- .../inventory/products/create-variant.js | 23 +++--- .../inventory/products/delete-variant.js | 23 +++--- .../inventory/products/get-product.js | 23 +++--- .../inventory/products/get-variant.js | 23 +++--- .../inventory/products/list-products.js | 23 +++--- .../inventory/products/list-variants.js | 23 +++--- .../inventory/reservation-items/index.js | 31 +++---- .../plugins/__tests__/inventory/service.js | 18 ++-- .../inventory/variant-inventory-service.js | 18 ++-- .../__tests__/medusa-plugin-sendgrid/index.js | 23 +++--- .../plugins/__tests__/pricing/get-product.ts | 18 ++-- .../__tests__/product/admin/create-product.ts | 8 +- .../plugins/__tests__/product/admin/index.ts | 21 ++--- .../admin/update-product-variant.spec.ts | 12 +-- .../product/admin/update-product.spec.ts | 35 ++++---- .../plugins/__tests__/services/product.ts | 22 ++--- .../stock-location/delete-sales-channels.js | 23 +++--- .../stock-location/delete-stock-location.js | 23 +++--- .../stock-location/sales-channels.js | 23 +++--- .../__tests__/stock-location/service.js | 18 ++-- .../inventory/create-inventory-items.ts | 11 ++- .../workflows/product/create-product.ts | 13 ++- .../workflows/product/update-product.ts | 14 ++-- integration-tests/plugins/medusa-config.js | 2 +- integration-tests/plugins/package.json | 2 +- integration-tests/setup-env.js | 2 + integration-tests/setup.js | 8 +- .../src/services/inmemory-cache.ts | 8 +- .../cache-redis/src/services/redis-cache.ts | 4 + packages/medusa/src/loaders/index.ts | 20 ++--- 45 files changed, 459 insertions(+), 403 deletions(-) create mode 100644 .changeset/tough-cups-breathe.md diff --git a/.changeset/tough-cups-breathe.md b/.changeset/tough-cups-breathe.md new file mode 100644 index 0000000000000..8c23b4675196f --- /dev/null +++ b/.changeset/tough-cups-breathe.md @@ -0,0 +1,6 @@ +--- +"@medusajs/cache-redis": major +"@medusajs/cache-inmemory": patch +--- + +Integration tests fixes and ignore ttl 0 on cache modules diff --git a/integration-tests/api/package.json b/integration-tests/api/package.json index 1c3c04a41b76f..fa7cbc9d27fe0 100644 --- a/integration-tests/api/package.json +++ b/integration-tests/api/package.json @@ -5,7 +5,7 @@ "license": "MIT", "private": true, "scripts": { - "test:integration": "jest --silent=false --maxWorkers=50% --bail --detectOpenHandles --forceExit", + "test:integration": "jest --silent=false --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage", "build": "babel src -d dist --extensions \".ts,.js\"" }, "dependencies": { diff --git a/integration-tests/environment-helpers/bootstrap-app.js b/integration-tests/environment-helpers/bootstrap-app.js index 382e00aaff990..13a1ce01501a7 100644 --- a/integration-tests/environment-helpers/bootstrap-app.js +++ b/integration-tests/environment-helpers/bootstrap-app.js @@ -2,30 +2,78 @@ const path = require("path") const express = require("express") const getPort = require("get-port") const { isObject } = require("@medusajs/utils") +const { setContainer } = require("./use-container") +const { setPort, setExpressServer } = require("./use-api") -module.exports = { - bootstrapApp: async ({ cwd, env = {} } = {}) => { - const app = express() +async function bootstrapApp({ cwd, env = {} } = {}) { + const app = express() - if (isObject(env)) { - Object.entries(env).forEach(([k, v]) => (process.env[k] = v)) - } + if (isObject(env)) { + Object.entries(env).forEach(([k, v]) => (process.env[k] = v)) + } + + const loaders = require("@medusajs/medusa/dist/loaders").default + + const { container, dbConnection, pgConnection } = await loaders({ + directory: path.resolve(cwd || process.cwd()), + expressApp: app, + isTest: false, + }) - const loaders = require("@medusajs/medusa/dist/loaders").default + const PORT = await getPort() - const { container, dbConnection } = await loaders({ - directory: path.resolve(cwd || process.cwd()), - expressApp: app, - isTest: false, + return { + container, + db: dbConnection, + pgConnection, + app, + port: PORT, + } +} + +module.exports = { + bootstrapApp, + startBootstrapApp: async ({ + cwd, + env = {}, + skipExpressListen = false, + } = {}) => { + const { app, port, container, db, pgConnection } = await bootstrapApp({ + cwd, + env, }) + let expressServer - const PORT = await getPort() + setContainer(container) - return { - container, - db: dbConnection, - app, - port: PORT, + if (skipExpressListen) { + return } + + const shutdown = async () => { + await Promise.all([ + expressServer.close(), + db?.destroy(), + pgConnection?.context?.destroy(), + ]) + + if (typeof global !== "undefined" && global?.gc) { + global.gc() + } + } + + return await new Promise((resolve, reject) => { + expressServer = app.listen(port, async (err) => { + if (err) { + await shutdown() + return reject(err) + } + setPort(port) + process.send(port) + resolve(shutdown) + }) + + setExpressServer(expressServer) + }) }, } diff --git a/integration-tests/environment-helpers/setup-server.js b/integration-tests/environment-helpers/setup-server.js index 17ac7565227d7..b70b85b7897ad 100644 --- a/integration-tests/environment-helpers/setup-server.js +++ b/integration-tests/environment-helpers/setup-server.js @@ -3,20 +3,9 @@ const { spawn } = require("child_process") const { setPort, useExpressServer } = require("./use-api") const { setContainer } = require("./use-container") -module.exports = ({ - cwd, - redisUrl, - uploadDir, - verbose, - env, - bootstrapApp = false, -}) => { +module.exports = async ({ cwd, redisUrl, uploadDir, verbose, env }) => { const serverPath = path.join(__dirname, "test-server.js") - if (bootstrapApp) { - require(serverPath) - } - // in order to prevent conflicts in redis, use a different db for each worker // same fix as for databases (works with up to 15) // redis dbs are 0-indexed and jest worker ids are indexed from 1 @@ -25,7 +14,7 @@ module.exports = ({ verbose = verbose ?? false - return new Promise((resolve, reject) => { + return await new Promise((resolve, reject) => { const medusaProcess = spawn("node", [path.resolve(serverPath)], { cwd, env: { @@ -44,11 +33,13 @@ module.exports = ({ medusaProcess.on("error", (err) => { console.log(err) + reject(err) process.exit() }) medusaProcess.on("uncaughtException", (err) => { console.log(err) + reject(err) medusaProcess.kill() }) diff --git a/integration-tests/environment-helpers/test-server.js b/integration-tests/environment-helpers/test-server.js index 8a049c032fe20..58da78e870c9a 100644 --- a/integration-tests/environment-helpers/test-server.js +++ b/integration-tests/environment-helpers/test-server.js @@ -1,18 +1,3 @@ -const { bootstrapApp } = require("./bootstrap-app") -const { setContainer } = require("./use-container") -const { setPort, setExpressServer } = require("./use-api") +const { startBootstrapApp } = require("./bootstrap-app") -const setup = async () => { - const { app, port, container } = await bootstrapApp() - - setContainer(container) - - const expressServer = app.listen(port, (err) => { - setPort(port) - process.send(port) - }) - - setExpressServer(expressServer) -} - -setup() +startBootstrapApp() diff --git a/integration-tests/environment-helpers/use-container.js b/integration-tests/environment-helpers/use-container.js index d1e66b1b14173..9d9520b0bba38 100644 --- a/integration-tests/environment-helpers/use-container.js +++ b/integration-tests/environment-helpers/use-container.js @@ -1,8 +1,3 @@ -const path = require("path") -const express = require("express") -const getPort = require("get-port") -const { isObject } = require("@medusajs/utils") - const AppUtils = { container_: null, diff --git a/integration-tests/environment-helpers/use-db.js b/integration-tests/environment-helpers/use-db.js index 36d02b0ee0ab2..3fe352f9a6bc9 100644 --- a/integration-tests/environment-helpers/use-db.js +++ b/integration-tests/environment-helpers/use-db.js @@ -1,11 +1,11 @@ const path = require("path") const { getConfigFile } = require("medusa-core-utils") +const { asValue } = require("awilix") const { isObject, createMedusaContainer } = require("@medusajs/utils") const { dropDatabase } = require("pg-god") const { DataSource } = require("typeorm") const dbFactory = require("./use-template-db") -const { getContainer } = require("./use-container") const { ContainerRegistrationKeys } = require("@medusajs/utils") const DB_HOST = process.env.DB_HOST @@ -69,10 +69,10 @@ const DbTestUtil = { }, shutdown: async function () { - await this.db_.destroy() + await this.db_?.destroy() await this.pgConnection_?.context?.destroy() - return await dropDatabase({ DB_NAME }, pgGodCredentials) + return await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials) }, } @@ -157,6 +157,12 @@ module.exports = { const container = createMedusaContainer() + container.register({ + [ContainerRegistrationKeys.CONFIG_MODULE]: asValue(configModule), + [ContainerRegistrationKeys.LOGGER]: asValue(console), + [ContainerRegistrationKeys.MANAGER]: asValue(dbDataSource.manager), + }) + const pgConnection = await pgConnectionLoader({ configModule, container }) instance.setPgConnection(pgConnection) @@ -168,6 +174,7 @@ module.exports = { const options = { database: { clientUrl: DB_URL, + connection: pgConnection, }, } await runMigrations(options) diff --git a/integration-tests/environment-helpers/use-template-db.js b/integration-tests/environment-helpers/use-template-db.js index a20091f6e3163..943a1b1303220 100644 --- a/integration-tests/environment-helpers/use-template-db.js +++ b/integration-tests/environment-helpers/use-template-db.js @@ -21,14 +21,13 @@ const pgGodCredentials = { class DatabaseFactory { constructor() { - this.dataSource_ = null this.masterDataSourceName = "master" this.templateDbName = "medusa-integration-template" } async createTemplateDb_({ cwd }) { const { configModule } = getConfigFile(cwd, `medusa-config`) - const dataSource = await this.getMasterDataSource() + const migrationDir = path.resolve( path.join( __dirname, @@ -80,8 +79,6 @@ class DatabaseFactory { await templateDbDataSource.runMigrations() await templateDbDataSource.destroy() - - return dataSource } async getMasterDataSource() { @@ -103,7 +100,7 @@ class DatabaseFactory { async createFromTemplate(dbName) { const dataSource = await this.getMasterDataSource() - await dataSource.query(`DROP DATABASE IF EXISTS "${dbName}";`) + await dropDatabase({ databaseName: dbName }, pgGodCredentials) await dataSource.query( `CREATE DATABASE "${dbName}" TEMPLATE "${this.templateDbName}";` ) diff --git a/integration-tests/globalTeardown.js b/integration-tests/globalTeardown.js index 856f66345c366..9493eedea2643 100644 --- a/integration-tests/globalTeardown.js +++ b/integration-tests/globalTeardown.js @@ -1,5 +1,24 @@ -const dbFactory = require("./environment-helpers/use-template-db") +const { dropDatabase } = require("pg-god") -module.exports = async () => { - await dbFactory.destroy() +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, +} + +const teardown = async () => { + try { + await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials) + } catch (e) { + console.error( + `This might fail if it is run during the unit tests since there is no database to drop. Otherwise, please check what is the issue. ${e.message}` + ) + } } + +module.exports = teardown diff --git a/integration-tests/plugins/__tests__/cart/store/index.ts b/integration-tests/plugins/__tests__/cart/store/index.ts index 75544ca654d7d..59f1b6a7ee056 100644 --- a/integration-tests/plugins/__tests__/cart/store/index.ts +++ b/integration-tests/plugins/__tests__/cart/store/index.ts @@ -1,19 +1,20 @@ -import { MoneyAmount, PriceList, Region } from "@medusajs/medusa" +import { + MoneyAmount, + PriceList, + ProductVariantMoneyAmount, + Region, +} from "@medusajs/medusa" import path from "path" - -import { ProductVariantMoneyAmount } from "@medusajs/medusa" -import { bootstrapApp } from "../../../../environment-helpers/bootstrap-app" -import setupServer from "../../../../environment-helpers/setup-server" -import { setPort, useApi } from "../../../../environment-helpers/use-api" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { useApi } from "../../../../environment-helpers/use-api" import { initDb, useDb } from "../../../../environment-helpers/use-db" import { simpleProductFactory } from "../../../../factories" jest.setTimeout(30000) describe("/store/carts", () => { - let medusaProcess let dbConnection - let express + let shutdownServer const doAfterEach = async () => { const db = useDb() @@ -23,18 +24,13 @@ describe("/store/carts", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - medusaProcess = await setupServer({ cwd }) - const { app, port } = await bootstrapApp({ cwd }) - setPort(port) - express = app.listen(port, () => { - process.send?.(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) }) afterAll(async () => { const db = useDb() await db.shutdown() - medusaProcess.kill() + await shutdownServer() }) describe("POST /store/carts", () => { diff --git a/integration-tests/plugins/__tests__/inventory/cart/cart.js b/integration-tests/plugins/__tests__/inventory/cart/cart.js index bd8058dc1c72c..adc1816c54058 100644 --- a/integration-tests/plugins/__tests__/inventory/cart/cart.js +++ b/integration-tests/plugins/__tests__/inventory/cart/cart.js @@ -1,22 +1,28 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") const cartSeeder = require("../../../../helpers/cart-seeder") const { simpleProductFactory } = require("../../../../factories") const { simpleSalesChannelFactory } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") jest.setTimeout(30000) const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("/store/carts", () => { - let express + let shutdownServer let appContainer let dbConnection @@ -32,19 +38,14 @@ describe("/store/carts", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/inventory-items/ff-many-to-many.js b/integration-tests/plugins/__tests__/inventory/inventory-items/ff-many-to-many.js index bd6f58f87f163..7e584d277f4df 100644 --- a/integration-tests/plugins/__tests__/inventory/inventory-items/ff-many-to-many.js +++ b/integration-tests/plugins/__tests__/inventory/inventory-items/ff-many-to-many.js @@ -2,51 +2,37 @@ const path = require("path") const { ProductVariantInventoryService } = require("@medusajs/medusa") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") jest.setTimeout(30000) const { - simpleProductFactory, - simpleOrderFactory, -} = require("../../../../factories") + getContainer, +} = require("../../../../environment-helpers/use-container") const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("Inventory Items endpoints", () => { let appContainer let dbConnection - let express - - let variantId - let inventoryItems - let locationId - let location2Id - let location3Id + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() // Set feature flag const flagRouter = appContainer.resolve("featureFlagRouter") flagRouter.setFlag("many_to_many_inventory", true) - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) - }) - - beforeEach(async () => { - await adminSeeder(dbConnection) }) afterAll(async () => { @@ -55,7 +41,11 @@ describe("Inventory Items endpoints", () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() + }) + + beforeEach(async () => { + await adminSeeder(dbConnection) }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/inventory-items/index.js b/integration-tests/plugins/__tests__/inventory/inventory-items/index.js index e91ecfe267a7f..1494044f9e353 100644 --- a/integration-tests/plugins/__tests__/inventory/inventory-items/index.js +++ b/integration-tests/plugins/__tests__/inventory/inventory-items/index.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") @@ -14,12 +17,15 @@ const { simpleProductFactory, simpleOrderFactory, } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("Inventory Items endpoints", () => { let appContainer let dbConnection - let express + let shutdownServer let variantId let inventoryItems @@ -30,13 +36,14 @@ describe("Inventory Items endpoints", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() + }) - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + afterAll(async () => { + const db = useDb() + await db.shutdown() + await shutdownServer() }) beforeEach(async () => { @@ -122,7 +129,7 @@ describe("Inventory Items endpoints", () => { afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/order/draft-order.js b/integration-tests/plugins/__tests__/inventory/order/draft-order.js index 591490f26c9c7..3630b666725e3 100644 --- a/integration-tests/plugins/__tests__/inventory/order/draft-order.js +++ b/integration-tests/plugins/__tests__/inventory/order/draft-order.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") const { @@ -18,31 +21,30 @@ const { const { simpleAddressFactory, } = require("../../../../factories/simple-address-factory") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") jest.setTimeout(30000) const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("/store/carts", () => { - let express + let shutdownServer let appContainer let dbConnection - const doAfterEach = async () => { - const db = useDb() - return await db.teardown() - } - beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() + }) - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + afterAll(async () => { + const db = useDb() + await db.shutdown() + await shutdownServer() }) beforeEach(async () => {}) @@ -51,7 +53,6 @@ describe("/store/carts", () => { const variantId = "test-variant" let region - let order let invItemId let prodVarInventoryService let inventoryService diff --git a/integration-tests/plugins/__tests__/inventory/order/order.js b/integration-tests/plugins/__tests__/inventory/order/order.js index 0f3926075dc25..fab2ed92052fc 100644 --- a/integration-tests/plugins/__tests__/inventory/order/order.js +++ b/integration-tests/plugins/__tests__/inventory/order/order.js @@ -1,10 +1,14 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + setPort, + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") const { @@ -17,32 +21,30 @@ const { simpleCartFactory, simpleShippingOptionFactory, } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") jest.setTimeout(150000) const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("/store/carts", () => { - let express + let shutdownServer let appContainer let dbConnection beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/products/create-variant.js b/integration-tests/plugins/__tests__/inventory/products/create-variant.js index e36dcb3d63a94..44706b332ec7f 100644 --- a/integration-tests/plugins/__tests__/inventory/products/create-variant.js +++ b/integration-tests/plugins/__tests__/inventory/products/create-variant.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const { ProductVariantInventoryService, @@ -16,28 +19,26 @@ const adminSeeder = require("../../../../helpers/admin-seeder") jest.setTimeout(30000) const { simpleProductFactory } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") describe("Create Variant", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/products/delete-variant.js b/integration-tests/plugins/__tests__/inventory/products/delete-variant.js index 176096888963c..2c2abc42d6a9c 100644 --- a/integration-tests/plugins/__tests__/inventory/products/delete-variant.js +++ b/integration-tests/plugins/__tests__/inventory/products/delete-variant.js @@ -1,38 +1,39 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") jest.setTimeout(30000) const { simpleProductFactory } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") describe("Delete Variant", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/products/get-product.js b/integration-tests/plugins/__tests__/inventory/products/get-product.js index 57ba9cbe30f4a..4bce1cb1ab578 100644 --- a/integration-tests/plugins/__tests__/inventory/products/get-product.js +++ b/integration-tests/plugins/__tests__/inventory/products/get-product.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") @@ -14,13 +17,16 @@ const { simpleProductFactory, simpleSalesChannelFactory, } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("Get products", () => { let appContainer let dbConnection - let express + let shutdownServer const productId = "test-product" const variantId = "test-variant" let invItem @@ -28,19 +34,14 @@ describe("Get products", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/products/get-variant.js b/integration-tests/plugins/__tests__/inventory/products/get-variant.js index c387a25b50c59..1c2d9a5d27b5b 100644 --- a/integration-tests/plugins/__tests__/inventory/products/get-variant.js +++ b/integration-tests/plugins/__tests__/inventory/products/get-variant.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") @@ -14,13 +17,16 @@ const { simpleProductFactory, simpleSalesChannelFactory, } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("Get variant", () => { let appContainer let dbConnection - let express + let shutdownServer const productId = "test-product" const variantId = "test-variant" let invItem @@ -32,19 +38,14 @@ describe("Get variant", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/products/list-products.js b/integration-tests/plugins/__tests__/inventory/products/list-products.js index 7a0a037ec05b1..ea72b61ba7f0d 100644 --- a/integration-tests/plugins/__tests__/inventory/products/list-products.js +++ b/integration-tests/plugins/__tests__/inventory/products/list-products.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") @@ -12,30 +15,28 @@ jest.setTimeout(30000) const { simpleProductFactory } = require("../../../../factories") const { simpleSalesChannelFactory } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("Create Variant", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/products/list-variants.js b/integration-tests/plugins/__tests__/inventory/products/list-variants.js index 5d557ac789ac8..cc099e8c084da 100644 --- a/integration-tests/plugins/__tests__/inventory/products/list-variants.js +++ b/integration-tests/plugins/__tests__/inventory/products/list-variants.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") @@ -12,29 +15,27 @@ jest.setTimeout(30000) const { simpleProductFactory } = require("../../../../factories") const { simpleSalesChannelFactory } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("List Variants", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/reservation-items/index.js b/integration-tests/plugins/__tests__/inventory/reservation-items/index.js index cc99de4880f75..d873405d60eb7 100644 --- a/integration-tests/plugins/__tests__/inventory/reservation-items/index.js +++ b/integration-tests/plugins/__tests__/inventory/reservation-items/index.js @@ -1,10 +1,13 @@ const path = require("path") const { - bootstrapApp, + startBootstrapApp, } = require("../../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../../environment-helpers/use-api") const adminSeeder = require("../../../../helpers/admin-seeder") @@ -16,12 +19,15 @@ const { simpleRegionFactory, } = require("../../../../factories") const { simpleSalesChannelFactory } = require("../../../../factories") +const { + getContainer, +} = require("../../../../environment-helpers/use-container") const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("Inventory Items endpoints", () => { let appContainer let dbConnection - let express + let shutdownServer let inventoryItem let locationId @@ -41,13 +47,14 @@ describe("Inventory Items endpoints", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() + }) - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + afterAll(async () => { + const db = useDb() + await db.shutdown() + await shutdownServer() }) beforeEach(async () => { @@ -138,12 +145,6 @@ describe("Inventory Items endpoints", () => { }) }) - afterAll(async () => { - const db = useDb() - await db.shutdown() - express.close() - }) - afterEach(async () => { jest.clearAllMocks() const db = useDb() diff --git a/integration-tests/plugins/__tests__/inventory/service.js b/integration-tests/plugins/__tests__/inventory/service.js index 44f9330cdbc9d..cffee069f6db4 100644 --- a/integration-tests/plugins/__tests__/inventory/service.js +++ b/integration-tests/plugins/__tests__/inventory/service.js @@ -1,30 +1,30 @@ const path = require("path") -const { bootstrapApp } = require("../../../environment-helpers/bootstrap-app") +const { + startBootstrapApp, +} = require("../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../environment-helpers/use-db") +const { getContainer } = require("../../../environment-helpers/use-container") +const { useExpressServer } = require("../../../environment-helpers/use-api") jest.setTimeout(50000) describe("Inventory Module", () => { + let shutdownServer let appContainer let dbConnection - let express beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/inventory/variant-inventory-service.js b/integration-tests/plugins/__tests__/inventory/variant-inventory-service.js index 022045fc453d6..5bf7ace38284a 100644 --- a/integration-tests/plugins/__tests__/inventory/variant-inventory-service.js +++ b/integration-tests/plugins/__tests__/inventory/variant-inventory-service.js @@ -1,15 +1,19 @@ const path = require("path") -const { bootstrapApp } = require("../../../environment-helpers/bootstrap-app") +const { + startBootstrapApp, +} = require("../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../environment-helpers/use-db") const { simpleProductFactory } = require("../../../factories") +const { getContainer } = require("../../../environment-helpers/use-container") +const { useExpressServer } = require("../../../environment-helpers/use-api") jest.setTimeout(50000) describe("Inventory Module", () => { let appContainer let dbConnection - let express + let shutdownServer let invItem1 let invItem2 @@ -19,18 +23,14 @@ describe("Inventory Module", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd, verbose: false }) - appContainer = container - - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) describe("ProductVariantInventoryService", () => { diff --git a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/index.js b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/index.js index f04eeb5f3d62d..06f628ceb739e 100644 --- a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/index.js +++ b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/index.js @@ -1,8 +1,13 @@ const path = require("path") -const { bootstrapApp } = require("../../../environment-helpers/bootstrap-app") +const { + startBootstrapApp, +} = require("../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../environment-helpers/use-api") const adminSeeder = require("../../../helpers/admin-seeder") @@ -14,11 +19,12 @@ const { simpleProductFactory, simpleShippingOptionFactory, } = require("../../../factories") +const { getContainer } = require("../../../environment-helpers/use-container") describe("medusa-plugin-sendgrid", () => { let appContainer let dbConnection - let express + let shutdownServer const doAfterEach = async () => { const db = useDb() @@ -28,19 +34,14 @@ describe("medusa-plugin-sendgrid", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/pricing/get-product.ts b/integration-tests/plugins/__tests__/pricing/get-product.ts index 6591fa89467f5..88f71e93cc79c 100644 --- a/integration-tests/plugins/__tests__/pricing/get-product.ts +++ b/integration-tests/plugins/__tests__/pricing/get-product.ts @@ -1,11 +1,12 @@ -import { setPort, useApi } from "../../../environment-helpers/use-api" +import { useApi } from "../../../environment-helpers/use-api" import { initDb, useDb } from "../../../environment-helpers/use-db" import { simpleCartFactory, simpleRegionFactory } from "../../../factories" import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { AxiosInstance } from "axios" import path from "path" -import { bootstrapApp } from "../../../environment-helpers/bootstrap-app" +import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" +import { getContainer } from "../../../environment-helpers/use-container" import adminSeeder from "../../../helpers/admin-seeder" jest.setTimeout(5000000) @@ -30,24 +31,19 @@ const env = { describe("Link Modules", () => { let medusaContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd, env } as any) - - const { container, app, port } = await bootstrapApp({ cwd, env }) - medusaContainer = container - setPort(port) - - express = app.listen(port) + shutdownServer = await startBootstrapApp({ cwd, env }) + medusaContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - - express.close() + await shutdownServer() }) beforeEach(async () => { diff --git a/integration-tests/plugins/__tests__/product/admin/create-product.ts b/integration-tests/plugins/__tests__/product/admin/create-product.ts index 03adb903687c7..ef1af2b397282 100644 --- a/integration-tests/plugins/__tests__/product/admin/create-product.ts +++ b/integration-tests/plugins/__tests__/product/admin/create-product.ts @@ -3,7 +3,7 @@ import { initDb, useDb } from "../../../../environment-helpers/use-db" import { Region } from "@medusajs/medusa" import { AxiosInstance } from "axios" import path from "path" -import setupServer from "../../../../environment-helpers/setup-server" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { useApi } from "../../../../environment-helpers/use-api" import { getContainer } from "../../../../environment-helpers/use-container" import adminSeeder from "../../../../helpers/admin-seeder" @@ -25,19 +25,19 @@ const env = { describe.skip("[Product & Pricing Module] POST /admin/products", () => { let dbConnection let appContainer - let medusaProcess + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd, env } as any) - medusaProcess = await setupServer({ cwd, env, bootstrapApp: true } as any) + shutdownServer = await startBootstrapApp({ cwd, env }) appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - medusaProcess.kill() + await shutdownServer() }) beforeEach(async () => { diff --git a/integration-tests/plugins/__tests__/product/admin/index.ts b/integration-tests/plugins/__tests__/product/admin/index.ts index ffca3acb7f41c..64edca16346cc 100644 --- a/integration-tests/plugins/__tests__/product/admin/index.ts +++ b/integration-tests/plugins/__tests__/product/admin/index.ts @@ -1,6 +1,6 @@ import path from "path" -import { bootstrapApp } from "../../../../environment-helpers/bootstrap-app" -import { setPort, useApi } from "../../../../environment-helpers/use-api" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { useApi } from "../../../../environment-helpers/use-api" import { initDb, useDb } from "../../../../environment-helpers/use-db" import adminSeeder from "../../../../helpers/admin-seeder" @@ -9,6 +9,7 @@ import productSeeder from "../../../../helpers/product-seeder" import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" import { Workflows } from "@medusajs/workflows" import { AxiosInstance } from "axios" +import { getContainer } from "../../../../environment-helpers/use-container" import { simpleProductFactory, simpleSalesChannelFactory, @@ -24,26 +25,20 @@ const adminHeaders = { describe("/admin/products", () => { let dbConnection - let express + let shutdownServer let medusaContainer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) - dbConnection = await initDb({ cwd } as any) - const { app, port, container } = await bootstrapApp({ cwd }) - medusaContainer = container - - setPort(port) - express = app.listen(port, () => { - process.send?.(port) - }) + dbConnection = await initDb({ cwd }) + shutdownServer = await startBootstrapApp({ cwd }) + medusaContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - - express.close() + await shutdownServer() }) it("Should have loaded the product module", function () { diff --git a/integration-tests/plugins/__tests__/product/admin/update-product-variant.spec.ts b/integration-tests/plugins/__tests__/product/admin/update-product-variant.spec.ts index 5fa882358d3b0..7fc8bc53e18c1 100644 --- a/integration-tests/plugins/__tests__/product/admin/update-product-variant.spec.ts +++ b/integration-tests/plugins/__tests__/product/admin/update-product-variant.spec.ts @@ -1,4 +1,3 @@ -import setupServer from "../../../../environment-helpers/setup-server" import { useApi } from "../../../../environment-helpers/use-api" import { getContainer } from "../../../../environment-helpers/use-container" import { initDb, useDb } from "../../../../environment-helpers/use-db" @@ -7,11 +6,12 @@ import { simpleRegionFactory, } from "../../../../factories" +import { AxiosInstance } from "axios" import path from "path" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import adminSeeder from "../../../../helpers/admin-seeder" import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { createVariantPriceSet } from "../../../helpers/create-variant-price-set" -import { AxiosInstance } from "axios" jest.setTimeout(50000) @@ -26,24 +26,24 @@ const env = { MEDUSA_FF_ISOLATE_PRODUCT_DOMAIN: true, } -describe("[Product & Pricing Module] POST /admin/products/:id/variants/:id", () => { +describe.skip("[Product & Pricing Module] POST /admin/products/:id/variants/:id", () => { let dbConnection let appContainer - let medusaProcess + let shutdownServer let product let variant beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd, env } as any) - medusaProcess = await setupServer({ cwd, env, bootstrapApp: true } as any) + shutdownServer = await startBootstrapApp({ cwd, env }) appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - medusaProcess.kill() + await shutdownServer() }) beforeEach(async () => { diff --git a/integration-tests/plugins/__tests__/product/admin/update-product.spec.ts b/integration-tests/plugins/__tests__/product/admin/update-product.spec.ts index f095368871e4f..c88f37457e786 100644 --- a/integration-tests/plugins/__tests__/product/admin/update-product.spec.ts +++ b/integration-tests/plugins/__tests__/product/admin/update-product.spec.ts @@ -1,4 +1,3 @@ -import setupServer from "../../../../environment-helpers/setup-server" import { useApi } from "../../../../environment-helpers/use-api" import { getContainer } from "../../../../environment-helpers/use-container" import { initDb, useDb } from "../../../../environment-helpers/use-db" @@ -7,6 +6,7 @@ import { simpleProductFactory } from "../../../../factories" import { Region } from "@medusajs/medusa" import { AxiosInstance } from "axios" import path from "path" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import adminSeeder from "../../../../helpers/admin-seeder" import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types" import { createVariantPriceSet } from "../../../helpers/create-variant-price-set" @@ -27,21 +27,21 @@ const env = { describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => { let dbConnection let appContainer - let medusaProcess + let shutdownServer let product let variant beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) dbConnection = await initDb({ cwd, env } as any) - medusaProcess = await setupServer({ cwd, env, bootstrapApp: true } as any) + shutdownServer = await startBootstrapApp({ cwd, env }) appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - medusaProcess.kill() + await shutdownServer() }) beforeEach(async () => { @@ -80,7 +80,7 @@ describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => { }) it("should update product variant price sets and prices", async () => { - const api = useApi() + const api = useApi() as any const data = { title: "test product update", variants: [ @@ -102,14 +102,13 @@ describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => { ], } - let response = await api.post( + await api.post(`/admin/products/${product.id}`, data, adminHeaders) + + const response = await api.get( `/admin/products/${product.id}`, - data, adminHeaders ) - response = await api.get(`/admin/products/${product.id}`, adminHeaders) - expect(response.status).toEqual(200) expect(response.data.product).toEqual( expect.objectContaining({ @@ -150,7 +149,7 @@ describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => { const moneyAmountToUpdate = priceSet.money_amounts?.[0] - const api = useApi() + const api = useApi() as any const data = { title: "test product update", variants: [ @@ -173,14 +172,15 @@ describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => { ], } - let response = await api.post( + console.log("I am here first") + await api.post(`/admin/products/${product.id}`, data, adminHeaders) + console.log("I am here") + + const response = await api.get( `/admin/products/${product.id}`, - data, adminHeaders ) - response = await api.get(`/admin/products/${product.id}`, adminHeaders) - expect(response.status).toEqual(200) expect(response.data.product).toEqual( expect.objectContaining({ @@ -248,14 +248,13 @@ describe.skip("[Product & Pricing Module] POST /admin/products/:id", () => { ], } - let response = await api.post( + await api.post(`/admin/products/${product.id}`, data, adminHeaders) + + const response = await api.get( `/admin/products/${product.id}`, - data, adminHeaders ) - response = await api.get(`/admin/products/${product.id}`, adminHeaders) - expect(response.status).toEqual(200) expect(response.data.product).toEqual( expect.objectContaining({ diff --git a/integration-tests/plugins/__tests__/services/product.ts b/integration-tests/plugins/__tests__/services/product.ts index ad3b8eeedf4a4..c42a531941699 100644 --- a/integration-tests/plugins/__tests__/services/product.ts +++ b/integration-tests/plugins/__tests__/services/product.ts @@ -1,35 +1,27 @@ import path from "path" +import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" +import { getContainer } from "../../../environment-helpers/use-container" import { initDb, useDb } from "../../../environment-helpers/use-db" -import { bootstrapApp } from "../../../environment-helpers/bootstrap-app" -import { setPort } from "../../../environment-helpers/use-api" jest.setTimeout(30000) describe("product", () => { - let dbConnection let medusaContainer let productService - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) - dbConnection = await initDb({ cwd } as any) - const { container, port, app } = await bootstrapApp({ cwd }) - - setPort(port) - express = app.listen(port, () => { - process.send!(port) - }) - - medusaContainer = container + await initDb({ cwd }) + shutdownServer = shutdownServer = await startBootstrapApp({ cwd }) + medusaContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/stock-location/delete-sales-channels.js b/integration-tests/plugins/__tests__/stock-location/delete-sales-channels.js index 30b67f70ca4fc..289993076a9b8 100644 --- a/integration-tests/plugins/__tests__/stock-location/delete-sales-channels.js +++ b/integration-tests/plugins/__tests__/stock-location/delete-sales-channels.js @@ -1,34 +1,35 @@ const path = require("path") -const { bootstrapApp } = require("../../../environment-helpers/bootstrap-app") +const { + startBootstrapApp, +} = require("../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../environment-helpers/use-api") const adminSeeder = require("../../../helpers/admin-seeder") +const { getContainer } = require("../../../environment-helpers/use-container") jest.setTimeout(30000) describe("Sales channels", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/stock-location/delete-stock-location.js b/integration-tests/plugins/__tests__/stock-location/delete-stock-location.js index 2902f73b6d8ea..2262a3f5e36d3 100644 --- a/integration-tests/plugins/__tests__/stock-location/delete-stock-location.js +++ b/integration-tests/plugins/__tests__/stock-location/delete-stock-location.js @@ -1,34 +1,35 @@ const path = require("path") -const { bootstrapApp } = require("../../../environment-helpers/bootstrap-app") +const { + startBootstrapApp, +} = require("../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../environment-helpers/use-api") const adminSeeder = require("../../../helpers/admin-seeder") +const { getContainer } = require("../../../environment-helpers/use-container") jest.setTimeout(30000) describe("Sales channels", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/stock-location/sales-channels.js b/integration-tests/plugins/__tests__/stock-location/sales-channels.js index 0b6b86164314d..08ef3020c228d 100644 --- a/integration-tests/plugins/__tests__/stock-location/sales-channels.js +++ b/integration-tests/plugins/__tests__/stock-location/sales-channels.js @@ -1,10 +1,16 @@ const path = require("path") -const { bootstrapApp } = require("../../../environment-helpers/bootstrap-app") +const { + startBootstrapApp, +} = require("../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../environment-helpers/use-db") -const { setPort, useApi } = require("../../../environment-helpers/use-api") +const { + useApi, + useExpressServer, +} = require("../../../environment-helpers/use-api") const adminSeeder = require("../../../helpers/admin-seeder") +const { getContainer } = require("../../../environment-helpers/use-container") jest.setTimeout(30000) @@ -13,24 +19,19 @@ const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } describe("Sales channels", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - setPort(port) - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/stock-location/service.js b/integration-tests/plugins/__tests__/stock-location/service.js index 9cec2189efade..be40e14c8b504 100644 --- a/integration-tests/plugins/__tests__/stock-location/service.js +++ b/integration-tests/plugins/__tests__/stock-location/service.js @@ -1,30 +1,30 @@ const path = require("path") -const { bootstrapApp } = require("../../../environment-helpers/bootstrap-app") +const { + startBootstrapApp, +} = require("../../../environment-helpers/bootstrap-app") const { initDb, useDb } = require("../../../environment-helpers/use-db") +const { getContainer } = require("../../../environment-helpers/use-container") +const { useExpressServer } = require("../../../environment-helpers/use-api") jest.setTimeout(30000) describe("Inventory Module", () => { let appContainer let dbConnection - let express + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - const { container, app, port } = await bootstrapApp({ cwd }) - appContainer = container - - express = app.listen(port, (err) => { - process.send(port) - }) + shutdownServer = await startBootstrapApp({ cwd }) + appContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - express.close() + await shutdownServer() }) afterEach(async () => { diff --git a/integration-tests/plugins/__tests__/workflows/inventory/create-inventory-items.ts b/integration-tests/plugins/__tests__/workflows/inventory/create-inventory-items.ts index 02fdb14ae3857..8b749de8c3d4c 100644 --- a/integration-tests/plugins/__tests__/workflows/inventory/create-inventory-items.ts +++ b/integration-tests/plugins/__tests__/workflows/inventory/create-inventory-items.ts @@ -6,24 +6,27 @@ import { pipe, } from "@medusajs/workflows" import path from "path" -import { bootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { getContainer } from "../../../../environment-helpers/use-container" import { initDb, useDb } from "../../../../environment-helpers/use-db" jest.setTimeout(30000) describe("CreateInventoryItem workflow", function () { let medusaContainer + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) - await initDb({ cwd } as any) - const { container } = await bootstrapApp({ cwd }) - medusaContainer = container + await initDb({ cwd }) + shutdownServer = await startBootstrapApp({ cwd, skipExpressListen: true }) + medusaContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() + await shutdownServer() }) it("should compensate all the invoke if something fails", async () => { diff --git a/integration-tests/plugins/__tests__/workflows/product/create-product.ts b/integration-tests/plugins/__tests__/workflows/product/create-product.ts index fb02d8e16b9be..5953965c6a5c7 100644 --- a/integration-tests/plugins/__tests__/workflows/product/create-product.ts +++ b/integration-tests/plugins/__tests__/workflows/product/create-product.ts @@ -7,24 +7,29 @@ import { pipe, } from "@medusajs/workflows" import path from "path" -import { bootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { getContainer } from "../../../../environment-helpers/use-container" import { initDb, useDb } from "../../../../environment-helpers/use-db" jest.setTimeout(30000) describe("CreateProduct workflow", function () { let medusaContainer + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) - await initDb({ cwd } as any) - const { container } = await bootstrapApp({ cwd }) - medusaContainer = container + await initDb({ cwd }) + shutdownServer = await startBootstrapApp({ cwd, skipExpressListen: true }) + medusaContainer = getContainer() }) afterAll(async () => { + console.log("GLOABL GC()", typeof global) + const db = useDb() await db.shutdown() + await shutdownServer() }) it("should compensate all the invoke if something fails", async () => { diff --git a/integration-tests/plugins/__tests__/workflows/product/update-product.ts b/integration-tests/plugins/__tests__/workflows/product/update-product.ts index 38d9772166a14..26e0863f60ff8 100644 --- a/integration-tests/plugins/__tests__/workflows/product/update-product.ts +++ b/integration-tests/plugins/__tests__/workflows/product/update-product.ts @@ -7,29 +7,29 @@ import { } from "@medusajs/workflows" import path from "path" -import { bootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" +import { getContainer } from "../../../../environment-helpers/use-container" import { initDb, useDb } from "../../../../environment-helpers/use-db" import { simpleProductFactory } from "../../../../factories" jest.setTimeout(30000) describe("UpdateProduct workflow", function () { - let medusaProcess let dbConnection let medusaContainer + let shutdownServer beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..", "..")) - dbConnection = await initDb({ cwd } as any) - const { container } = await bootstrapApp({ cwd }) - medusaContainer = container + dbConnection = await initDb({ cwd }) + shutdownServer = await startBootstrapApp({ cwd, skipExpressListen: true }) + medusaContainer = getContainer() }) afterAll(async () => { const db = useDb() await db.shutdown() - - medusaProcess.kill() + await shutdownServer() }) beforeEach(async () => { diff --git a/integration-tests/plugins/medusa-config.js b/integration-tests/plugins/medusa-config.js index fd215f04e2990..630721bb369f5 100644 --- a/integration-tests/plugins/medusa-config.js +++ b/integration-tests/plugins/medusa-config.js @@ -58,7 +58,7 @@ module.exports = { }, [Modules.CACHE]: { resolve: "@medusajs/cache-inmemory", - options: { ttl: 5 }, + options: { ttl: 0 }, // Cache disabled }, [Modules.PRODUCT]: { scope: "internal", diff --git a/integration-tests/plugins/package.json b/integration-tests/plugins/package.json index a5f73b935c1b3..0b1b18de06da5 100644 --- a/integration-tests/plugins/package.json +++ b/integration-tests/plugins/package.json @@ -5,7 +5,7 @@ "license": "MIT", "private": true, "scripts": { - "test:integration": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit", + "test:integration": "node --expose-gc ./../../node_modules/.bin/jest --silent=false --runInBand --bail --logHeapUsage --forceExit", "build": "babel src -d dist --extensions \".ts,.js\"" }, "dependencies": { diff --git a/integration-tests/setup-env.js b/integration-tests/setup-env.js index da786853ddeea..65ead03c62a91 100644 --- a/integration-tests/setup-env.js +++ b/integration-tests/setup-env.js @@ -6,3 +6,5 @@ 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}` } + +global.performance = require("perf_hooks").performance diff --git a/integration-tests/setup.js b/integration-tests/setup.js index 484988cd45b03..c41fd5105d929 100644 --- a/integration-tests/setup.js +++ b/integration-tests/setup.js @@ -12,5 +12,11 @@ const pgGodCredentials = { } afterAll(async () => { - await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials) + try { + await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials) + } catch (e) { + console.error( + `This might fail if it is run during the unit tests since there is no database to drop. Otherwise, please check what is the issue. ${e.message}` + ) + } }) diff --git a/packages/cache-inmemory/src/services/inmemory-cache.ts b/packages/cache-inmemory/src/services/inmemory-cache.ts index 37020a96e92f9..12552db07a031 100644 --- a/packages/cache-inmemory/src/services/inmemory-cache.ts +++ b/packages/cache-inmemory/src/services/inmemory-cache.ts @@ -45,6 +45,10 @@ class InMemoryCacheService implements ICacheService { * @param ttl - expiration time in seconds */ async set(key: string, data: T, ttl: number = this.TTL): Promise { + if (ttl === 0) { + return + } + const record: CacheRecord = { data, expire: ttl * 1000 + Date.now() } const oldRecord = this.store.get(key) @@ -54,8 +58,8 @@ class InMemoryCacheService implements ICacheService { this.timoutRefs.delete(key) } - const ref = setTimeout(() => { - this.invalidate(key) + const ref = setTimeout(async () => { + await this.invalidate(key) }, ttl * 1000) ref.unref() diff --git a/packages/cache-redis/src/services/redis-cache.ts b/packages/cache-redis/src/services/redis-cache.ts index 442bcc9a08f8d..b562ef8415459 100644 --- a/packages/cache-redis/src/services/redis-cache.ts +++ b/packages/cache-redis/src/services/redis-cache.ts @@ -35,6 +35,10 @@ class RedisCacheService implements ICacheService { data: Record, ttl: number = this.TTL ): Promise { + if (ttl === 0) { + return + } + await this.redis.set( this.getCacheKey(key), JSON.stringify(data), diff --git a/packages/medusa/src/loaders/index.ts b/packages/medusa/src/loaders/index.ts index c5ed4e7184cae..d2e8904900f24 100644 --- a/packages/medusa/src/loaders/index.ts +++ b/packages/medusa/src/loaders/index.ts @@ -1,32 +1,26 @@ -import { - MedusaApp, - ModulesDefinition, - moduleLoader, - registerModules, -} from "@medusajs/modules-sdk" +import { moduleLoader, registerModules } from "@medusajs/modules-sdk" import { Express, NextFunction, Request, Response } from "express" import databaseLoader, { dataSource } from "./database" import pluginsLoader, { registerPluginModels } from "./plugins" -import { Connection } from "typeorm" import { ContainerRegistrationKeys } from "@medusajs/utils" import { asValue } from "awilix" import { createMedusaContainer } from "medusa-core-utils" import { track } from "medusa-telemetry" import { EOL } from "os" import requestIp from "request-ip" -import modulesConfig from "../modules-config" +import { Connection } from "typeorm" import { MedusaContainer } from "../types/global" import apiLoader from "./api" +import loadConfig from "./config" import defaultsLoader from "./defaults" import expressLoader from "./express" import featureFlagsLoader from "./feature-flags" import IsolatePricingDomainFeatureFlag from "./feature-flags/isolate-pricing-domain" import IsolateProductDomainFeatureFlag from "./feature-flags/isolate-product-domain" import Logger from "./logger" -import { joinerConfig } from "../joiner-config" -import loadConfig from "./config" +import loadMedusaApp from "./medusa-app" import modelsLoader from "./models" import passportLoader from "./passport" import pgConnectionLoader from "./pg-connection" @@ -36,7 +30,6 @@ import searchIndexLoader from "./search-index" import servicesLoader from "./services" import strategiesLoader from "./strategies" import subscribersLoader from "./subscribers" -import loadMedusaApp from "./medusa-app" type Options = { directory: string @@ -52,6 +45,7 @@ export default async ({ container: MedusaContainer dbConnection: Connection app: Express + pgConnection: unknown }> => { const configModule = loadConfig(rootDirectory) @@ -102,7 +96,7 @@ export default async ({ const stratAct = Logger.success(stratActivity, "Strategies initialized") || {} track("STRATEGIES_INIT_COMPLETED", { duration: stratAct.duration }) - await pgConnectionLoader({ container, configModule }) + const pgConnection = await pgConnectionLoader({ container, configModule }) const modulesActivity = Logger.activity(`Initializing modules${EOL}`) @@ -202,5 +196,5 @@ export default async ({ Logger.success(searchActivity, "Indexing event emitted") || {} track("SEARCH_ENGINE_INDEXING_COMPLETED", { duration: searchAct.duration }) - return { container, dbConnection, app: expressApp } + return { container, dbConnection, app: expressApp, pgConnection } }