Feat(medusa-test-utils, authentication, customer, cart, payment, pricing, product, promotion): Add initModule util (#6200)

* use initModules instead of initialize when runnning auth module integration tests

* rm unused module-config

* correct db schema fix

* update authentication integration tests w/ initModule

* update cart integration tests w/ initModule

* update customer integration tests w/ initModule

* update payment integration tests w/ initModule

* update pricing integration tests w/ initModule

* update product integration tests w/ initModule

* update promotion integration tests w/ initModule

* add initModule to more product tests and return medusaApp from initModule

* align moduleOptions naming

* update dependencies
This commit is contained in:
Philip Korsholm
2024-01-25 10:37:38 +08:00
committed by GitHub
parent 134af77667
commit efd9204e26
40 changed files with 1042 additions and 486 deletions
@@ -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<any, any>({
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<void>
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<void>
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<void>
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<void>
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<void>
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<void>
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<void>
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_)
@@ -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<void>
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 () => {
@@ -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<void>
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()
@@ -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<void>
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, {
@@ -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<void>
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")
})
})
})
@@ -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<void>
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")
})
})
})
@@ -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<void>
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"
)
})
})
})
@@ -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<void>
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_)
@@ -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<string, any> = 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: [],
}
}
@@ -1 +1,2 @@
export * from "./database"
export * from "./get-init-module-config"