diff --git a/packages/authentication/integration-tests/__tests__/services/module/auth-provider.spec.ts b/packages/authentication/integration-tests/__tests__/services/module/auth-provider.spec.ts index 59b80e01f6..2cbc5c92ad 100644 --- a/packages/authentication/integration-tests/__tests__/services/module/auth-provider.spec.ts +++ b/packages/authentication/integration-tests/__tests__/services/module/auth-provider.spec.ts @@ -1,33 +1,41 @@ -import { SqlEntityManager } from "@mikro-orm/postgresql" - -import { MikroOrmWrapper } from "../../../utils" -import { createAuthProviders } from "../../../__fixtures__/auth-provider" import { IAuthenticationModuleService } from "@medusajs/types" -import { initialize } from "../../../../src" -import { DB_URL } from "@medusajs/pricing/integration-tests/utils" +import { MikroOrmWrapper } from "../../../utils" +import { SqlEntityManager } from "@mikro-orm/postgresql" +import { createAuthProviders } from "../../../__fixtures__/auth-provider" import { createAuthUsers } from "../../../__fixtures__/auth-user" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils/dist" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) describe("AuthenticationModuleService - AuthProvider", () => { let service: IAuthenticationModuleService let testManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.AUTHENTICATION] + + shutdownFunc = shutdown + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() testManager = MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - await createAuthProviders(testManager) await createAuthUsers(testManager) }) + afterAll(async () => { + await shutdownFunc() + }) + afterEach(async () => { await MikroOrmWrapper.clearDatabase() }) diff --git a/packages/authentication/integration-tests/__tests__/services/module/auth-user.spec.ts b/packages/authentication/integration-tests/__tests__/services/module/auth-user.spec.ts index 677fd8e35f..3f8c0d3f9d 100644 --- a/packages/authentication/integration-tests/__tests__/services/module/auth-user.spec.ts +++ b/packages/authentication/integration-tests/__tests__/services/module/auth-user.spec.ts @@ -3,27 +3,32 @@ import { SqlEntityManager } from "@mikro-orm/postgresql" import { MikroOrmWrapper } from "../../../utils" import { createAuthProviders } from "../../../__fixtures__/auth-provider" import { createAuthUsers } from "../../../__fixtures__/auth-user" -import { DB_URL } from "@medusajs/pricing/integration-tests/utils" import { IAuthenticationModuleService } from "@medusajs/types" -import { initialize } from "../../../../src" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils/dist" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) describe("AuthenticationModuleService - AuthUser", () => { let service: IAuthenticationModuleService let testManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.AUTHENTICATION] + + shutdownFunc = shutdown + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() testManager = MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - await createAuthProviders(testManager) await createAuthUsers(testManager) }) @@ -32,6 +37,10 @@ describe("AuthenticationModuleService - AuthUser", () => { await MikroOrmWrapper.clearDatabase() }) + afterAll(async () => { + await shutdownFunc() + }) + describe("listAuthUsers", () => { it("should list authUsers", async () => { const authUsers = await service.listAuthUsers() @@ -237,7 +246,7 @@ describe("AuthenticationModuleService - AuthUser", () => { { id: "test", provider_id: "manual", - entity_id: "test" + entity_id: "test", }, ]) diff --git a/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts b/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts index cefb712ce1..2f27b56a12 100644 --- a/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts +++ b/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts @@ -1,28 +1,32 @@ -import { DB_URL } from "@medusajs/pricing/integration-tests/utils" import { IAuthenticationModuleService } from "@medusajs/types" -import { MedusaModule } from "@medusajs/modules-sdk" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" import { MikroOrmWrapper } from "../../../utils" import { SqlEntityManager } from "@mikro-orm/postgresql" import { createAuthProviders } from "../../../__fixtures__/auth-provider" -import { initialize } from "../../../../src" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils/dist" jest.setTimeout(30000) describe("AuthenticationModuleService - AuthProvider", () => { let service: IAuthenticationModuleService let testManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.AUTHENTICATION] + + shutdownFunc = shutdown + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() testManager = MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - if (service.__hooks?.onApplicationStart) { await service.__hooks.onApplicationStart() } @@ -33,6 +37,10 @@ describe("AuthenticationModuleService - AuthProvider", () => { MedusaModule.clearInstances() }) + afterAll(async () => { + await shutdownFunc() + }) + describe("listAuthProviders", () => { it("should list default AuthProviders registered by loaders", async () => { const authProviders = await service.listAuthProviders() diff --git a/packages/authentication/integration-tests/__tests__/services/providers/username-password.spec.ts b/packages/authentication/integration-tests/__tests__/services/providers/username-password.spec.ts index b8556ea1aa..e667444b50 100644 --- a/packages/authentication/integration-tests/__tests__/services/providers/username-password.spec.ts +++ b/packages/authentication/integration-tests/__tests__/services/providers/username-password.spec.ts @@ -1,12 +1,12 @@ -import { DB_URL } from "@medusajs/pricing/integration-tests/utils" import { IAuthenticationModuleService } from "@medusajs/types" -import { MedusaModule } from "@medusajs/modules-sdk" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" import { MikroOrmWrapper } from "../../../utils" import Scrypt from "scrypt-kdf" import { SqlEntityManager } from "@mikro-orm/postgresql" import { createAuthProviders } from "../../../__fixtures__/auth-provider" import { createAuthUsers } from "../../../__fixtures__/auth-user" -import { initialize } from "../../../../src" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" jest.setTimeout(30000) const seedDefaultData = async (testManager) => { @@ -17,19 +17,27 @@ const seedDefaultData = async (testManager) => { describe("AuthenticationModuleService - AuthProvider", () => { let service: IAuthenticationModuleService let testManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.AUTHENTICATION] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() testManager = MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - - if(service.__hooks?.onApplicationStart) { + if (service.__hooks?.onApplicationStart) { await service.__hooks.onApplicationStart() } }) @@ -70,8 +78,7 @@ describe("AuthenticationModuleService - AuthProvider", () => { success: true, authUser: expect.objectContaining({ entity_id: email, - provider_metadata: { - }, + provider_metadata: {}, }), }) }) diff --git a/packages/authentication/integration-tests/utils/get-init-module-config.ts b/packages/authentication/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000..5ba9ea8c9b --- /dev/null +++ b/packages/authentication/integration-tests/utils/get-init-module-config.ts @@ -0,0 +1,33 @@ +import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" + +import { DB_URL } from "./database" + +export function getInitModuleConfig() { + const moduleOptions = { + defaultAdapterOptions: { + database: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_AUTHENTICATION_DB_SCHEMA, + }, + }, + } + + const injectedDependencies = {} + + const modulesConfig_ = { + [Modules.AUTHENTICATION]: { + definition: ModulesDefinition[Modules.AUTHENTICATION], + options: moduleOptions, + }, + } + + return { + injectedDependencies, + modulesConfig: modulesConfig_, + databaseConfig: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_AUTHENTICATION_DB_SCHEMA, + }, + joinerConfig: [], + } +} diff --git a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index fc6a2033a1..e7c1b602a5 100644 --- a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -1,22 +1,32 @@ import { ICartModuleService } from "@medusajs/types" import { CheckConstraintViolationException } from "@mikro-orm/core" -import { initialize } from "../../../../src/initialize" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { Modules } from "@medusajs/modules-sdk" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" jest.setTimeout(30000) describe("Cart Module Service", () => { let service: ICartModuleService + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.CART] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_CART_DB_SCHEMA, - }, - }) }) afterEach(async () => { diff --git a/packages/cart/integration-tests/utils/get-init-module-config.ts b/packages/cart/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000..574ac50a5d --- /dev/null +++ b/packages/cart/integration-tests/utils/get-init-module-config.ts @@ -0,0 +1,33 @@ +import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" + +import { DB_URL } from "./database" + +export function getInitModuleConfig() { + const moduleOptions = { + defaultAdapterOptions: { + database: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_CART_DB_SCHEMA, + }, + }, + } + + const injectedDependencies = {} + + const modulesConfig_ = { + [Modules.CART]: { + definition: ModulesDefinition[Modules.CART], + options: moduleOptions, + }, + } + + return { + injectedDependencies, + modulesConfig: modulesConfig_, + databaseConfig: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_CART_DB_SCHEMA, + }, + joinerConfig: [], + } +} diff --git a/packages/customer/integration-tests/__tests__/services/customer-module/index.spec.ts b/packages/customer/integration-tests/__tests__/services/customer-module/index.spec.ts index dbc60d896f..40c87afeb2 100644 --- a/packages/customer/integration-tests/__tests__/services/customer-module/index.spec.ts +++ b/packages/customer/integration-tests/__tests__/services/customer-module/index.spec.ts @@ -1,21 +1,31 @@ import { ICustomerModuleService } from "@medusajs/types" -import { initialize } from "../../../../src/initialize" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" jest.setTimeout(30000) describe("Customer Module Service", () => { let service: ICustomerModuleService + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.CUSTOMER] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_CUSTOMER_DB_SCHEMA, - }, - }) }) afterEach(async () => { diff --git a/packages/customer/integration-tests/utils/get-init-module-config.ts b/packages/customer/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000..c1f92f6e86 --- /dev/null +++ b/packages/customer/integration-tests/utils/get-init-module-config.ts @@ -0,0 +1,33 @@ +import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" + +import { DB_URL } from "./database" + +export function getInitModuleConfig() { + const moduleOptions = { + defaultAdapterOptions: { + database: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_CUSTOMER_DB_SCHEMA, + }, + }, + } + + const injectedDependencies = {} + + const modulesConfig_ = { + [Modules.CUSTOMER]: { + definition: ModulesDefinition[Modules.CUSTOMER], + options: moduleOptions, + }, + } + + return { + injectedDependencies, + modulesConfig: modulesConfig_, + databaseConfig: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_CUSTOMER_DB_SCHEMA, + }, + joinerConfig: [], + } +} diff --git a/packages/medusa-test-utils/package.json b/packages/medusa-test-utils/package.json index 82ee9e7653..e2817df816 100644 --- a/packages/medusa-test-utils/package.json +++ b/packages/medusa-test-utils/package.json @@ -30,6 +30,8 @@ "typescript": "^5.1.6" }, "dependencies": { + "@medusajs/modules-sdk": "workspace:^", + "@medusajs/utils": "workspace:^", "@mikro-orm/migrations": "5.9.7", "@mikro-orm/postgresql": "5.9.7", "medusa-core-utils": "^1.2.1", diff --git a/packages/medusa-test-utils/src/index.ts b/packages/medusa-test-utils/src/index.ts index 4a01de7064..8351d21097 100644 --- a/packages/medusa-test-utils/src/index.ts +++ b/packages/medusa-test-utils/src/index.ts @@ -3,3 +3,4 @@ export { default as IdMap } from "./id-map" export * as JestUtils from "./jest" export { default as MockManager } from "./mock-manager" export { default as MockRepository } from "./mock-repository" +export * from "./init-modules" diff --git a/packages/medusa-test-utils/src/init-modules.ts b/packages/medusa-test-utils/src/init-modules.ts new file mode 100644 index 0000000000..067c70a4c3 --- /dev/null +++ b/packages/medusa-test-utils/src/init-modules.ts @@ -0,0 +1,59 @@ +import { knex } from "knex" +import { + MedusaApp, + MedusaModule, + MedusaModuleConfig, + ModuleJoinerConfig, +} from "@medusajs/modules-sdk" +import { ContainerRegistrationKeys } from "@medusajs/utils" + +interface InitModulesOptions { + injectedDependencies?: Record + databaseConfig: { + clientUrl: string + schema?: string + } + modulesConfig: MedusaModuleConfig + joinerConfig?: ModuleJoinerConfig[] +} + +export async function initModules({ + injectedDependencies, + databaseConfig, + modulesConfig, + joinerConfig, +}: InitModulesOptions) { + injectedDependencies ??= {} + + let sharedPgConnection = + injectedDependencies?.[ContainerRegistrationKeys.PG_CONNECTION] + + if (!sharedPgConnection) { + sharedPgConnection = knex({ + client: "pg", + searchPath: databaseConfig.schema, + connection: { + connectionString: databaseConfig.clientUrl, + }, + }) + + injectedDependencies[ContainerRegistrationKeys.PG_CONNECTION] = + sharedPgConnection + } + + const medusaApp = await MedusaApp({ + modulesConfig, + servicesConfig: joinerConfig, + injectedDependencies, + }) + + async function shutdown() { + await (sharedPgConnection as any).context?.destroy() + MedusaModule.clearInstances() + } + + return { + medusaApp, + shutdown, + } +} diff --git a/packages/payment/integration-tests/__tests__/services/payment-module/index.spec.ts b/packages/payment/integration-tests/__tests__/services/payment-module/index.spec.ts index 98ff66df31..c29eb90882 100644 --- a/packages/payment/integration-tests/__tests__/services/payment-module/index.spec.ts +++ b/packages/payment/integration-tests/__tests__/services/payment-module/index.spec.ts @@ -4,12 +4,30 @@ import { SqlEntityManager } from "@mikro-orm/postgresql" import { initialize } from "../../../../src/initialize" import { DB_URL, MikroOrmWrapper } from "../../../utils" import { createPaymentCollections } from "../../../__fixtures__/payment-collection" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) describe("Payment Module Service", () => { let service: IPaymentModuleService let repositoryManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PAYMENT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() diff --git a/packages/payment/integration-tests/utils/get-init-module-config.ts b/packages/payment/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000..6c6c667102 --- /dev/null +++ b/packages/payment/integration-tests/utils/get-init-module-config.ts @@ -0,0 +1,33 @@ +import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" + +import { DB_URL } from "./database" + +export function getInitModuleConfig() { + const moduleOptions = { + defaultAdapterOptions: { + database: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PAYMENT_DB_SCHEMA, + }, + }, + } + + const injectedDependencies = {} + + const modulesConfig_ = { + [Modules.PAYMENT]: { + definition: ModulesDefinition[Modules.PAYMENT], + options: moduleOptions, + }, + } + + return { + injectedDependencies, + modulesConfig: modulesConfig_, + databaseConfig: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PAYMENT_DB_SCHEMA, + }, + joinerConfig: [], + } +} diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts index 25ada46e25..d31c6dfa97 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts @@ -7,9 +7,11 @@ import { import { PriceListType } from "@medusajs/utils" import { SqlEntityManager } from "@mikro-orm/postgresql" import { PriceSet } from "@models" -import { initialize } from "../../../../src" import { seedPriceData } from "../../../__fixtures__/seed-price-data" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) @@ -55,17 +57,26 @@ describe("PricingModule Service - Calculate Price", () => { let testManager: SqlEntityManager let repositoryManager: SqlEntityManager let data!: PriceSet[] + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() testManager = MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) }) afterEach(async () => { diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/currency.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/currency.spec.ts index 7da19d47bc..3c76e717b8 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/currency.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/currency.spec.ts @@ -1,10 +1,11 @@ import { IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Currency } from "@models" - -import { initialize } from "../../../../src" import { createCurrencies } from "../../../__fixtures__/currency" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" describe("PricingModule Service - Currency", () => { let service: IPricingModuleService @@ -12,17 +13,25 @@ describe("PricingModule Service - Currency", () => { let repositoryManager: SqlEntityManager let data!: Currency[] + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) + beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - testManager = MikroOrmWrapper.forkManager() data = await createCurrencies(testManager) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts index 2b8fd005c8..76adb62a22 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/money-amount.spec.ts @@ -1,16 +1,17 @@ import { IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Currency, MoneyAmount } from "@models" - -import { initialize } from "../../../../src" import { createCurrencies } from "../../../__fixtures__/currency" import { createMoneyAmounts } from "../../../__fixtures__/money-amount" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount" import { createPriceSets } from "../../../__fixtures__/price-set" import { createRuleTypes } from "../../../__fixtures__/rule-type" import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" jest.setTimeout(30000) @@ -20,21 +21,27 @@ describe("PricingModule Service - MoneyAmount", () => { let repositoryManager: SqlEntityManager let data!: MoneyAmount[] let currencyData!: Currency[] + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - testManager = MikroOrmWrapper.forkManager() - testManager = await MikroOrmWrapper.forkManager() currencyData = await createCurrencies(testManager) data = await createMoneyAmounts(testManager) }) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts index a67d5dc57d..f659615812 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts @@ -1,11 +1,13 @@ -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" import { IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { initialize } from "../../../../src" import { createPriceLists } from "../../../__fixtures__/price-list" import { createPriceListRules } from "../../../__fixtures__/price-list-rules" import { createRuleTypes } from "../../../__fixtures__/rule-type" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" jest.setTimeout(30000) @@ -13,18 +15,26 @@ describe("PriceListRule Service", () => { let service: IPricingModuleService let testManager: SqlEntityManager let repositoryManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = await MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - testManager = await MikroOrmWrapper.forkManager() await createRuleTypes(testManager) await createPriceLists(testManager) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts index 3dfac8bdc0..774ceec504 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list.spec.ts @@ -1,29 +1,39 @@ -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" import { IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { createCurrencies } from "../../../__fixtures__/currency" import { createPriceLists } from "../../../__fixtures__/price-list" import { createPriceSets } from "../../../__fixtures__/price-set" -import { initialize } from "../../../../src" +import { Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" jest.setTimeout(30000) describe("PriceList Service", () => { let service: IPricingModuleService let testManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() await MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - testManager = await MikroOrmWrapper.forkManager() await createCurrencies(testManager) await createPriceSets(testManager) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts index 7dd4989929..d447055216 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts @@ -1,7 +1,7 @@ import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { initialize, PriceSetMoneyAmount } from "../../../../src" +import { PriceSetMoneyAmount } from "../../../../src" import { createCurrencies } from "../../../__fixtures__/currency" import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import { createPriceRules } from "../../../__fixtures__/price-rule" @@ -9,25 +9,36 @@ import { createPriceSets } from "../../../__fixtures__/price-set" import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount" import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules" import { createRuleTypes } from "../../../__fixtures__/rule-type" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) describe("PricingModule Service - PriceRule", () => { let service: IPricingModuleService let testManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() testManager = MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - await createCurrencies(testManager) await createMoneyAmounts(testManager) await createPriceSets(testManager) @@ -296,6 +307,7 @@ describe("PricingModule Service - PriceRule", () => { price_set: "price-set-1", money_amount: ma.id, title: "test", + rules_count: 0, } ) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts index ac428f6e61..076a0d2c3f 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set-money-amount-rules.spec.ts @@ -1,14 +1,15 @@ import { IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" - -import { initialize } from "../../../../src" import { createCurrencies } from "../../../__fixtures__/currency" import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import { createPriceSets } from "../../../__fixtures__/price-set" import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount" import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules" import { createRuleTypes } from "../../../__fixtures__/rule-type" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) @@ -16,17 +17,25 @@ describe("PricingModule Service - PriceSetMoneyAmountRules", () => { let service: IPricingModuleService let testManager: SqlEntityManager let repositoryManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = await MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - testManager = await MikroOrmWrapper.forkManager() await createCurrencies(testManager) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts index 54b8f4073f..41ffa5c4dd 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts @@ -6,9 +6,12 @@ import { import { SqlEntityManager } from "@mikro-orm/postgresql" import { PriceSet } from "@models" -import { initialize, PriceSetRuleType } from "../../../../src" +import { PriceSetRuleType } from "../../../../src" import { seedPriceData } from "../../../__fixtures__/seed-price-data" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) @@ -31,17 +34,25 @@ describe("PricingModule Service - PriceSet", () => { let service: IPricingModuleService let repositoryManager: SqlEntityManager let data!: PriceSet[] + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - const testManager = MikroOrmWrapper.forkManager() await seedPriceData(testManager) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/rule-type.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/rule-type.spec.ts index 04b5d763d3..2011087b97 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/rule-type.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/rule-type.spec.ts @@ -1,25 +1,34 @@ import { IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" - -import { initialize } from "../../../../src" import { createRuleTypes } from "../../../__fixtures__/rule-type" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" +import { Modules } from "@medusajs/modules-sdk" describe("PricingModuleService ruleType", () => { let service: IPricingModuleService let testManager: SqlEntityManager + let shutdownFunc: () => Promise + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRICING] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() MikroOrmWrapper.forkManager() - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRICING_DB_SCHEMA, - }, - }) - testManager = MikroOrmWrapper.forkManager() await createRuleTypes(testManager) diff --git a/packages/pricing/integration-tests/utils/get-init-module-config.ts b/packages/pricing/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000..88054f9042 --- /dev/null +++ b/packages/pricing/integration-tests/utils/get-init-module-config.ts @@ -0,0 +1,31 @@ +import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" + +import { DB_URL } from "./database" + +export function getInitModuleConfig() { + const moduleOptions = { + defaultAdapterOptions: { + database: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PRICING_DB_SCHEMA, + }, + }, + } + + const modulesConfig_ = { + [Modules.PRODUCT]: true, + [Modules.PRICING]: { + definition: ModulesDefinition[Modules.PRICING], + options: moduleOptions, + }, + } + + return { + modulesConfig: modulesConfig_, + databaseConfig: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PRICING_DB_SCHEMA, + }, + joinerConfig: [], + } +} diff --git a/packages/product/integration-tests/__tests__/module.ts b/packages/product/integration-tests/__tests__/module.ts index 8ca43c3947..9e7a1b75aa 100644 --- a/packages/product/integration-tests/__tests__/module.ts +++ b/packages/product/integration-tests/__tests__/module.ts @@ -1,9 +1,7 @@ -import { MedusaModule } from "@medusajs/modules-sdk" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" import { IProductModuleService } from "@medusajs/types" import { kebabCase } from "@medusajs/utils" import { knex } from "knex" -import { initialize } from "../../src" -import { EventBusService } from "../__fixtures__/event-bus" import * as CustomRepositories from "../__fixtures__/module" import { buildProductAndRelationsData, @@ -11,6 +9,8 @@ import { } from "../__fixtures__/product" import { productsData } from "../__fixtures__/product/data" import { DB_URL, TestDatabase } from "../utils" +import { initModules } from "medusa-test-utils" +import { getInitModuleConfig } from "../utils/get-init-module-config" const sharedPgConnection = knex({ client: "pg", @@ -30,26 +30,28 @@ const afterEach_ = async () => { } describe("Product module", function () { - const eventBus = new EventBusService() - describe("Using built-in data access layer", function () { let module: IProductModuleService + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { const testManager = await beforeEach_() await createProductAndTags(testManager, productsData) - - module = await initialize( - { - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }, - { - eventBusModuleService: eventBus, - } - ) }) afterEach(afterEach_) @@ -72,24 +74,28 @@ describe("Product module", function () { describe("Using custom data access layer", function () { let module + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + const initModulesConfig = getInitModuleConfig({ + repositories: CustomRepositories, + }) + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { const testManager = await beforeEach_() - await createProductAndTags(testManager, productsData) - - module = await initialize( - { - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - repositories: CustomRepositories, - }, - { - eventBusModuleService: eventBus, - } - ) }) afterEach(afterEach_) @@ -114,22 +120,27 @@ describe("Product module", function () { describe("Using custom data access layer and manager", function () { let module + let shutdownFunc: () => Promise + + afterEach(async () => { + await shutdownFunc() + }) beforeEach(async () => { const testManager = await beforeEach_() await createProductAndTags(testManager, productsData) MedusaModule.clearInstances() + const initModulesConfig = getInitModuleConfig({ + manager: testManager, + repositories: CustomRepositories, + }) - module = await initialize( - { - manager: testManager, - repositories: CustomRepositories, - }, - { - eventBusModuleService: eventBus, - } - ) + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown }) afterEach(afterEach_) @@ -154,23 +165,28 @@ describe("Product module", function () { describe("Using an existing connection", function () { let module: IProductModuleService + let shutdownFunc: () => Promise + + afterEach(async () => { + await shutdownFunc() + }) beforeEach(async () => { const testManager = await beforeEach_() await createProductAndTags(testManager, productsData) MedusaModule.clearInstances() - - module = await initialize( - { - database: { - connection: sharedPgConnection, - }, + const initModulesConfig = getInitModuleConfig({ + database: { + connection: sharedPgConnection, }, - { - eventBusModuleService: eventBus, - } - ) + }) + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown }) afterEach(afterEach_) @@ -191,23 +207,27 @@ describe("Product module", function () { describe("create", function () { let module: IProductModuleService let images = ["image-1"] + let shutdownFunc: () => Promise + + afterEach(async () => { + await shutdownFunc() + }) beforeEach(async () => { await beforeEach_() MedusaModule.clearInstances() - - module = await initialize( - { - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, + const initModulesConfig = getInitModuleConfig({ + database: { + connection: sharedPgConnection, }, - { - eventBusModuleService: eventBus, - } - ) + }) + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown }) afterEach(afterEach_) @@ -292,23 +312,24 @@ describe("Product module", function () { describe("softDelete", function () { let module: IProductModuleService let images = ["image-1"] + let shutdownFunc: () => Promise + + afterEach(async () => { + await shutdownFunc() + }) beforeEach(async () => { await beforeEach_() MedusaModule.clearInstances() - module = await initialize( - { - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }, - { - eventBusModuleService: eventBus, - } - ) + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown }) afterEach(afterEach_) @@ -367,23 +388,24 @@ describe("Product module", function () { describe("restore", function () { let module: IProductModuleService let images = ["image-1"] + let shutdownFunc: () => Promise + + afterEach(async () => { + await shutdownFunc() + }) beforeEach(async () => { await beforeEach_() MedusaModule.clearInstances() - module = await initialize( - { - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }, - { - eventBusModuleService: eventBus, - } - ) + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown }) afterEach(afterEach_) diff --git a/packages/product/integration-tests/__tests__/services/product-module-service/product-categories.spec.ts b/packages/product/integration-tests/__tests__/services/product-module-service/product-categories.spec.ts index 11013c715d..3900d1b6b3 100644 --- a/packages/product/integration-tests/__tests__/services/product-module-service/product-categories.spec.ts +++ b/packages/product/integration-tests/__tests__/services/product-module-service/product-categories.spec.ts @@ -1,37 +1,42 @@ import { IProductModuleService, ProductTypes } from "@medusajs/types" import { Product, ProductCategory } from "@models" import { SqlEntityManager } from "@mikro-orm/postgresql" - -import { initialize } from "../../../../src" -import { DB_URL, TestDatabase } from "../../../utils" +import { getInitModuleConfig, TestDatabase } from "../../../utils" import { createProductCategories } from "../../../__fixtures__/product-category" import { productCategoriesRankData } from "../../../__fixtures__/product-category/data" import { EventBusService } from "../../../__fixtures__/event-bus" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils/dist" describe("ProductModuleService product categories", () => { let service: IProductModuleService let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager let productOne: Product let productTwo: Product let productCategoryOne: ProductCategory let productCategoryTwo: ProductCategory let productCategories: ProductCategory[] - let eventBus + + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await TestDatabase.setupDatabase() - repositoryManager = await TestDatabase.forkManager() - eventBus = new EventBusService() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }, { - eventBusModuleService: eventBus - }) testManager = await TestDatabase.forkManager() @@ -286,19 +291,17 @@ describe("ProductModuleService product categories", () => { }) it("should emit events through event bus", async () => { - const eventBusSpy = jest.spyOn(EventBusService.prototype, 'emit') + const eventBusSpy = jest.spyOn(EventBusService.prototype, "emit") + const category = await service.createCategory({ name: "New Category", parent_category_id: productCategoryOne.id, }) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith( - "product-category.created", - { - id: category.id - } - ) + expect(eventBusSpy).toHaveBeenCalledWith("product-category.created", { + id: category.id, + }) }) it("should append rank from an existing category depending on parent", async () => { @@ -379,18 +382,15 @@ describe("ProductModuleService product categories", () => { }) it("should emit events through event bus", async () => { - const eventBusSpy = jest.spyOn(EventBusService.prototype, 'emit') + const eventBusSpy = jest.spyOn(EventBusService.prototype, "emit") await service.updateCategory(productCategoryZero.id, { name: "New Category", }) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith( - "product-category.updated", - { - id: productCategoryZero.id - } - ) + expect(eventBusSpy).toHaveBeenCalledWith("product-category.updated", { + id: productCategoryZero.id, + }) }) it("should update the name of the category successfully", async () => { @@ -551,16 +551,13 @@ describe("ProductModuleService product categories", () => { }) it("should emit events through event bus", async () => { - const eventBusSpy = jest.spyOn(EventBusService.prototype, 'emit') + const eventBusSpy = jest.spyOn(EventBusService.prototype, "emit") await service.deleteCategory(productCategoryOne.id) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith( - "product-category.deleted", - { - id: productCategoryOne.id - } - ) + expect(eventBusSpy).toHaveBeenCalledWith("product-category.deleted", { + id: productCategoryOne.id, + }) }) it("should throw an error when an id does not exist", async () => { diff --git a/packages/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts b/packages/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts index 856dd05a5e..cfb52913ed 100644 --- a/packages/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts +++ b/packages/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts @@ -1,39 +1,41 @@ import { IProductModuleService, ProductTypes } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Product, ProductCollection } from "@models" - -import { initialize } from "../../../../src" import { EventBusService } from "../../../__fixtures__/event-bus" import { createCollections } from "../../../__fixtures__/product" -import { DB_URL, TestDatabase } from "../../../utils" +import { getInitModuleConfig, TestDatabase } from "../../../utils" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" describe("ProductModuleService product collections", () => { let service: IProductModuleService let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager let productOne: Product let productTwo: Product let productCollectionOne: ProductCollection let productCollectionTwo: ProductCollection let productCollections: ProductCollection[] - let eventBus + + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) beforeEach(async () => { await TestDatabase.setupDatabase() - repositoryManager = await TestDatabase.forkManager() - eventBus = new EventBusService() - - service = await initialize( - { - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }, - { - eventBusModuleService: eventBus, - } - ) testManager = await TestDatabase.forkManager() diff --git a/packages/product/integration-tests/__tests__/services/product-module-service/product-options.spec.ts b/packages/product/integration-tests/__tests__/services/product-module-service/product-options.spec.ts index fb8ffbcf21..1803bf5cf3 100644 --- a/packages/product/integration-tests/__tests__/services/product-module-service/product-options.spec.ts +++ b/packages/product/integration-tests/__tests__/services/product-module-service/product-options.spec.ts @@ -1,28 +1,38 @@ import { IProductModuleService, ProductTypes } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Product, ProductOption } from "@models" -import { initialize } from "../../../../src" -import { DB_URL, TestDatabase } from "../../../utils" +import { getInitModuleConfig, TestDatabase } from "../../../utils" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" describe("ProductModuleService product options", () => { let service: IProductModuleService let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager let optionOne: ProductOption let optionTwo: ProductOption let productOne: Product let productTwo: Product + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) + beforeEach(async () => { await TestDatabase.setupDatabase() - repositoryManager = await TestDatabase.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }) testManager = await TestDatabase.forkManager() productOne = testManager.create(Product, { diff --git a/packages/product/integration-tests/__tests__/services/product-module-service/product-tags.spec.ts b/packages/product/integration-tests/__tests__/services/product-module-service/product-tags.spec.ts index b3340499d4..0aacbee390 100644 --- a/packages/product/integration-tests/__tests__/services/product-module-service/product-tags.spec.ts +++ b/packages/product/integration-tests/__tests__/services/product-module-service/product-tags.spec.ts @@ -1,29 +1,38 @@ -import { initialize } from "../../../../src" -import { DB_URL, TestDatabase } from "../../../utils" -import { IProductModuleService } from "@medusajs/types" +import { getInitModuleConfig, TestDatabase } from "../../../utils" +import { IProductModuleService, ProductTypes } from "@medusajs/types" import { Product, ProductTag } from "@models" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { ProductTypes } from "@medusajs/types" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils/dist" describe("ProductModuleService product tags", () => { let service: IProductModuleService let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager let tagOne: ProductTag let tagTwo: ProductTag let productOne: Product let productTwo: Product + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) + beforeEach(async () => { await TestDatabase.setupDatabase() - repositoryManager = await TestDatabase.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }) testManager = await TestDatabase.forkManager() productOne = testManager.create(Product, { @@ -103,7 +112,7 @@ describe("ProductModuleService product tags", () => { { select: ["value", "products.id"], relations: ["products"], - take: 1 + take: 1, } ) @@ -111,9 +120,11 @@ describe("ProductModuleService product tags", () => { { id: tagOne.id, value: tagOne.value, - products: [{ - id: productOne.id, - }], + products: [ + { + id: productOne.id, + }, + ], }, ]) }) @@ -149,11 +160,9 @@ describe("ProductModuleService product tags", () => { id: tagOne.id, }), ]) - ;[tags, count] = await service.listAndCountTags({}, { take: 1 }) expect(count).toEqual(2) - ;[tags, count] = await service.listAndCountTags({}, { take: 1, skip: 1 }) expect(count).toEqual(2) @@ -172,7 +181,7 @@ describe("ProductModuleService product tags", () => { { select: ["value", "products.id"], relations: ["products"], - take: 1 + take: 1, } ) @@ -181,9 +190,11 @@ describe("ProductModuleService product tags", () => { { id: tagOne.id, value: tagOne.value, - products: [{ - id: productOne.id, - }], + products: [ + { + id: productOne.id, + }, + ], }, ]) }) @@ -196,28 +207,27 @@ describe("ProductModuleService product tags", () => { expect(tag).toEqual( expect.objectContaining({ id: tagOne.id, - }), + }) ) }) it("should return requested attributes when requested through config", async () => { - const tag = await service.retrieveTag( - tagOne.id, - { - select: ["id", "value", "products.title"], - relations: ["products"], - } - ) + const tag = await service.retrieveTag(tagOne.id, { + select: ["id", "value", "products.title"], + relations: ["products"], + }) expect(tag).toEqual( expect.objectContaining({ id: tagOne.id, value: tagOne.value, - products: [{ - id: "product-1", - title: "product 1", - }], - }), + products: [ + { + id: "product-1", + title: "product 1", + }, + ], + }) ) }) @@ -230,7 +240,9 @@ describe("ProductModuleService product tags", () => { error = e } - expect(error.message).toEqual("ProductTag with id: does-not-exist was not found") + expect(error.message).toEqual( + "ProductTag with id: does-not-exist was not found" + ) }) }) @@ -238,12 +250,10 @@ describe("ProductModuleService product tags", () => { const tagId = "tag-1" it("should delete the product tag given an ID successfully", async () => { - await service.deleteTags( - [tagId], - ) + await service.deleteTags([tagId]) const tags = await service.listTags({ - id: tagId + id: tagId, }) expect(tags).toHaveLength(0) @@ -254,12 +264,12 @@ describe("ProductModuleService product tags", () => { const tagId = "tag-1" it("should update the value of the tag successfully", async () => { - await service.updateTags( - [{ + await service.updateTags([ + { id: tagId, - value: "UK" - }] - ) + value: "UK", + }, + ]) const productTag = await service.retrieveTag(tagId) @@ -273,31 +283,32 @@ describe("ProductModuleService product tags", () => { await service.updateTags([ { id: "does-not-exist", - value: "UK" - } + value: "UK", + }, ]) } catch (e) { error = e } - expect(error.message).toEqual('ProductTag with id "does-not-exist" not found') + expect(error.message).toEqual( + 'ProductTag with id "does-not-exist" not found' + ) }) }) describe("createTags", () => { it("should create a tag successfully", async () => { - const res = await service.createTags( - [{ - value: "UK" - }] - ) + const res = await service.createTags([ + { + value: "UK", + }, + ]) const productTag = await service.listTags({ - value: "UK" + value: "UK", }) expect(productTag[0]?.value).toEqual("UK") }) }) }) - diff --git a/packages/product/integration-tests/__tests__/services/product-module-service/product-types.spec.ts b/packages/product/integration-tests/__tests__/services/product-module-service/product-types.spec.ts index 1f08c28f50..70776157a1 100644 --- a/packages/product/integration-tests/__tests__/services/product-module-service/product-types.spec.ts +++ b/packages/product/integration-tests/__tests__/services/product-module-service/product-types.spec.ts @@ -1,27 +1,36 @@ -import { initialize } from "../../../../src" -import { DB_URL, TestDatabase } from "../../../utils" +import { getInitModuleConfig, TestDatabase } from "../../../utils" import { IProductModuleService } from "@medusajs/types" import { ProductType } from "@models" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { ProductTypes } from "@medusajs/types" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils/dist" describe("ProductModuleService product types", () => { let service: IProductModuleService let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager let typeOne: ProductType let typeTwo: ProductType + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) + beforeEach(async () => { await TestDatabase.setupDatabase() - repositoryManager = await TestDatabase.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }) testManager = await TestDatabase.forkManager() @@ -87,7 +96,7 @@ describe("ProductModuleService product types", () => { }, { select: ["value"], - take: 1 + take: 1, } ) @@ -130,12 +139,13 @@ describe("ProductModuleService product types", () => { id: typeOne.id, }), ]) - ;[types, count] = await service.listAndCountTypes({}, { take: 1 }) expect(count).toEqual(2) - - ;[types, count] = await service.listAndCountTypes({}, { take: 1, skip: 1 }) + ;[types, count] = await service.listAndCountTypes( + {}, + { take: 1, skip: 1 } + ) expect(count).toEqual(2) expect(types).toEqual([ @@ -152,7 +162,7 @@ describe("ProductModuleService product types", () => { }, { select: ["value"], - take: 1 + take: 1, } ) @@ -173,24 +183,19 @@ describe("ProductModuleService product types", () => { expect(type).toEqual( expect.objectContaining({ id: typeOne.id, - }), + }) ) }) it("should return requested attributes when requested through config", async () => { - const type = await service.retrieveType( - typeOne.id, - { - select: ["id", "value"], - } - ) + const type = await service.retrieveType(typeOne.id, { + select: ["id", "value"], + }) - expect(type).toEqual( - { - id: typeOne.id, - value: typeOne.value, - }, - ) + expect(type).toEqual({ + id: typeOne.id, + value: typeOne.value, + }) }) it("should throw an error when a type with ID does not exist", async () => { @@ -202,7 +207,9 @@ describe("ProductModuleService product types", () => { error = e } - expect(error.message).toEqual("ProductType with id: does-not-exist was not found") + expect(error.message).toEqual( + "ProductType with id: does-not-exist was not found" + ) }) }) @@ -210,12 +217,10 @@ describe("ProductModuleService product types", () => { const typeId = "type-1" it("should delete the product type given an ID successfully", async () => { - await service.deleteTypes( - [typeId], - ) + await service.deleteTypes([typeId]) const types = await service.listTypes({ - id: typeId + id: typeId, }) expect(types).toHaveLength(0) @@ -226,12 +231,12 @@ describe("ProductModuleService product types", () => { const typeId = "type-1" it("should update the value of the type successfully", async () => { - await service.updateTypes( - [{ + await service.updateTypes([ + { id: typeId, - value: "UK" - }] - ) + value: "UK", + }, + ]) const productType = await service.retrieveType(typeId) @@ -245,31 +250,32 @@ describe("ProductModuleService product types", () => { await service.updateTypes([ { id: "does-not-exist", - value: "UK" - } + value: "UK", + }, ]) } catch (e) { error = e } - expect(error.message).toEqual('ProductType with id "does-not-exist" not found') + expect(error.message).toEqual( + 'ProductType with id "does-not-exist" not found' + ) }) }) describe("createTypes", () => { it("should create a type successfully", async () => { - const res = await service.createTypes( - [{ - value: "UK" - }] - ) + const res = await service.createTypes([ + { + value: "UK", + }, + ]) const productType = await service.listTypes({ - value: "UK" + value: "UK", }) expect(productType[0]?.value).toEqual("UK") }) }) }) - diff --git a/packages/product/integration-tests/__tests__/services/product-module-service/product-variants.spec.ts b/packages/product/integration-tests/__tests__/services/product-module-service/product-variants.spec.ts index 26c5dc43bb..0b78d41a14 100644 --- a/packages/product/integration-tests/__tests__/services/product-module-service/product-variants.spec.ts +++ b/packages/product/integration-tests/__tests__/services/product-module-service/product-variants.spec.ts @@ -1,30 +1,38 @@ -import { initialize } from "../../../../src" -import { DB_URL, TestDatabase } from "../../../utils" -import { IProductModuleService } from "@medusajs/types" +import { getInitModuleConfig, TestDatabase } from "../../../utils" +import { IProductModuleService, ProductTypes } from "@medusajs/types" import { Product, ProductVariant } from "@models" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { ProductTypes } from "@medusajs/types" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils/dist" describe("ProductModuleService product variants", () => { let service: IProductModuleService let testManager: SqlEntityManager - let repositoryManager: SqlEntityManager let variantOne: ProductVariant let variantTwo: ProductVariant let productOne: Product let productTwo: Product + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) + beforeEach(async () => { await TestDatabase.setupDatabase() - repositoryManager = await TestDatabase.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }) - testManager = await TestDatabase.forkManager() productOne = testManager.create(Product, { @@ -140,18 +148,15 @@ describe("ProductModuleService product variants", () => { expect.objectContaining({ id: "test-1", title: "variant 1", - }), + }) ) }) it("should return requested attributes when requested through config", async () => { - const result = await service.retrieveVariant( - variantOne.id, - { - select: ["id", "title", "product.title"] as any, - relations: ["product"], - } - ) + const result = await service.retrieveVariant(variantOne.id, { + select: ["id", "title", "product.title"] as any, + relations: ["product"], + }) expect(result).toEqual( expect.objectContaining({ @@ -162,7 +167,7 @@ describe("ProductModuleService product variants", () => { id: "product-1", title: "product 1", }), - }), + }) ) }) @@ -175,8 +180,9 @@ describe("ProductModuleService product variants", () => { error = e } - expect(error.message).toEqual("ProductVariant with id: does-not-exist was not found") + expect(error.message).toEqual( + "ProductVariant with id: does-not-exist was not found" + ) }) }) }) - diff --git a/packages/product/integration-tests/__tests__/services/product-module-service/products.spec.ts b/packages/product/integration-tests/__tests__/services/product-module-service/products.spec.ts index 62dc805468..98bbc6418a 100644 --- a/packages/product/integration-tests/__tests__/services/product-module-service/products.spec.ts +++ b/packages/product/integration-tests/__tests__/services/product-module-service/products.spec.ts @@ -1,4 +1,4 @@ -import { MedusaModule } from "@medusajs/modules-sdk" +import { MedusaModule, Modules } from "@medusajs/modules-sdk" import { IProductModuleService, ProductTypes, @@ -18,7 +18,8 @@ import { EventBusService } from "../../../__fixtures__/event-bus" import { createCollections, createTypes } from "../../../__fixtures__/product" import { createProductCategories } from "../../../__fixtures__/product-category" import { buildProductAndRelationsData } from "../../../__fixtures__/product/data/create-product" -import { DB_URL, TestDatabase } from "../../../utils" +import { DB_URL, getInitModuleConfig, TestDatabase } from "../../../utils" +import { initModules } from "medusa-test-utils" const beforeEach_ = async () => { await TestDatabase.setupDatabase() @@ -57,7 +58,6 @@ describe("ProductModuleService products", function () { let productTypeOne: ProductType let productTypeTwo: ProductType let images = ["image-1"] - let eventBus const productCategoriesData = [ { @@ -88,6 +88,24 @@ describe("ProductModuleService products", function () { }, ] + let shutdownFunc: () => Promise + + beforeAll(async () => { + MedusaModule.clearInstances() + + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + module = medusaApp.modules[Modules.PRODUCT] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + await shutdownFunc() + }) + beforeEach(async () => { const testManager = await beforeEach_() @@ -149,21 +167,6 @@ describe("ProductModuleService products", function () { }) await testManager.persistAndFlush([productOne, productTwo]) - - MedusaModule.clearInstances() - - eventBus = new EventBusService() - module = await initialize( - { - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, - }, - }, - { - eventBusModuleService: eventBus, - } - ) }) afterEach(afterEach_) diff --git a/packages/product/integration-tests/utils/get-init-module-config.ts b/packages/product/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000..a57806bb17 --- /dev/null +++ b/packages/product/integration-tests/utils/get-init-module-config.ts @@ -0,0 +1,40 @@ +import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" + +import { DB_URL } from "./database" +import { EventBusService } from "../__fixtures__/event-bus" + +export function getInitModuleConfig( + additionalOptions: any = {}, + injectedDependencies: null | Record = null +) { + const moduleOptions = { + defaultAdapterOptions: { + database: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, + }, + }, + ...additionalOptions, + } + + const modulesConfig_ = { + [Modules.PRODUCT]: { + definition: ModulesDefinition[Modules.PRODUCT], + options: moduleOptions, + }, + } + + const defaultInjectedDependencies = { + eventBusModuleService: new EventBusService(), + } + + return { + injectedDependencies: injectedDependencies ?? defaultInjectedDependencies, + modulesConfig: modulesConfig_, + databaseConfig: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PRODUCT_DB_SCHEMA, + }, + joinerConfig: [], + } +} diff --git a/packages/product/integration-tests/utils/index.ts b/packages/product/integration-tests/utils/index.ts index 6b917ed30e..ba28fb5523 100644 --- a/packages/product/integration-tests/utils/index.ts +++ b/packages/product/integration-tests/utils/index.ts @@ -1 +1,2 @@ export * from "./database" +export * from "./get-init-module-config" diff --git a/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts b/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts index 43d1b3b06d..122cde64af 100644 --- a/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts +++ b/packages/promotion/integration-tests/__tests__/services/promotion-module/campaign.spec.ts @@ -1,26 +1,36 @@ import { IPromotionModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { initialize } from "../../../../src" import { createCampaigns } from "../../../__fixtures__/campaigns" import { createPromotions } from "../../../__fixtures__/promotion" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) describe("Promotion Module Service: Campaigns", () => { let service: IPromotionModuleService let repositoryManager: SqlEntityManager + let shutdownFunc: () => void + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PROMOTION] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA, - }, - }) }) afterEach(async () => { diff --git a/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts b/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts index 398f7e00e3..4b97bdd521 100644 --- a/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts +++ b/packages/promotion/integration-tests/__tests__/services/promotion-module/compute-actions.spec.ts @@ -1,26 +1,36 @@ import { IPromotionModuleService } from "@medusajs/types" import { PromotionType } from "@medusajs/utils" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { initialize } from "../../../../src" import { createCampaigns } from "../../../__fixtures__/campaigns" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils" jest.setTimeout(30000) describe("Promotion Service: computeActions", () => { let service: IPromotionModuleService let repositoryManager: SqlEntityManager + let shutdownFunc: () => void + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PROMOTION] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA, - }, - }) }) afterEach(async () => { diff --git a/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts b/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts index f0520ff801..909f31dbc4 100644 --- a/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts +++ b/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts @@ -5,27 +5,37 @@ import { PromotionType, } from "@medusajs/utils" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { initialize } from "../../../../src" import { createCampaigns } from "../../../__fixtures__/campaigns" import { createPromotions } from "../../../__fixtures__/promotion" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { Modules } from "@medusajs/modules-sdk" +import { initModules } from "medusa-test-utils/dist" jest.setTimeout(30000) describe("Promotion Service", () => { let service: IPromotionModuleService let repositoryManager: SqlEntityManager + let shutdownFunc: () => void + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PROMOTION] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA, - }, - }) }) afterEach(async () => { diff --git a/packages/promotion/integration-tests/__tests__/services/promotion-module/register-usage.spec.ts b/packages/promotion/integration-tests/__tests__/services/promotion-module/register-usage.spec.ts index 354b3c5582..3d96d4191b 100644 --- a/packages/promotion/integration-tests/__tests__/services/promotion-module/register-usage.spec.ts +++ b/packages/promotion/integration-tests/__tests__/services/promotion-module/register-usage.spec.ts @@ -1,27 +1,37 @@ import { IPromotionModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { initialize } from "../../../../src" import { createCampaigns } from "../../../__fixtures__/campaigns" -import { DB_URL, MikroOrmWrapper } from "../../../utils" +import { MikroOrmWrapper } from "../../../utils" +import { getInitModuleConfig } from "../../../utils/get-init-module-config" +import { initModules } from "medusa-test-utils/dist" +import { Modules } from "@medusajs/modules-sdk" jest.setTimeout(30000) describe("Promotion Service: campaign usage", () => { let service: IPromotionModuleService let repositoryManager: SqlEntityManager + let shutdownFunc: () => void + + beforeAll(async () => { + const initModulesConfig = getInitModuleConfig() + + const { medusaApp, shutdown } = await initModules(initModulesConfig) + + service = medusaApp.modules[Modules.PROMOTION] + + shutdownFunc = shutdown + }) + + afterAll(async () => { + shutdownFunc() + }) beforeEach(async () => { await MikroOrmWrapper.setupDatabase() repositoryManager = MikroOrmWrapper.forkManager() await createCampaigns(repositoryManager) - - service = await initialize({ - database: { - clientUrl: DB_URL, - schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA, - }, - }) }) afterEach(async () => { diff --git a/packages/promotion/integration-tests/utils/get-init-module-config.ts b/packages/promotion/integration-tests/utils/get-init-module-config.ts new file mode 100644 index 0000000000..b512096f97 --- /dev/null +++ b/packages/promotion/integration-tests/utils/get-init-module-config.ts @@ -0,0 +1,33 @@ +import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" + +import { DB_URL } from "./database" + +export function getInitModuleConfig() { + const moduleOptions = { + defaultAdapterOptions: { + database: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA, + }, + }, + } + + const injectedDependencies = {} + + const modulesConfig_ = { + [Modules.PROMOTION]: { + definition: ModulesDefinition[Modules.PROMOTION], + options: moduleOptions, + }, + } + + return { + injectedDependencies, + modulesConfig: modulesConfig_, + databaseConfig: { + clientUrl: DB_URL, + schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA, + }, + joinerConfig: [], + } +} diff --git a/yarn.lock b/yarn.lock index 115f392aff..29cfcf49b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -37842,6 +37842,8 @@ __metadata: version: 0.0.0-use.local resolution: "medusa-test-utils@workspace:packages/medusa-test-utils" dependencies: + "@medusajs/modules-sdk": "workspace:^" + "@medusajs/utils": "workspace:^" "@mikro-orm/migrations": 5.9.7 "@mikro-orm/postgresql": 5.9.7 cross-env: ^5.2.1