fix(medusa-test-utils): Run with modules without models (#9062)
* fix(medusa-test-utils): Run with modules without models * cleanup * rm dummy model * rm dummy model * improve models loading * find models * find models * find models * finalize * cleanup * fix deps * fix deps * fix deps * fix deps * fix deps
This commit is contained in:
@@ -23,51 +23,30 @@
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@medusajs/framework": "^0.0.1",
|
||||
"@medusajs/modules-sdk": "*",
|
||||
"@medusajs/types": "^1.11.16",
|
||||
"@mikro-orm/core": "5.9.7",
|
||||
"@mikro-orm/migrations": "5.9.7",
|
||||
"@mikro-orm/postgresql": "5.9.7",
|
||||
"cross-env": "^5.2.1",
|
||||
"express": "^4.18.3",
|
||||
"get-port": "^5.1.0",
|
||||
"jest": "^29.7.0",
|
||||
"pg-god": "^1.0.12",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^5.1.6"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@medusajs/medusa": "*",
|
||||
"@medusajs/modules-sdk": "*",
|
||||
"@mikro-orm/core": "5.9.7",
|
||||
"@mikro-orm/migrations": "5.9.7",
|
||||
"@mikro-orm/postgresql": "5.9.7",
|
||||
"axios": "^0.28.0",
|
||||
"express": "^4.18.3",
|
||||
"pg-god": "^1.0.12"
|
||||
"@medusajs/medusa": "*"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@medusajs/core": {
|
||||
"optional": true
|
||||
},
|
||||
"@medusajs/medusa": {
|
||||
"optional": true
|
||||
},
|
||||
"@medusajs/migrations": {
|
||||
"optional": true
|
||||
},
|
||||
"@medusajs/modules-sdk": {
|
||||
"optional": true
|
||||
},
|
||||
"@medusajs/postgresql": {
|
||||
"optional": true
|
||||
},
|
||||
"axios": {
|
||||
"optional": true
|
||||
},
|
||||
"express": {
|
||||
"optional": true
|
||||
},
|
||||
"pg-god": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/utils": "^1.11.9",
|
||||
"get-port": "^5.1",
|
||||
"axios": "^0.21.4",
|
||||
"randomatic": "^3.1.1"
|
||||
},
|
||||
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808"
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import type { MedusaAppLoader } from "@medusajs/framework"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
|
||||
/**
|
||||
* Initiates the database connection
|
||||
|
||||
@@ -16,8 +16,6 @@ import { configLoaderOverride } from "./medusa-test-runner-utils/config"
|
||||
import { applyEnvVarsToProcess } from "./medusa-test-runner-utils/utils"
|
||||
import { clearInstances } from "./medusa-test-runner-utils/clear-instances"
|
||||
|
||||
const axios = require("axios").default
|
||||
|
||||
const DB_HOST = process.env.DB_HOST
|
||||
const DB_USERNAME = process.env.DB_USERNAME
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD
|
||||
@@ -205,6 +203,8 @@ export function medusaIntegrationTestRunner({
|
||||
await syncLinks(appLoader)
|
||||
}
|
||||
|
||||
const axios = (await import("axios")).default.default
|
||||
|
||||
const cancelTokenSource = axios.CancelToken.source()
|
||||
|
||||
globalContainer = containerRes
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
DmlEntity,
|
||||
loadModels,
|
||||
ModulesSdkUtils,
|
||||
normalizeImportPathWithSource,
|
||||
toMikroOrmEntities,
|
||||
} from "@medusajs/utils"
|
||||
import { TestDatabase, getDatabaseURL, getMikroOrmWrapper } from "./database"
|
||||
import { InitModulesOptions, initModules } from "./init-modules"
|
||||
import { getDatabaseURL, getMikroOrmWrapper, TestDatabase } from "./database"
|
||||
import { initModules, InitModulesOptions } from "./init-modules"
|
||||
import { default as MockEventBusService } from "./mock-event-bus-service"
|
||||
import * as fs from "fs"
|
||||
|
||||
export interface SuiteOptions<TService = unknown> {
|
||||
MikroOrmWrapper: TestDatabase
|
||||
@@ -17,6 +21,46 @@ export interface SuiteOptions<TService = unknown> {
|
||||
}
|
||||
}
|
||||
|
||||
function createMikroOrmWrapper(options: {
|
||||
moduleModels?: (Function | DmlEntity<any, any>)[]
|
||||
resolve?: string
|
||||
dbConfig: any
|
||||
}): {
|
||||
MikroOrmWrapper: TestDatabase
|
||||
models: (Function | DmlEntity<any, any>)[]
|
||||
} {
|
||||
let moduleModels: (Function | DmlEntity<any, any>)[] =
|
||||
options.moduleModels ?? []
|
||||
|
||||
if (!options.moduleModels) {
|
||||
const basePath = normalizeImportPathWithSource(
|
||||
options.resolve ?? process.cwd()
|
||||
)
|
||||
|
||||
const modelsPath = fs.existsSync(`${basePath}/dist/models`)
|
||||
? "/dist/models"
|
||||
: fs.existsSync(`${basePath}/models`)
|
||||
? "/models"
|
||||
: ""
|
||||
|
||||
if (modelsPath) {
|
||||
moduleModels = loadModels(`${basePath}${modelsPath}`)
|
||||
} else {
|
||||
moduleModels = []
|
||||
}
|
||||
}
|
||||
|
||||
moduleModels = toMikroOrmEntities(moduleModels)
|
||||
|
||||
const MikroOrmWrapper = getMikroOrmWrapper({
|
||||
mikroOrmEntities: moduleModels,
|
||||
clientUrl: options.dbConfig.clientUrl,
|
||||
schema: options.dbConfig.schema,
|
||||
})
|
||||
|
||||
return { MikroOrmWrapper, models: moduleModels }
|
||||
}
|
||||
|
||||
export function moduleIntegrationTestRunner<TService = any>({
|
||||
moduleName,
|
||||
moduleModels,
|
||||
@@ -43,9 +87,6 @@ export function moduleIntegrationTestRunner<TService = any>({
|
||||
|
||||
process.env.LOG_LEVEL = "error"
|
||||
|
||||
moduleModels ??= Object.values(require(`${process.cwd()}/src/models`))
|
||||
moduleModels = toMikroOrmEntities(moduleModels)
|
||||
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
const dbName = `medusa-${moduleName.toLowerCase()}-integration-${tempName}`
|
||||
|
||||
@@ -58,12 +99,14 @@ export function moduleIntegrationTestRunner<TService = any>({
|
||||
// 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 { MikroOrmWrapper, models } = createMikroOrmWrapper({
|
||||
moduleModels,
|
||||
resolve,
|
||||
dbConfig,
|
||||
})
|
||||
|
||||
moduleModels = models
|
||||
|
||||
const modulesConfig_ = {
|
||||
[moduleName]: {
|
||||
definition: moduleSdkImports.ModulesDefinition[moduleName],
|
||||
@@ -117,7 +160,9 @@ export function moduleIntegrationTestRunner<TService = any>({
|
||||
} as SuiteOptions<TService>
|
||||
|
||||
const beforeEach_ = async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
if (moduleModels.length) {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
}
|
||||
const output = await initModules(moduleOptions_)
|
||||
shutdown = output.shutdown
|
||||
medusaApp = output.medusaApp
|
||||
@@ -125,7 +170,9 @@ export function moduleIntegrationTestRunner<TService = any>({
|
||||
}
|
||||
|
||||
const afterEach_ = async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
if (moduleModels.length) {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
}
|
||||
await shutdown()
|
||||
moduleService = {}
|
||||
medusaApp = {}
|
||||
|
||||
Reference in New Issue
Block a user