chore(tests): Try to use the api integration tests for v2 (#6588)

Few things to keep in mind:
1. You need to set MEDUSA_FF_MEDUSA_V2 to true before running the tests to run with the v2 API
2. You can use the `breaking` function to differentiate between v1 and v2 differences. This can help us identify what was breaking pretty quickly afterwards
3. You will need to run specific tests for now instead of all if you want to target v2. I think that's fine though, as we don't really need these to run on every PR until we have feature parity (and by then, all tests would be both v1 and v2 compatible)


**note: Adrien** 
- add a new way to load modules only to run their loaders comparable to the way to run the migrations only
- improve tests runner to cleanup the data properly as well as re running all loaders and core defaults

Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Stevche RadevskiandAdrien de Peretti authored and GitHub committed 2024-03-07 08:05:43 +00:00
1 parent 000eb61e33
commit 12b035cb18
76 files changed
+773 -371

No files matched your search

@@ -3,12 +3,13 @@ import { initDb } from "./medusa-test-runner-utils/use-db"
import { startBootstrapApp } from "./medusa-test-runner-utils/bootstrap-app"
import { createDatabase, dropDatabase } from "pg-god"
import { ContainerLike } from "@medusajs/types"
import { createMedusaContainer } from "@medusajs/utils"
const axios = require("axios").default
const keepTables = [
"store",
"staged_job",
/*"store",*/
/* "staged_job",
"shipping_profile",
"fulfillment_provider",
"payment_provider",
@@ -16,7 +17,7 @@ const keepTables = [
"region_country",
"currency",
"migrations",
"mikro_orm_migrations",
"mikro_orm_migrations",*/
]
const DB_HOST = process.env.DB_HOST
@@ -45,26 +46,19 @@ const dbTestUtilFactory = (): any => ({
forceDelete,
schema,
}: { forceDelete?: string[]; schema?: string } = {}) {
forceDelete = forceDelete || []
forceDelete ??= []
const manager = this.db_.manager
schema ??= "public"
await manager.query(`SET session_replication_role = 'replica';`)
const tableNames = await manager.query(`SELECT table_name
FROM information_schema.tables
WHERE table_schema = '${
schema ?? "public"
}';`)
WHERE table_schema = '${schema}';`)
for (const { table_name } of tableNames) {
if (
keepTables.includes(table_name) &&
!forceDelete.includes(table_name)
) {
continue
}
await manager.query(`DELETE
FROM "${table_name}";`)
FROM ${schema}."${table_name}";`)
}
await manager.query(`SET session_replication_role = 'origin';`)
@@ -209,9 +203,29 @@ export function medusaIntegrationTestRunner({
shutdown = shutdown_
}
const beforeEach_ = async () => {
const container = options.getContainer()
const copiedContainer = createMedusaContainer({}, container)
if (process.env.MEDUSA_FF_MEDUSA_V2 != "true") {
const defaultLoader =
require("@medusajs/medusa/dist/loaders/defaults").default
await defaultLoader({
container: copiedContainer,
})
}
const medusaAppLoaderRunner =
require("@medusajs/medusa/dist/loaders/medusa-app").runModulesLoader
await medusaAppLoaderRunner({
container: copiedContainer,
configModule: container.resolve("configModule"),
})
}
const afterEach_ = async () => {
try {
await dbUtils.teardown({ forceDelete: [], schema })
await dbUtils.teardown({ schema })
} catch (error) {
console.error("Error tearing down database:", error)
}
@@ -219,6 +233,7 @@ export function medusaIntegrationTestRunner({
return describe("", () => {
beforeAll(beforeAll_)
beforeEach(beforeEach_)
afterEach(afterEach_)
afterAll(async () => {
await dbUtils.shutdown(dbName)