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
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import {
|
||||
MedusaApp,
|
||||
MedusaModule,
|
||||
MedusaModuleConfig,
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
ModuleJoinerConfig,
|
||||
} from "@medusajs/modules-sdk"
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
ModulesSdkUtils,
|
||||
@@ -16,7 +15,12 @@ export interface InitModulesOptions {
|
||||
clientUrl: string
|
||||
schema?: string
|
||||
}
|
||||
modulesConfig: MedusaModuleConfig
|
||||
modulesConfig: {
|
||||
[key: string]:
|
||||
| string
|
||||
| boolean
|
||||
| Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
|
||||
}
|
||||
joinerConfig?: ModuleJoinerConfig[]
|
||||
preventConnectionDestroyWarning?: boolean
|
||||
}
|
||||
@@ -28,6 +32,8 @@ export async function initModules({
|
||||
joinerConfig,
|
||||
preventConnectionDestroyWarning = false,
|
||||
}: InitModulesOptions) {
|
||||
const moduleSdkImports = require("@medusajs/modules-sdk")
|
||||
|
||||
injectedDependencies ??= {}
|
||||
|
||||
let sharedPgConnection =
|
||||
@@ -44,7 +50,7 @@ export async function initModules({
|
||||
sharedPgConnection
|
||||
}
|
||||
|
||||
const medusaApp = await MedusaApp({
|
||||
const medusaApp = await moduleSdkImports.MedusaApp({
|
||||
modulesConfig,
|
||||
servicesConfig: joinerConfig,
|
||||
injectedDependencies,
|
||||
@@ -64,7 +70,7 @@ export async function initModules({
|
||||
)
|
||||
}
|
||||
}
|
||||
MedusaModule.clearInstances()
|
||||
moduleSdkImports.MedusaModule.clearInstances()
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -34,6 +34,10 @@ const dbTestUtilFactory = (): any => ({
|
||||
schema,
|
||||
}: { forceDelete?: string[]; schema?: string } = {}) {
|
||||
forceDelete ??= []
|
||||
if (!this.db_) {
|
||||
return
|
||||
}
|
||||
|
||||
const manager = this.db_.manager
|
||||
|
||||
schema ??= "public"
|
||||
@@ -90,9 +94,7 @@ export function medusaIntegrationTestRunner({
|
||||
schema?: string
|
||||
debug?: boolean
|
||||
force_modules_migration?: boolean
|
||||
testSuite: <TService = unknown>(
|
||||
options: MedusaSuiteOptions<TService>
|
||||
) => () => void
|
||||
testSuite: <TService = unknown>(options: MedusaSuiteOptions<TService>) => void
|
||||
}) {
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
moduleName = moduleName ?? Math.random().toString(36).substring(7)
|
||||
@@ -147,37 +149,63 @@ export function medusaIntegrationTestRunner({
|
||||
|
||||
const beforeAll_ = async () => {
|
||||
await dbUtils.create(dbName)
|
||||
const { dbDataSource, pgConnection } = await initDb({
|
||||
cwd,
|
||||
env,
|
||||
force_modules_migration,
|
||||
database_extra: {},
|
||||
dbUrl: dbConfig.clientUrl,
|
||||
dbSchema: dbConfig.schema,
|
||||
})
|
||||
dbUtils.db_ = dbDataSource
|
||||
dbUtils.pgConnection_ = pgConnection
|
||||
|
||||
const {
|
||||
shutdown: serverShutdown,
|
||||
container: container_,
|
||||
port,
|
||||
} = await startBootstrapApp({
|
||||
cwd,
|
||||
env,
|
||||
})
|
||||
let dataSourceRes
|
||||
let pgConnectionRes
|
||||
|
||||
try {
|
||||
const { dbDataSource, pgConnection } = await initDb({
|
||||
cwd,
|
||||
env,
|
||||
force_modules_migration,
|
||||
database_extra: {},
|
||||
dbUrl: dbConfig.clientUrl,
|
||||
dbSchema: dbConfig.schema,
|
||||
})
|
||||
|
||||
dataSourceRes = dbDataSource
|
||||
pgConnectionRes = pgConnection
|
||||
} catch (error) {
|
||||
console.error("Error initializing database", error?.message)
|
||||
throw error
|
||||
}
|
||||
|
||||
dbUtils.db_ = dataSourceRes
|
||||
dbUtils.pgConnection_ = pgConnectionRes
|
||||
|
||||
let containerRes
|
||||
let serverShutdownRes
|
||||
let portRes
|
||||
try {
|
||||
const {
|
||||
shutdown = () => void 0,
|
||||
container,
|
||||
port,
|
||||
} = await startBootstrapApp({
|
||||
cwd,
|
||||
env,
|
||||
})
|
||||
|
||||
containerRes = container
|
||||
serverShutdownRes = shutdown
|
||||
portRes = port
|
||||
} catch (error) {
|
||||
console.error("Error starting the app", error?.message)
|
||||
throw error
|
||||
}
|
||||
|
||||
const cancelTokenSource = axios.CancelToken.source()
|
||||
apiUtils = axios.create({
|
||||
baseURL: `http://localhost:${port}`,
|
||||
cancelToken: cancelTokenSource.token,
|
||||
})
|
||||
|
||||
container = container_
|
||||
container = containerRes
|
||||
shutdown = async () => {
|
||||
await serverShutdown()
|
||||
await serverShutdownRes()
|
||||
cancelTokenSource.cancel("Request canceled by shutdown")
|
||||
}
|
||||
|
||||
apiUtils = axios.create({
|
||||
baseURL: `http://localhost:${portRes}`,
|
||||
cancelToken: cancelTokenSource.token,
|
||||
})
|
||||
}
|
||||
|
||||
const beforeEach_ = async () => {
|
||||
@@ -191,26 +219,37 @@ export function medusaIntegrationTestRunner({
|
||||
const copiedContainer = createMedusaContainer({}, container)
|
||||
|
||||
if (process.env.MEDUSA_FF_MEDUSA_V2 != "true") {
|
||||
const defaultLoader =
|
||||
require("@medusajs/medusa/dist/loaders/defaults").default
|
||||
await defaultLoader({
|
||||
container: copiedContainer,
|
||||
})
|
||||
try {
|
||||
const defaultLoader =
|
||||
require("@medusajs/medusa/dist/loaders/defaults").default
|
||||
await defaultLoader({
|
||||
container: copiedContainer,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error runner medusa loaders", error?.message)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const medusaAppLoaderRunner =
|
||||
require("@medusajs/medusa/dist/loaders/medusa-app").runModulesLoader
|
||||
await medusaAppLoaderRunner({
|
||||
container: copiedContainer,
|
||||
configModule: container.resolve("configModule"),
|
||||
})
|
||||
try {
|
||||
const medusaAppLoaderRunner =
|
||||
require("@medusajs/medusa/dist/loaders/medusa-app").runModulesLoader
|
||||
await medusaAppLoaderRunner({
|
||||
container: copiedContainer,
|
||||
configModule: container.resolve("configModule"),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error runner modules loaders", error?.message)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const afterEach_ = async () => {
|
||||
try {
|
||||
await dbUtils.teardown({ schema })
|
||||
} catch (error) {
|
||||
console.error("Error tearing down database:", error)
|
||||
console.error("Error tearing down database:", error?.message)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { InitModulesOptions, initModules } from "./init-modules"
|
||||
import { MedusaAppOutput, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||
import { TestDatabase, getDatabaseURL, getMikroOrmWrapper } from "./database"
|
||||
import { initModules, InitModulesOptions } from "./init-modules"
|
||||
import { getDatabaseURL, getMikroOrmWrapper, TestDatabase } from "./database"
|
||||
|
||||
import { MockEventBusService } from "."
|
||||
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
export interface SuiteOptions<TService = unknown> {
|
||||
MikroOrmWrapper: TestDatabase
|
||||
medusaApp: MedusaAppOutput
|
||||
medusaApp: any
|
||||
service: TService
|
||||
dbConfig: {
|
||||
schema: string
|
||||
@@ -37,6 +36,8 @@ export function moduleIntegrationTestRunner({
|
||||
debug?: boolean
|
||||
testSuite: <TService = unknown>(options: SuiteOptions<TService>) => () => void
|
||||
}) {
|
||||
const moduleSdkImports = require("@medusajs/modules-sdk")
|
||||
|
||||
process.env.LOG_LEVEL = "error"
|
||||
|
||||
moduleModels ??= Object.values(require(`${process.cwd()}/src/models`))
|
||||
@@ -62,7 +63,7 @@ export function moduleIntegrationTestRunner({
|
||||
|
||||
const modulesConfig_ = {
|
||||
[moduleName]: {
|
||||
definition: ModulesDefinition[moduleName],
|
||||
definition: moduleSdkImports.ModulesDefinition[moduleName],
|
||||
resolve,
|
||||
options: {
|
||||
defaultAdapterOptions: {
|
||||
@@ -89,7 +90,7 @@ export function moduleIntegrationTestRunner({
|
||||
|
||||
let shutdown: () => Promise<void>
|
||||
let moduleService
|
||||
let medusaApp: MedusaAppOutput = {} as MedusaAppOutput
|
||||
let medusaApp = {}
|
||||
|
||||
const options = {
|
||||
MikroOrmWrapper,
|
||||
@@ -100,7 +101,7 @@ export function moduleIntegrationTestRunner({
|
||||
return medusaApp[prop]
|
||||
},
|
||||
}
|
||||
) as MedusaAppOutput,
|
||||
),
|
||||
service: new Proxy(
|
||||
{},
|
||||
{
|
||||
@@ -123,7 +124,7 @@ export function moduleIntegrationTestRunner({
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
await shutdown()
|
||||
moduleService = {}
|
||||
medusaApp = {} as MedusaAppOutput
|
||||
medusaApp = {}
|
||||
}
|
||||
|
||||
return describe("", () => {
|
||||
|
||||
Reference in New Issue
Block a user