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 authored and GitHub committed 2024-03-06 10:03:07 +00:00
1 parent a6736c7ee0
commit 51bb6f1e89
99 files changed
+10164 -10303

No files matched your search

@@ -1,12 +1,5 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICurrencyModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { DataSource } from "typeorm"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -15,53 +8,39 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("Currency - Admin", () => {
let dbConnection: DataSource
let appContainer
let shutdownServer
let service: ICurrencyModuleService
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, api, getContainer }) => {
describe("Currency - Admin", () => {
let container
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
service = appContainer.resolve(ModuleRegistrationName.CURRENCY)
})
beforeEach(async () => {
container = getContainer()
await createAdminUser(dbConnection, adminHeaders, container)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should correctly retrieve and list currencies", async () => {
const listResp = await api.get("/admin/currencies", adminHeaders)
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
expect(listResp.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "aud",
}),
expect.objectContaining({
code: "cad",
}),
])
)
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should correctly retrieve and list currencies", async () => {
const api = useApi() as any
const listResp = await api.get("/admin/currencies", adminHeaders)
expect(listResp.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "aud",
}),
expect.objectContaining({
code: "cad",
}),
])
)
const retrieveResp = await api.get(`/admin/currencies/aud`, adminHeaders)
expect(retrieveResp.data.currency).toEqual(
listResp.data.currencies.find((c) => c.code === "aud")
)
})
const retrieveResp = await api.get(
`/admin/currencies/aud`,
adminHeaders
)
expect(retrieveResp.data.currency).toEqual(
listResp.data.currencies.find((c) => c.code === "aud")
)
})
})
},
})
@@ -1,11 +1,4 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICurrencyModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { DataSource } from "typeorm"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -14,49 +7,32 @@ const storeHeaders = {
headers: {},
}
describe("Currency - Store", () => {
let dbConnection: DataSource
let appContainer
let shutdownServer
let service: ICurrencyModuleService
medusaIntegrationTestRunner({
env,
testSuite: ({ api }) => {
describe("Currency - Store", () => {
it("should correctly retrieve and list currencies", async () => {
const listResp = await api.get("/store/currencies", storeHeaders)
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
service = appContainer.resolve(ModuleRegistrationName.CURRENCY)
})
expect(listResp.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "aud",
}),
expect.objectContaining({
code: "cad",
}),
])
)
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should correctly retrieve and list currencies", async () => {
const api = useApi() as any
const listResp = await api.get("/store/currencies", storeHeaders)
expect(listResp.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "aud",
}),
expect.objectContaining({
code: "cad",
}),
])
)
const retrieveResp = await api.get(`/store/currencies/aud`, storeHeaders)
expect(retrieveResp.data.currency).toEqual(
listResp.data.currencies.find((c) => c.code === "aud")
)
})
const retrieveResp = await api.get(
`/store/currencies/aud`,
storeHeaders
)
expect(retrieveResp.data.currency).toEqual(
listResp.data.currencies.find((c) => c.code === "aud")
)
})
})
},
})