Files
medusa-store/integration-tests/environment-helpers/bootstrap-app.js
Adrien de Peretti 82a176e30e chore(medusa-test-utils):Handle errors gracefully (#6901)
**What**
- Better error handling and error message
- update deps management and dynamic import/require
- Pass a new flag to the modules loaders for the module loaders to be able to act depending on it. In that case, the module can determine what should be run or not. e.g in the workflow engine redis, when we are only partially loading the module, we do not want to set the Distributed transaction storage
2024-04-02 14:10:17 +00:00

80 lines
1.6 KiB
JavaScript

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, shutdown } = await loaders({
directory: path.resolve(cwd || process.cwd()),
expressApp: app,
isTest: false,
})
const PORT = await getPort()
return {
shutdown,
container,
app,
port: PORT,
}
}
module.exports = {
bootstrapApp,
startBootstrapApp: async ({
cwd,
env = {},
skipExpressListen = false,
} = {}) => {
const {
app,
port,
container,
shutdown: medusaShutdown,
} = await bootstrapApp({
cwd,
env,
})
let expressServer
setContainer(container)
if (skipExpressListen) {
return
}
const shutdown = async () => {
await Promise.all([expressServer.close(), medusaShutdown()])
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)
})
},
}