Files
medusa-store/integration-tests/http/__tests__/feature-flag/resources-without-flags.spec.ts
Carlos R. L. Rodrigues e413cfefc2 feat(utils): define file config (#13283)
** What
 - Allow auto-loaded Medusa files to export a config object.
 - Currently supports isDisabled to control loading.
 - new instance `FeatureFlag` exported by `@medusajs/framework/utils`
 - `feature-flags` is now a supported folder for medusa projects, modules, providers and plugins. They will be loaded and added to `FeatureFlag`

** Why
 - Enables conditional loading of routes, migrations, jobs, subscribers, workflows, and other files based on feature flags.

```ts
// /src/feature-flags

import { FlagSettings } from "@medusajs/framework/feature-flags"

const CustomFeatureFlag: FlagSettings = {
  key: "custom_feature",
  default_val: false,
  env_key: "FF_MY_CUSTOM_FEATURE",
  description: "Enable xyz",
}

export default CustomFeatureFlag
```

```ts
// /src/modules/my-custom-module/migration/Migration20250822135845.ts

import { FeatureFlag } from "@medusajs/framework/utils"

export class Migration20250822135845 extends Migration {
  override async up(){ }
  override async down(){ }
}

defineFileConfig({
  isDisabled: () => !FeatureFlag.isFeatureEnabled("custom_feature")
})
```
2025-08-26 12:22:30 +00:00

47 lines
1.7 KiB
TypeScript

import { MedusaWorkflow } from "@medusajs/framework/workflows-sdk"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import path from "path"
import { setTimeout as setTimeoutPromise } from "timers/promises"
import { testJobHandler } from "../../__fixtures__/feature-flag/src/jobs/test-job"
jest.setTimeout(100000)
medusaIntegrationTestRunner({
cwd: path.join(__dirname, "../../__fixtures__/feature-flag"),
testSuite: ({ api, dbConnection }) => {
describe("Resources loaded without feature flags", () => {
it("should not load migration when feature flag is disabled and not run job", async () => {
const migrationNotExecuted = await dbConnection.raw(
`SELECT name FROM "mikro_orm_migrations" WHERE name = 'MigrationTest'`
)
expect(migrationNotExecuted.rows).toHaveLength(0)
const migrationExecuted = await dbConnection.raw(
`SELECT name FROM "mikro_orm_migrations" WHERE name = 'Noop'`
)
expect(migrationExecuted.rows).toHaveLength(1)
expect(migrationExecuted.rows[0].name).toBe("Noop")
await setTimeoutPromise(1000)
expect(testJobHandler).toHaveBeenCalledTimes(0)
})
it("should not load workflow when feature flag is disabled", async () => {
expect(MedusaWorkflow.getWorkflow("test-workflow")).toBeUndefined()
})
it("should not load scheduled job when feature flag is disabled", async () => {
expect(
MedusaWorkflow.getWorkflow("job-greeting-every-second")
).toBeUndefined()
})
it("should not load endpoint when feature flag is disabled", async () => {
expect(api.get("/custom")).rejects.toThrow()
})
})
},
})