fix(integration): setup (#5511)

* fix(integration): setup
This commit is contained in:
Adrien de Peretti
2023-11-01 13:56:12 -04:00
committed by GitHub
parent 4692f54b49
commit 80fe362f33
45 changed files with 461 additions and 405 deletions
+67 -19
View File
@@ -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")
async function bootstrapApp({ cwd, env = {} } = {}) {
const app = express()
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 PORT = await getPort()
return {
container,
db: dbConnection,
pgConnection,
app,
port: PORT,
}
}
module.exports = {
bootstrapApp: async ({ cwd, env = {} } = {}) => {
const app = express()
if (isObject(env)) {
Object.entries(env).forEach(([k, v]) => (process.env[k] = v))
}
const loaders = require("@medusajs/medusa/dist/loaders").default
const { container, dbConnection } = await loaders({
directory: path.resolve(cwd || process.cwd()),
expressApp: app,
isTest: false,
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)
})
},
}
@@ -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()
})
@@ -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()
@@ -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,
@@ -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)
@@ -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}";`
)