Inject config module in tests (#13937)
* add failing test * inject configModule in module test runner * changeset * fix injected configModule
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import { Module } from "@medusajs/framework/utils"
|
||||
import TestService from "./service"
|
||||
|
||||
export const TEST_MODULE = "test"
|
||||
|
||||
export default Module(TEST_MODULE, {
|
||||
service: TestService,
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
export default class TestService {
|
||||
private dependencies: Record<string, any>
|
||||
private options: any
|
||||
|
||||
constructor(dependencies: Record<string, any>, options: any) {
|
||||
this.dependencies = dependencies
|
||||
this.options = options
|
||||
}
|
||||
|
||||
async getDependencies(): Promise<Record<string, any>> {
|
||||
return this.dependencies
|
||||
}
|
||||
|
||||
async getOptions(): Promise<any> {
|
||||
return this.options
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export default class InternalService {
|
||||
private dependencies: Record<string, any>
|
||||
|
||||
constructor(dependencies: Record<string, any>) {
|
||||
this.dependencies = dependencies
|
||||
}
|
||||
|
||||
async getDependencies(): Promise<Record<string, any>> {
|
||||
return this.dependencies
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
|
||||
import TestService from "../__fixtures__/test-module/service"
|
||||
import InternalService from "../__fixtures__/test-module/services/internal"
|
||||
import { moduleIntegrationTestRunner } from "../module-test-runner"
|
||||
|
||||
moduleIntegrationTestRunner<TestService>({
|
||||
moduleName: "test",
|
||||
resolve: "./__fixtures__/test-module",
|
||||
moduleOptions: {
|
||||
option1: "value1",
|
||||
},
|
||||
testSuite: ({ service }) => {
|
||||
describe("Module Test Runner", () => {
|
||||
it("should inject all basic dependencies on the main service", async () => {
|
||||
const dependencies = await service.getDependencies()
|
||||
expect(dependencies[ContainerRegistrationKeys.LOGGER]).toBeDefined()
|
||||
expect(
|
||||
dependencies[ContainerRegistrationKeys.PG_CONNECTION]
|
||||
).toBeDefined()
|
||||
expect(dependencies[Modules.EVENT_BUS]).toBeDefined()
|
||||
expect(dependencies["baseRepository"]).toBeDefined()
|
||||
|
||||
const configModule =
|
||||
dependencies[ContainerRegistrationKeys.CONFIG_MODULE]
|
||||
expect(configModule).toBeDefined()
|
||||
expect(configModule.modules["test"]?.options?.option1).toBe("value1")
|
||||
})
|
||||
|
||||
it("should inject internal services on the main service", async () => {
|
||||
const dependencies = await service.getDependencies()
|
||||
expect(dependencies["internalService"]).toBeInstanceOf(InternalService)
|
||||
})
|
||||
|
||||
it("should inject basic dependencies on internal services", async () => {
|
||||
const internalService = (await service.getDependencies())[
|
||||
"internalService"
|
||||
] as InternalService
|
||||
const dependencies = await internalService.getDependencies()
|
||||
expect(dependencies[ContainerRegistrationKeys.LOGGER]).toBeDefined()
|
||||
expect(
|
||||
dependencies[ContainerRegistrationKeys.PG_CONNECTION]
|
||||
).toBeDefined()
|
||||
expect(dependencies[Modules.EVENT_BUS]).toBeDefined()
|
||||
expect(dependencies["baseRepository"]).toBeDefined()
|
||||
|
||||
const configModule =
|
||||
dependencies[ContainerRegistrationKeys.CONFIG_MODULE]
|
||||
expect(configModule).toBeDefined()
|
||||
expect(configModule.modules["test"]?.options?.option1).toBe("value1")
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,3 +1,4 @@
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
DmlEntity,
|
||||
@@ -8,7 +9,6 @@ import {
|
||||
normalizeImportPathWithSource,
|
||||
toMikroOrmEntities,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
import * as fs from "fs"
|
||||
import { getDatabaseURL, getMikroOrmWrapper, TestDatabase } from "./database"
|
||||
import { initModules, InitModulesOptions } from "./init-modules"
|
||||
@@ -183,6 +183,9 @@ class ModuleTestRunner<TService = any> {
|
||||
[ContainerRegistrationKeys.PG_CONNECTION]: this.connection,
|
||||
[Modules.EVENT_BUS]: new MockEventBusService(),
|
||||
[ContainerRegistrationKeys.LOGGER]: console,
|
||||
[ContainerRegistrationKeys.CONFIG_MODULE]: {
|
||||
modules: this.modulesConfig,
|
||||
},
|
||||
...this.injectedDependencies,
|
||||
},
|
||||
modulesConfig: this.modulesConfig,
|
||||
|
||||
Reference in New Issue
Block a user