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 Radevski
2024-03-07 08:05:43 +00:00
committed by GitHub
co-authored by Adrien de Peretti
parent 000eb61e33
commit 12b035cb18
76 changed files with 773 additions and 371 deletions
@@ -14,18 +14,21 @@ export const moduleLoader = async ({
moduleResolutions,
logger,
migrationOnly,
loaderOnly,
}: {
container: MedusaContainer
moduleResolutions: Record<string, ModuleResolution>
logger: Logger
migrationOnly?: boolean
loaderOnly?: boolean
}): Promise<void> => {
for (const resolution of Object.values(moduleResolutions ?? {})) {
const registrationResult = await loadModule(
container,
resolution,
logger!,
migrationOnly
migrationOnly,
loaderOnly
)
if (registrationResult?.error) {
@@ -48,7 +51,8 @@ async function loadModule(
container: MedusaContainer,
resolution: ModuleResolution,
logger: Logger,
migrationOnly?: boolean
migrationOnly?: boolean,
loaderOnly?: boolean
): Promise<{ error?: Error } | void> {
const modDefinition = resolution.definition
const registrationName = modDefinition.registrationName
@@ -85,5 +89,11 @@ async function loadModule(
return
}
return await loadInternalModule(container, resolution, logger, migrationOnly)
return await loadInternalModule(
container,
resolution,
logger,
migrationOnly,
loaderOnly
)
}
@@ -17,7 +17,8 @@ export async function loadInternalModule(
container: MedusaContainer,
resolution: ModuleResolution,
logger: Logger,
migrationOnly?: boolean
migrationOnly?: boolean,
loaderOnly?: boolean
): Promise<{ error?: Error } | void> {
const registrationName = resolution.definition.registrationName
@@ -121,6 +122,10 @@ export async function loadInternalModule(
}
}
if (loaderOnly) {
return
}
const moduleService = loadedModule.service
container.register({
[registrationName]: asFunction((cradle) => {
+32 -3
View File
@@ -67,10 +67,11 @@ export type SharedResources = {
}
}
async function loadModules(
export async function loadModules(
modulesConfig,
sharedContainer,
migrationOnly = false
migrationOnly = false,
loaderOnly = false
) {
const allModules = {}
@@ -111,8 +112,13 @@ async function loadModules(
moduleDefinition: definition as ModuleDefinition,
moduleExports,
migrationOnly,
loaderOnly,
})) as LoadedModule
if (loaderOnly) {
return
}
const service = loaded[moduleName]
sharedContainer.register({
[service.__definition.registrationName]: asValue(service),
@@ -207,6 +213,10 @@ export type MedusaAppOptions = {
remoteFetchData?: RemoteFetchDataCallback
injectedDependencies?: any
onApplicationStartCb?: () => void
/**
* Forces the modules bootstrapper to only run the modules loaders and return prematurely
*/
loaderOnly?: boolean
}
async function MedusaApp_({
@@ -221,6 +231,7 @@ async function MedusaApp_({
injectedDependencies = {},
onApplicationStartCb,
migrationOnly = false,
loaderOnly = false,
}: MedusaAppOptions & { migrationOnly?: boolean } = {}): Promise<{
modules: Record<string, LoadedModule | LoadedModule[]>
link: RemoteLink | undefined
@@ -285,7 +296,25 @@ async function MedusaApp_({
})
}
const allModules = await loadModules(modules, sharedContainer_, migrationOnly)
const allModules = await loadModules(
modules,
sharedContainer_,
migrationOnly,
loaderOnly
)
if (loaderOnly) {
return {
modules: allModules,
link: undefined,
query: async () => {
throw new Error("Querying not allowed in loaderOnly mode")
},
runMigrations: async () => {
throw new Error("Migrations not allowed in loaderOnly mode")
},
}
}
// Share Event bus with link modules
injectedDependencies[ModuleRegistrationName.EVENT_BUS] =
+22 -9
View File
@@ -65,6 +65,10 @@ export type ModuleBootstrapOptions = {
* Don't forget to clear the instances (MedusaModule.clearInstances()) after the migration are done.
*/
migrationOnly?: boolean
/**
* Forces the modules bootstrapper to only run the modules loaders and return prematurely
*/
loaderOnly?: boolean
}
export type LinkModuleBootstrapOptions = {
@@ -220,6 +224,7 @@ export class MedusaModule {
moduleDefinition,
injectedDependencies,
migrationOnly,
loaderOnly,
}: ModuleBootstrapOptions): Promise<{
[key: string]: T
}> {
@@ -227,25 +232,28 @@ export class MedusaModule {
stringifyCircular({ moduleKey, defaultPath, declaration })
)
if (MedusaModule.instances_.has(hashKey)) {
if (!loaderOnly && MedusaModule.instances_.has(hashKey)) {
return MedusaModule.instances_.get(hashKey)! as {
[key: string]: T
}
}
if (MedusaModule.loading_.has(hashKey)) {
if (!loaderOnly && MedusaModule.loading_.has(hashKey)) {
return MedusaModule.loading_.get(hashKey)
}
let finishLoading: any
let errorLoading: any
MedusaModule.loading_.set(
hashKey,
new Promise((resolve, reject) => {
finishLoading = resolve
errorLoading = reject
})
)
if (!loaderOnly) {
MedusaModule.loading_.set(
hashKey,
new Promise((resolve, reject) => {
finishLoading = resolve
errorLoading = reject
})
)
}
let modDeclaration =
declaration ??
@@ -294,6 +302,7 @@ export class MedusaModule {
moduleResolutions,
logger: logger_,
migrationOnly,
loaderOnly
})
} catch (err) {
errorLoading(err)
@@ -302,6 +311,10 @@ export class MedusaModule {
const services = {}
if (loaderOnly) {
return services
}
for (const resolution of Object.values(
moduleResolutions
) as ModuleResolution[]) {