feat(fulfillment): implementation part 2 (#6408)
**What**
> [!NOTE]
> I can see this pr becoming huge, so I d like to get this partial one merged 👍
- Fixes shared connection usage (mikro orm compare the instance to its own package and therefore was resulting in not trully reusing the provided connection leading to exhausting the connection pool as multiple connections was created and end up not being all destroyed properly under the hood, discovered in my integration tests)
- Create shipping options method implementation
- DTO's definition and service interface update
- integration tests
- Re work of the indexes with new util update
- Test runner utils to remove a big chunk of the boilerplate of the packages integrations
FIXES CORE-1742
This commit is contained in:
committed by
GitHub
parent
680dfcdad3
commit
1d91b7429b
@@ -0,0 +1,128 @@
|
||||
import { getDatabaseURL, getMikroOrmWrapper, TestDatabase } from "./database"
|
||||
import { MedusaAppOutput, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||
import { initModules, InitModulesOptions } from "./init-modules"
|
||||
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
export interface SuiteOptions<TService = unknown> {
|
||||
MikroOrmWrapper: TestDatabase
|
||||
medusaApp: MedusaAppOutput
|
||||
service: TService
|
||||
dbConfig: {
|
||||
schema: string
|
||||
clientUrl: string
|
||||
}
|
||||
}
|
||||
|
||||
export function moduleIntegrationTestRunner({
|
||||
moduleName,
|
||||
moduleModels,
|
||||
joinerConfig = [],
|
||||
schema = "public",
|
||||
testSuite,
|
||||
}: {
|
||||
moduleName: string
|
||||
moduleModels?: any[]
|
||||
joinerConfig?: any[]
|
||||
schema?: string
|
||||
dbName?: string
|
||||
testSuite: <TService = unknown>(options: SuiteOptions<TService>) => () => void
|
||||
}) {
|
||||
moduleModels = Object.values(require(`${process.cwd()}/src/models`))
|
||||
// migrationPath ??= process.cwd() + "/src/migrations/!(*.d).{js,ts,cjs}"
|
||||
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
const dbName = `medusa-${moduleName.toLowerCase()}-integration-${tempName}`
|
||||
|
||||
const dbConfig = {
|
||||
clientUrl: getDatabaseURL(dbName),
|
||||
schema,
|
||||
}
|
||||
|
||||
// Use a unique connection for all the entire suite
|
||||
const connection = ModulesSdkUtils.createPgConnection(dbConfig)
|
||||
|
||||
const MikroOrmWrapper = getMikroOrmWrapper({
|
||||
mikroOrmEntities: moduleModels,
|
||||
clientUrl: dbConfig.clientUrl,
|
||||
schema: dbConfig.schema,
|
||||
})
|
||||
|
||||
const modulesConfig_ = {
|
||||
[moduleName]: {
|
||||
definition: ModulesDefinition[moduleName],
|
||||
options: {
|
||||
defaultAdapterOptions: {
|
||||
database: dbConfig,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const moduleOptions: InitModulesOptions = {
|
||||
injectedDependencies: {
|
||||
[ContainerRegistrationKeys.PG_CONNECTION]: connection,
|
||||
},
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: dbConfig,
|
||||
joinerConfig,
|
||||
preventConnectionDestroyWarning: true,
|
||||
}
|
||||
|
||||
let shutdown: () => Promise<void>
|
||||
let moduleService
|
||||
let medusaApp: MedusaAppOutput = {} as MedusaAppOutput
|
||||
|
||||
const options = {
|
||||
MikroOrmWrapper,
|
||||
medusaApp: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: (target, prop) => {
|
||||
return medusaApp[prop]
|
||||
},
|
||||
}
|
||||
) as MedusaAppOutput,
|
||||
service: new Proxy(
|
||||
{},
|
||||
{
|
||||
get: (target, prop) => {
|
||||
return moduleService[prop]
|
||||
},
|
||||
}
|
||||
),
|
||||
} as SuiteOptions
|
||||
|
||||
const beforeEach_ = async () => {
|
||||
try {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
const output = await initModules(moduleOptions)
|
||||
shutdown = output.shutdown
|
||||
medusaApp = output.medusaApp
|
||||
moduleService = output.medusaApp.modules[moduleName]
|
||||
} catch (error) {
|
||||
console.error("Error setting up database:", error)
|
||||
}
|
||||
}
|
||||
|
||||
const afterEach_ = async () => {
|
||||
try {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
await shutdown()
|
||||
moduleService = {}
|
||||
medusaApp = {} as MedusaAppOutput
|
||||
} catch (error) {
|
||||
console.error("Error tearing down database:", error)
|
||||
}
|
||||
}
|
||||
|
||||
return describe("", () => {
|
||||
beforeEach(beforeEach_)
|
||||
afterEach(afterEach_)
|
||||
afterAll(async () => {
|
||||
await (connection as any).context?.destroy()
|
||||
await (connection as any).destroy()
|
||||
})
|
||||
|
||||
testSuite(options)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user