Chore/integration tests modules utils (#6581)

new wrapper for medusa integration tests.
for now it is only applied to the modules directory, but it could be used in the api integration tests or any other integrations that requires a db and a server up and running. 

It is not perfect, but I wanted to have something working and centralised before improving it, also avoiding too many conflicts with other prs
This commit is contained in:
Adrien de Peretti
2024-03-06 10:03:07 +00:00
committed by GitHub
parent a6736c7ee0
commit 51bb6f1e89
99 changed files with 10164 additions and 10303 deletions
@@ -6,200 +6,190 @@ import {
IRegionModuleService,
ISalesChannelModuleService,
} from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("Cart links", () => {
let dbConnection
let appContainer
let shutdownServer
let cartModuleService: ICartModuleService
let regionModule: IRegionModuleService
let customerModule: ICustomerModuleService
let scModuleService: ISalesChannelModuleService
let paymentModuleService: IPaymentModuleService
let remoteQuery, remoteLink
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Cart links", () => {
let appContainer
let cartModuleService: ICartModuleService
let regionModule: IRegionModuleService
let customerModule: ICustomerModuleService
let scModuleService: ISalesChannelModuleService
let paymentModuleService: IPaymentModuleService
let remoteQuery, remoteLink
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
customerModule = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
scModuleService = appContainer.resolve(ModuleRegistrationName.SALES_CHANNEL)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
paymentModuleService = appContainer.resolve(ModuleRegistrationName.PAYMENT)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should query carts, sales channels, customers, regions with remote query", async () => {
const region = await regionModule.create({
name: "Region",
currency_code: "usd",
})
const customer = await customerModule.create({
email: "tony@stark.com",
})
const salesChannel = await scModuleService.create({
name: "Webshop",
})
const cart = await cartModuleService.create({
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
sales_channel_id: salesChannel.id,
customer_id: customer.id,
})
const paymentCollection =
await paymentModuleService.createPaymentCollections({
currency_code: "usd",
region_id: region.id,
amount: 1000,
beforeAll(async () => {
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
customerModule = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
scModuleService = appContainer.resolve(
ModuleRegistrationName.SALES_CHANNEL
)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
paymentModuleService = appContainer.resolve(
ModuleRegistrationName.PAYMENT
)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
await remoteLink.create([
{
[Modules.CART]: {
cart_id: cart.id,
},
[Modules.PAYMENT]: {
payment_collection_id: paymentCollection.id,
},
},
])
it("should query carts, sales channels, customers, regions with remote query", async () => {
const region = await regionModule.create({
name: "Region",
currency_code: "usd",
})
const carts = await remoteQuery({
cart: {
fields: ["id"],
region: {
fields: ["id"],
},
customer: {
fields: ["id"],
},
sales_channel: {
fields: ["id"],
},
payment_collection: {
fields: ["id"],
},
},
const customer = await customerModule.create({
email: "tony@stark.com",
})
const salesChannel = await scModuleService.create({
name: "Webshop",
})
const cart = await cartModuleService.create({
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
sales_channel_id: salesChannel.id,
customer_id: customer.id,
})
const paymentCollection =
await paymentModuleService.createPaymentCollections({
currency_code: "usd",
region_id: region.id,
amount: 1000,
})
await remoteLink.create([
{
[Modules.CART]: {
cart_id: cart.id,
},
[Modules.PAYMENT]: {
payment_collection_id: paymentCollection.id,
},
},
])
const carts = await remoteQuery({
cart: {
fields: ["id"],
region: {
fields: ["id"],
},
customer: {
fields: ["id"],
},
sales_channel: {
fields: ["id"],
},
payment_collection: {
fields: ["id"],
},
},
})
const salesChannels = await remoteQuery({
sales_channel: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const customers = await remoteQuery({
customer: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const regions = await remoteQuery({
region: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const paymentCollections = await remoteQuery({
payment: {
fields: ["id"],
cart: {
fields: ["id"],
},
},
})
expect(carts).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: cart.id,
customer: expect.objectContaining({ id: customer.id }),
sales_channel: expect.objectContaining({ id: salesChannel.id }),
region: expect.objectContaining({ id: region.id }),
payment_collection: expect.objectContaining({
id: paymentCollection.id,
}),
}),
])
)
expect(salesChannels).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: salesChannel.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(customers).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: customer.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(regions).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(paymentCollections).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: paymentCollection.id,
cart: expect.objectContaining({ id: cart.id }),
}),
])
)
})
})
const salesChannels = await remoteQuery({
sales_channel: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const customers = await remoteQuery({
customer: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const regions = await remoteQuery({
region: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
const paymentCollections = await remoteQuery({
payment: {
fields: ["id"],
cart: {
fields: ["id"],
},
},
})
expect(carts).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: cart.id,
customer: expect.objectContaining({ id: customer.id }),
sales_channel: expect.objectContaining({ id: salesChannel.id }),
region: expect.objectContaining({ id: region.id }),
payment_collection: expect.objectContaining({
id: paymentCollection.id,
}),
}),
])
)
expect(salesChannels).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: salesChannel.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(customers).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: customer.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(regions).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
expect(paymentCollections).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: paymentCollection.id,
cart: expect.objectContaining({ id: cart.id }),
}),
])
)
})
},
})
@@ -1,91 +1,77 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICartModuleService, IRegionModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("Link: Cart Region", () => {
let dbConnection
let appContainer
let shutdownServer
let cartModuleService: ICartModuleService
let regionModule: IRegionModuleService
let remoteQuery
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Link: Cart Region", () => {
let appContainer
let cartModuleService: ICartModuleService
let regionModule: IRegionModuleService
let remoteQuery
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
remoteQuery = appContainer.resolve("remoteQuery")
})
beforeAll(async () => {
appContainer = getContainer()
cartModuleService = appContainer.resolve(ModuleRegistrationName.CART)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
remoteQuery = appContainer.resolve("remoteQuery")
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should query carts and regions with remote query", async () => {
const region = await regionModule.create({
name: "Region",
currency_code: "usd",
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
const cart = await cartModuleService.create({
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
})
it("should query carts and regions with remote query", async () => {
const region = await regionModule.create({
name: "Region",
currency_code: "usd",
const carts = await remoteQuery({
cart: {
fields: ["id"],
region: {
fields: ["id"],
},
},
})
const regions = await remoteQuery({
region: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
expect(carts).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: cart.id,
region: expect.objectContaining({ id: region.id }),
}),
])
)
expect(regions).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
})
})
const cart = await cartModuleService.create({
email: "tony@stark.com",
currency_code: "usd",
region_id: region.id,
})
const carts = await remoteQuery({
cart: {
fields: ["id"],
region: {
fields: ["id"],
},
},
})
const regions = await remoteQuery({
region: {
fields: ["id"],
carts: {
fields: ["id"],
},
},
})
expect(carts).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: cart.id,
region: expect.objectContaining({ id: region.id }),
}),
])
)
expect(regions).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
carts: expect.arrayContaining([
expect.objectContaining({ id: cart.id }),
]),
}),
])
)
})
},
})
@@ -3,103 +3,91 @@ import {
IApiKeyModuleService,
ISalesChannelModuleService,
} from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("Publishable keys and sales channel link", () => {
let dbConnection
let appContainer
let shutdownServer
let apiKeyModule: IApiKeyModuleService
let scModuleService: ISalesChannelModuleService
let remoteQuery
let remoteLink
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Publishable keys and sales channel link", () => {
let appContainer
let apiKeyModule: IApiKeyModuleService
let scModuleService: ISalesChannelModuleService
let remoteQuery
let remoteLink
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
apiKeyModule = appContainer.resolve(ModuleRegistrationName.API_KEY)
scModuleService = appContainer.resolve(ModuleRegistrationName.SALES_CHANNEL)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
beforeAll(async () => {
appContainer = getContainer()
apiKeyModule = appContainer.resolve(ModuleRegistrationName.API_KEY)
scModuleService = appContainer.resolve(
ModuleRegistrationName.SALES_CHANNEL
)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should query api key and sales channels link with remote query", async () => {
const salesChannel = await scModuleService.create({
name: "Webshop",
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
const apiKeys = await apiKeyModule.create([
{
title: "Api key",
type: "publishable",
created_by: "test",
},
{
title: "Api key 2",
type: "publishable",
created_by: "test",
},
])
it("should query api key and sales channels link with remote query", async () => {
const salesChannel = await scModuleService.create({
name: "Webshop",
await remoteLink.create([
{
[Modules.API_KEY]: {
publishable_key_id: apiKeys[0].id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
},
{
[Modules.API_KEY]: {
publishable_key_id: apiKeys[1].id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
},
])
const queryObject = remoteQueryObjectFromString({
entryPoint: "api_key",
variables: {
filters: { token: apiKeys[0].token },
},
fields: ["id", "sales_channels.id"],
})
const keyLinks = await remoteQuery(queryObject)
expect(keyLinks).toHaveLength(1)
expect(keyLinks).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: apiKeys[0].id,
sales_channels: expect.arrayContaining([
expect.objectContaining({ id: salesChannel.id }),
]),
}),
])
)
})
})
const apiKeys = await apiKeyModule.create([
{
title: "Api key",
type: "publishable",
created_by: "test",
},
{
title: "Api key 2",
type: "publishable",
created_by: "test",
},
])
await remoteLink.create([
{
[Modules.API_KEY]: {
publishable_key_id: apiKeys[0].id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
},
{
[Modules.API_KEY]: {
publishable_key_id: apiKeys[1].id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
},
])
const queryObject = remoteQueryObjectFromString({
entryPoint: "api_key",
variables: {
filters: { token: apiKeys[0].token },
},
fields: ["id", "sales_channels.id"],
})
const keyLinks = await remoteQuery(queryObject)
expect(keyLinks).toHaveLength(1)
expect(keyLinks).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: apiKeys[0].id,
sales_channels: expect.arrayContaining([
expect.objectContaining({ id: salesChannel.id }),
]),
}),
])
)
})
},
})
@@ -1,106 +1,92 @@
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import { IPaymentModuleService, IRegionModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
describe("Region and Payment Providers", () => {
let dbConnection
let appContainer
let shutdownServer
let regionModule: IRegionModuleService
let paymentModule: IPaymentModuleService
let remoteQuery
let remoteLink
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Region and Payment Providers", () => {
let appContainer
let regionModule: IRegionModuleService
let paymentModule: IPaymentModuleService
let remoteQuery
let remoteLink
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
paymentModule = appContainer.resolve(ModuleRegistrationName.PAYMENT)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
beforeAll(async () => {
appContainer = getContainer()
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
paymentModule = appContainer.resolve(ModuleRegistrationName.PAYMENT)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should query region and payment provider link with remote query", async () => {
const region = await regionModule.create({
name: "North America",
currency_code: "usd",
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
await remoteLink.create([
{
[Modules.REGION]: {
region_id: region.id,
},
[Modules.PAYMENT]: {
payment_provider_id: "pp_system_default",
},
},
])
it("should query region and payment provider link with remote query", async () => {
const region = await regionModule.create({
name: "North America",
currency_code: "usd",
})
const links = await remoteQuery({
region: {
fields: ["id"],
payment_providers: {
fields: ["id"],
},
},
})
await remoteLink.create([
{
[Modules.REGION]: {
region_id: region.id,
},
[Modules.PAYMENT]: {
payment_provider_id: "pp_system_default",
},
},
])
const otherLink = await remoteQuery({
payment_providers: {
fields: ["id"],
regions: {
fields: ["id"],
},
},
})
const links = await remoteQuery({
region: {
fields: ["id"],
payment_providers: {
fields: ["id"],
},
},
})
const otherLink = await remoteQuery({
payment_providers: {
fields: ["id"],
regions: {
fields: ["id"],
},
},
})
expect(links).toHaveLength(1)
expect(links).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
payment_providers: expect.arrayContaining([
expect.objectContaining({
id: "pp_system_default",
}),
]),
}),
])
)
expect(otherLink).toHaveLength(1)
expect(otherLink).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "pp_system_default",
regions: expect.arrayContaining([
expect(links).toHaveLength(1)
expect(links).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: region.id,
payment_providers: expect.arrayContaining([
expect.objectContaining({
id: "pp_system_default",
}),
]),
}),
]),
}),
])
)
})
])
)
expect(otherLink).toHaveLength(1)
expect(otherLink).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "pp_system_default",
regions: expect.arrayContaining([
expect.objectContaining({
id: region.id,
}),
]),
}),
])
)
})
})
},
})