fix(module-sdk): Shared modules loading (#4611)

* WIP

* tests wording

* Create nasty-files-type.md

* cleanup
This commit is contained in:
Adrien de Peretti
2023-07-27 12:53:31 +02:00
committed by GitHub
parent 8d0ce0af06
commit 379c83933e
5 changed files with 35 additions and 33 deletions
@@ -23,7 +23,7 @@ describe("modules loader", () => {
container = createMedusaContainer()
})
it("registers service as undefined in container when no resolution path is given", async () => {
it("should register the service as undefined in the container when no resolution path is given", async () => {
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
resolutionPath: false,
@@ -52,7 +52,7 @@ describe("modules loader", () => {
expect(testService).toBe(undefined)
})
it("registers service ", async () => {
it("should register the service ", async () => {
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
resolutionPath: "@modules/default",
@@ -93,7 +93,7 @@ describe("modules loader", () => {
expect(typeof testService).toEqual("object")
})
it("runs defined loaders and logs error", async () => {
it("should run the defined loaders and logs the errors if something fails", async () => {
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
resolutionPath: "@modules/brokenloader",
@@ -121,7 +121,7 @@ describe("modules loader", () => {
)
})
it("logs error if no service is defined", async () => {
it("should log the errors if no service is defined", async () => {
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
resolutionPath: "@modules/no-service",
@@ -149,7 +149,7 @@ describe("modules loader", () => {
)
})
it("throws error if no service is defined and module is required", async () => {
it("should throw an error if no service is defined and the module is required", async () => {
expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
@@ -181,7 +181,7 @@ describe("modules loader", () => {
}
})
it("throws error if default package isn't found and module is required", async () => {
it("should throw an error if the default package isn't found and the module is required", async () => {
expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
@@ -213,7 +213,7 @@ describe("modules loader", () => {
}
})
it("throws error if no scope is defined to the module", async () => {
it("should throw an error if no scope is defined on the module declaration", async () => {
expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
@@ -245,7 +245,7 @@ describe("modules loader", () => {
}
})
it("throws error if resources is not set when scope is defined as internal", async () => {
it("should throw an error if the resources is not set when scope is defined as internal", async () => {
expect.assertions(1)
const moduleResolutions: Record<string, ModuleResolution> = {
testService: {
@@ -69,24 +69,20 @@ export async function loadInternalModule(
}
}
const localContainer =
resources === MODULE_RESOURCE_TYPE.ISOLATED
? createMedusaContainer()
: (container.createScope() as MedusaContainer)
const localContainer = createMedusaContainer()
if (resources === MODULE_RESOURCE_TYPE.ISOLATED) {
const moduleDependencies = resolution?.dependencies ?? []
const dependencies = resolution?.dependencies ?? []
if (resources === MODULE_RESOURCE_TYPE.SHARED) {
dependencies.push("manager", "configModule")
}
for (const dependency of moduleDependencies) {
localContainer.register(
dependency,
asFunction(() => {
return container.hasRegistration(dependency)
? container.resolve(dependency)
: undefined
})
)
}
for (const dependency of dependencies) {
localContainer.register(
dependency,
asFunction(() => {
return container.resolve(dependency, { allowUnregistered: true })
})
)
}
const moduleLoaders = loadedModule?.loaders ?? []