Skip to content

Commit

Permalink
chore(medusa-test-utils): Add custom link loading before syncing (#9249)
Browse files Browse the repository at this point in the history
**What**
Include custom links to be loaded from the medusa integration test runner
  • Loading branch information
adrien2p authored Sep 23, 2024
1 parent eb7899d commit e1fdc54
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { MedusaAppLoader } from "@medusajs/framework"
import { join } from "path"
import { MedusaContainer } from "@medusajs/types"
import { ContainerRegistrationKeys } from "@medusajs/utils"

/**
* Initiates the database connection
Expand Down Expand Up @@ -29,8 +32,14 @@ export async function migrateDatabase(appLoader: MedusaAppLoader) {
/**
* Syncs links with the databse
*/
export async function syncLinks(appLoader: MedusaAppLoader) {
export async function syncLinks(
appLoader: MedusaAppLoader,
directory: string,
container: MedusaContainer
) {
try {
await loadCustomLinks(directory, container)

const planner = await appLoader.getLinksExecutionPlanner()
const actionPlan = await planner.createPlan()
actionPlan.forEach((action) => {
Expand All @@ -42,3 +51,22 @@ export async function syncLinks(appLoader: MedusaAppLoader) {
throw err
}
}

async function loadCustomLinks(directory: string, container: MedusaContainer) {
// TODO: move to framework once settle down
const { getResolvedPlugins } = await import(
// @ts-expect-error
"@medusajs/medusa/dist/loaders/helpers/resolve-plugins"
)

const configModule = container.resolve(
ContainerRegistrationKeys.CONFIG_MODULE
)
const plugins = getResolvedPlugins(directory, configModule, true) || []
const linksSourcePaths = plugins.map((plugin) =>
join(plugin.resolve, "links")
)

const { LinkLoader } = await import("@medusajs/framework")
await new LinkLoader(linksSourcePaths).load()
}
8 changes: 4 additions & 4 deletions packages/core/medusa-test-utils/src/medusa-test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
import { applyEnvVarsToProcess } from "./medusa-test-runner-utils/utils"

const DB_HOST = process.env.DB_HOST
const DB_USERNAME = process.env.DB_USERNAME ?? ''
const DB_PASSWORD = process.env.DB_PASSWORD ?? ''
const DB_USERNAME = process.env.DB_USERNAME ?? ""
const DB_PASSWORD = process.env.DB_PASSWORD ?? ""

const pgGodCredentials = {
user: DB_USERNAME,
Expand Down Expand Up @@ -179,7 +179,7 @@ export function medusaIntegrationTestRunner({

console.log(`Migrating database with core migrations and links ${dbName}`)
await migrateDatabase(appLoader)
await syncLinks(appLoader)
await syncLinks(appLoader, cwd, container)
await clearInstances()

let containerRes: MedusaContainer = container
Expand Down Expand Up @@ -215,7 +215,7 @@ export function medusaIntegrationTestRunner({
if (inApp) {
console.log(`Migrating database with core migrations and links ${dbName}`)
await migrateDatabase(appLoader)
await syncLinks(appLoader)
await syncLinks(appLoader, cwd, containerRes)
}

const axios = (await import("axios")).default.default
Expand Down

0 comments on commit e1fdc54

Please sign in to comment.