Repository: medusajs/medusa
File: packages/medusa-test-utils/src/medusa-test-runner.ts (TypeScript)

Code before:

    await this.afterEach()

    const container = this.globalContainer as MedusaContainer
    const copiedContainer = createMedusaContainer({}, container)

    try {
      const { MedusaAppLoader } = await import("@medusajs/framework")
      const medusaAppLoader = new MedusaAppLoader({
        container: copiedContainer,
        medusaConfigPath: this.modulesConfigPath,
        cwd: this.cwd,
      })
      await medusaAppLoader.runModulesLoader()
    } catch (error) {
      await copiedContainer.dispose?.()
      logger.error("Error running modules loaders:", error?.message)
      throw error
    }
  }

  public async afterEach(): Promise<void> {
    try {
      await waitWorkflowExecutions(this.globalContainer as MedusaContainer)

      if (!this?.disableAutoTeardown) {
        // Perform automatic teardown
        await this.dbUtils.teardown({ schema: this.schema })
      }
    } catch (error) {
      logger.error("Error tearing down database:", error?.message)
      throw error
    }
  }

  public getOptions(): MedusaSuiteOptions {
    return {
      api: this.createApiProxy(),
      dbConnection: this.createDbConnectionProxy(),
      getMedusaApp: () => this.loadedApplication,
      getContainer: () => this.globalContainer as MedusaContainer,
      dbConfig: {
        dbName: this.dbName,
        schema: this.schema,
        clientUrl: this.dbConfig.clientUrl,
      },
      dbUtils: this.dbUtils,
      utils: {
        waitWorkflowExecutions: () =>
          waitWorkflowExecutions(this.globalContainer as MedusaContainer),
      },

Review comment (suggestion):
Is there really a context where `this` is undefined ? Why adding the `?`

Code after:

    await this.afterEach()

    const container = this.globalContainer as MedusaContainer
    const copiedContainer = createMedusaContainer({}, container)

    try {
      const { MedusaAppLoader } = await import("@medusajs/framework")
      const medusaAppLoader = new MedusaAppLoader({
        container: copiedContainer,
        medusaConfigPath: this.modulesConfigPath,
        cwd: this.cwd,
      })
      await medusaAppLoader.runModulesLoader()
    } catch (error) {
      await copiedContainer.dispose?.()
      logger.error("Error running modules loaders:", error?.message)
      throw error
    }
  }

  public async afterEach(): Promise<void> {
    try {
      await waitWorkflowExecutions(this.globalContainer as MedusaContainer)

      if (!this.disableAutoTeardown) {
        // Perform automatic teardown
        await this.dbUtils.teardown({ schema: this.schema })
      }
    } catch (error) {
      logger.error("Error tearing down database:", error?.message)
      throw error
    }
  }

  public getOptions(): MedusaSuiteOptions {
    return {
      api: this.createApiProxy(),
      dbConnection: this.createDbConnectionProxy(),
      getMedusaApp: () => this.loadedApplication,
      getContainer: () => this.globalContainer as MedusaContainer,
      dbConfig: {
        dbName: this.dbName,
        schema: this.schema,
        clientUrl: this.dbConfig.clientUrl,
      },
      dbUtils: this.dbUtils,
      utils: {
        waitWorkflowExecutions: () =>
          waitWorkflowExecutions(this.globalContainer as MedusaContainer),
      },