diff --git a/docs/guidance/vitest.md b/docs/guidance/vitest.md index ba0aba8b7dc..45c1a7a5824 100644 --- a/docs/guidance/vitest.md +++ b/docs/guidance/vitest.md @@ -24,7 +24,8 @@ ESLint rule enforces the Testing Library query rule. Do not disable it. - Use `vi.hoisted()` only for bindings needed by a hoisted mock factory. Keep mutable scenario state inside the test that uses it. - Vitest automatically resets mocks, restores spies, and unstubs globals and - environment variables before each test. Do not repeat that cleanup in test + environment variables before each test, and shared mocks restore their own + reactive state with `onTestFinished`. Do not repeat that cleanup in test lifecycle hooks. - Install `vi.stubGlobal()` and `vi.spyOn()` calls in `beforeEach` or in the test that needs them. Module-scope stubs and spies are removed before the @@ -40,31 +41,106 @@ ESLint rule enforces the Testing Library query rule. Do not disable it. state, or duplicate spies. - Type each complete default from the real function's return type. Preserve async and cancellation behavior. Pass the default implementation to - `vi.fn` instead of setting it in `beforeEach`. -- For composables, return fresh state from each call. If repeated calls must - share a result, create it in the test and pin it with - `vi.mocked(useX).mockReturnValue(result)`. + `vi.fn` instead of setting it in `beforeEach`; `mockReset` + restores that implementation before every test. +- A composable mock returns one stable object, so a test can configure it + and then create the consumer that reads it. Never replace a field that + holds a `ref`, `computed`, or object: a consumer that already destructured + it keeps the old one. Because that object is shared by every test in the + file, tests that configure it must not run with `test.concurrent`. +- Until an older mock that builds a fresh object per call (`useBillingContext`) + is converted, pin it in the test with + `vi.mocked(useX).mockReturnValue(useX())` before configuring the result. - Do not use `mock.results` or other call history as a cache. Keep shared result identity explicit in the test that needs it. -- Assign writable fields directly. To override a readonly field on a - configurable mock object, use `vi.spyOn(flags, 'flagName', 'get')`. Vitest - also supports plain data properties; it installs a temporary getter and - restores the original property with the spy. Use `mockReturnValue` for a - fixed value or `mockImplementation` to read changing test state. - Override only the field the test needs. Do not rebuild a full result with nested spreads or add hooks that repeat the shared defaults. -For example, configure a live flag inside the test before creating its consumer: +#### Mock state that `mockReset` does not touch + +`mockReset` restores `vi.fn` implementations and call history, not plain +fields, `reactive()` objects, or `ref` values. A test that writes +`useFeatureFlags().flags.assetsEnabled = true` leaks it into every later test +unless the mock restores it. Register the restore with `onTestFinished` inside +the mock function, so every test that touches the mock cleans up after itself: ```ts -const enabled = ref(false) -const featureFlags = useFeatureFlags() -vi.mocked(useFeatureFlags).mockReturnValue(featureFlags) -vi.spyOn(featureFlags.flags, 'billingControlEnabled', 'get').mockImplementation( - () => enabled.value -) +import { onTestFinished, vi } from 'vitest' + +type FeatureFlags = ReturnType['flags'] + +const defaultFlags: FeatureFlags = { + assetsEnabled: false, + hostedBillingDestination: 'stripe' +} + +const featureFlags: ReturnType = { + flags: reactive({ ...defaultFlags }), + featureFlag: vi.fn((_, defaultValue) => computed(() => defaultValue)) +} + +export const useFeatureFlags = vi.fn(() => { + onTestFinished(() => { + Object.assign(featureFlags.flags, defaultFlags) + }) + return featureFlags +}) ``` +Restore in place (`Object.assign(reactiveObject, defaults)`, +`someRef.value = default`) so consumer `computed`s are notified. Do not call +`beforeEach` inside a mock module: it binds to the file being collected, so a +cached module (`isolate: false`) registers no hook for later files, and a +re-import after `vi.resetModules()` registers a duplicate. + +`onTestFinished` throws `Hook onTestFinished() can only be called inside a +test` when the mock is called at `describe` scope or in `beforeAll`. That is +intended: configure mock state in `beforeEach` or in the test, where the +cleanup can be attached to the test that dirtied it. + +#### Reactive fields: match the real contract + +- Writable (`flags.x`, `enableAppBuilder`): back it with `reactive()` or + `ref` and assign it in the test. When the contract types it `readonly`, + write through `vi.mocked(useFeatureFlags().flags).x = true`. +- Derived with a real mutator (`mode` set by `setMode`): derive every + `computed` from one private source and implement the mutator against it, so + related fields cannot disagree. Tests call the mutator; the mock restores + the source: + + ```ts + const mode = ref('graph') + const appMode: ReturnType = { + mode: computed(() => mode.value), + isArrangeMode: computed(() => mode.value === 'builder:arrange'), + setMode: vi.fn((next) => { + mode.value = next + }) + } + export const useAppMode = vi.fn(() => { + onTestFinished(() => { + mode.value = 'graph' + }) + return appMode + }) + ``` + +- Derived with no mutator (`isLoggedIn`, `canTopUp`): spy on the getter in + the test; `restoreMocks` removes it. Use `mockReturnValue` for a fixed + value. When the value changes mid-test, read a `ref` inside + `mockImplementation`, because Vue does not track a spy's fixed return and + a consumer `computed` caches the first read: + + ```ts + const loggedIn = ref(false) + vi.spyOn(useCurrentUser().isLoggedIn, 'value', 'get').mockImplementation( + () => loggedIn.value + ) + ``` + +Never assign `useCurrentUser().isLoggedIn = computed(() => true)`: it leaks, +and a consumer created earlier keeps the old computed. + ## No Real Network `vitest.setup.ts` blocks every `http(s)` `fetch`, and happy-dom is configured not diff --git a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts index c6821f1e2e2..3c7a08c776e 100644 --- a/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts +++ b/src/components/sidebar/tabs/ModelLibrarySidebarTab.test.ts @@ -163,7 +163,6 @@ const i18n = createI18n({ describe('ModelLibrarySidebarTab', () => { beforeEach(() => { - vi.mocked(useFeatureFlags().flags).assetsEnabled = false resetRoot() useAssetDownloadStore().lastCompletedDownload = null useSettingStore().settingValues['Comfy.ModelLibrary.AutoLoadAll'] = false diff --git a/src/composables/__mocks__/useFeatureFlags.ts b/src/composables/__mocks__/useFeatureFlags.ts index 629bf29c34b..d81f52cc587 100644 --- a/src/composables/__mocks__/useFeatureFlags.ts +++ b/src/composables/__mocks__/useFeatureFlags.ts @@ -1,47 +1,56 @@ -import { vi } from 'vitest' +import { onTestFinished, vi } from 'vitest' import { computed, reactive, watchEffect } from 'vue' import type { useFeatureFlags as realUseFeatureFlags } from '../useFeatureFlags' export const startFeatureFlagTelemetry = vi.fn(() => watchEffect(() => {})) +type FeatureFlags = ReturnType['flags'] + +const defaultFlags: FeatureFlags = { + supportsPreviewMetadata: false, + maxUploadSize: 0, + supportsManagerV4: false, + modelUploadButtonEnabled: false, + assetDeletionEnabled: false, + assetRenameEnabled: false, + privateModelsEnabled: false, + onboardingSurveyEnabled: false, + linearToggleEnabled: false, + partnerNodeGovernanceEnabled: false, + partnerRunGateEnabled: false, + userSecretsEnabled: false, + nodeReplacementsEnabled: false, + nodeLibraryEssentialsEnabled: false, + workflowSharingEnabled: false, + comfyHubUploadEnabled: false, + comfyHubProfileGateEnabled: false, + hostedBillingDestination: 'stripe', + hostedBillingWebEnabled: false, + showSignInButton: undefined, + unifiedCloudAuthEnabled: false, + billingControlEnabled: false, + legacyBillingMigrationEnabled: false, + embeddedCheckoutEnabled: false, + billingSdkTopupEnabled: false, + billingSdkTopupRailEnabled: false, + v1PaymentRecovery: false, + freeTierJobAllowanceEnabled: false, + churnkeyAppId: '', + signupTurnstileMode: 'off', + supportsModelTypeTags: false, + onboardingTourEnabled: false, + assetsEnabled: false +} + const featureFlags: ReturnType = { - flags: reactive({ - supportsPreviewMetadata: false, - maxUploadSize: 0, - supportsManagerV4: false, - modelUploadButtonEnabled: false, - assetDeletionEnabled: false, - assetRenameEnabled: false, - privateModelsEnabled: false, - onboardingSurveyEnabled: false, - linearToggleEnabled: false, - partnerNodeGovernanceEnabled: false, - partnerRunGateEnabled: false, - userSecretsEnabled: false, - nodeReplacementsEnabled: false, - nodeLibraryEssentialsEnabled: false, - workflowSharingEnabled: false, - comfyHubUploadEnabled: false, - comfyHubProfileGateEnabled: false, - hostedBillingDestination: 'stripe', - hostedBillingWebEnabled: false, - showSignInButton: undefined, - unifiedCloudAuthEnabled: false, - billingControlEnabled: false, - legacyBillingMigrationEnabled: false, - embeddedCheckoutEnabled: false, - billingSdkTopupEnabled: false, - billingSdkTopupRailEnabled: false, - v1PaymentRecovery: false, - freeTierJobAllowanceEnabled: false, - churnkeyAppId: '', - signupTurnstileMode: 'off', - supportsModelTypeTags: false, - onboardingTourEnabled: false, - assetsEnabled: false - }), + flags: reactive({ ...defaultFlags }), featureFlag: vi.fn((_, defaultValue) => computed(() => defaultValue)) } -export const useFeatureFlags = vi.fn(() => featureFlags) +export const useFeatureFlags = vi.fn(() => { + onTestFinished(() => { + Object.assign(featureFlags.flags, defaultFlags) + }) + return featureFlags +}) diff --git a/src/composables/auth/__mocks__/useAuthActions.ts b/src/composables/auth/__mocks__/useAuthActions.ts index 50ea460b89f..f8adcd834c8 100644 --- a/src/composables/auth/__mocks__/useAuthActions.ts +++ b/src/composables/auth/__mocks__/useAuthActions.ts @@ -1,4 +1,4 @@ -import { vi } from 'vitest' +import { onTestFinished, vi } from 'vitest' import { ref } from 'vue' import type { useAuthActions as realUseAuthActions } from '../useAuthActions' @@ -19,4 +19,9 @@ const actions: ReturnType = { accessError: ref(false) } -export const useAuthActions = vi.fn(() => actions) +export const useAuthActions = vi.fn(() => { + onTestFinished(() => { + actions.accessError.value = false + }) + return actions +}) diff --git a/src/composables/billing/useBillingRouting.test.ts b/src/composables/billing/useBillingRouting.test.ts index 5133b0825f9..0dad8bef91b 100644 --- a/src/composables/billing/useBillingRouting.test.ts +++ b/src/composables/billing/useBillingRouting.test.ts @@ -32,7 +32,6 @@ const team = fromPartial< describe('useBillingRouting', () => { beforeEach(() => { - vi.mocked(useFeatureFlags().flags).legacyBillingMigrationEnabled = false const refs = storeToRefs(useTeamWorkspaceStore()) mockActiveWorkspace = refs.activeWorkspace mockActiveWorkspaceBillingRail = refs.activeWorkspaceBillingRail diff --git a/src/composables/useCoreCommands.test.ts b/src/composables/useCoreCommands.test.ts index 9fec17e84dd..1d63dad08e3 100644 --- a/src/composables/useCoreCommands.test.ts +++ b/src/composables/useCoreCommands.test.ts @@ -242,7 +242,6 @@ describe('useCoreCommands', () => { const mockSubgraph = createMockSubgraph()! beforeEach(() => { - vi.mocked(useFeatureFlags().flags).assetsEnabled = false mockWorkflowStore = useWorkflowStore() mockWorkflowStore.activeWorkflow = fromPartial< NonNullable diff --git a/src/composables/useWorkflowActionsMenu.test.ts b/src/composables/useWorkflowActionsMenu.test.ts index e56bf0e6527..7a7e6e39f85 100644 --- a/src/composables/useWorkflowActionsMenu.test.ts +++ b/src/composables/useWorkflowActionsMenu.test.ts @@ -87,7 +87,6 @@ function findItem(items: MenuItems, label: string): WorkflowMenuAction { describe('useWorkflowActionsMenu', () => { beforeEach(() => { - vi.mocked(useFeatureFlags().flags).linearToggleEnabled = false mockBookmarkStore = useWorkflowBookmarkStore() mockWorkflowStore = useWorkflowStore() mockCommandStore = useCommandStore() diff --git a/src/platform/assets/components/AssetBrowserModal.test.ts b/src/platform/assets/components/AssetBrowserModal.test.ts index b42e8de306e..c1043031471 100644 --- a/src/platform/assets/components/AssetBrowserModal.test.ts +++ b/src/platform/assets/components/AssetBrowserModal.test.ts @@ -127,7 +127,6 @@ const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0)) beforeEach(() => { - vi.mocked(useFeatureFlags().flags).supportsModelTypeTags = false vi.mocked(useModelToNodeStore().getCategoryForNodeType).mockImplementation( () => 'checkpoints' ) diff --git a/src/platform/assets/composables/useAssetBrowser.test.ts b/src/platform/assets/composables/useAssetBrowser.test.ts index 7c9262dc6a5..9513dd85b71 100644 --- a/src/platform/assets/composables/useAssetBrowser.test.ts +++ b/src/platform/assets/composables/useAssetBrowser.test.ts @@ -29,7 +29,6 @@ function useAssetBrowser(...args: Parameters) { afterEach(() => { apps.splice(0).forEach((app) => app.unmount()) - vi.mocked(useFeatureFlags().flags).supportsModelTypeTags = false }) describe('useAssetBrowser', () => { diff --git a/src/platform/assets/services/assetService.test.ts b/src/platform/assets/services/assetService.test.ts index 8c3c18a0f17..069c2440ddb 100644 --- a/src/platform/assets/services/assetService.test.ts +++ b/src/platform/assets/services/assetService.test.ts @@ -88,7 +88,6 @@ function validAsset(overrides: Partial = {}): AssetItem { } beforeEach(() => { - vi.mocked(useFeatureFlags().flags).supportsModelTypeTags = false const registeredNodeTypes: Record = { CheckpointLoaderSimple: 'ckpt_name', LoraLoader: 'lora_name' diff --git a/src/platform/assets/utils/assetPreviewUtil.test.ts b/src/platform/assets/utils/assetPreviewUtil.test.ts index 70275406182..8db1f70f025 100644 --- a/src/platform/assets/utils/assetPreviewUtil.test.ts +++ b/src/platform/assets/utils/assetPreviewUtil.test.ts @@ -79,7 +79,6 @@ const localAssetWithPreview = { } beforeEach(() => { - vi.mocked(useFeatureFlags().flags).assetsEnabled = false vi.spyOn(useAssetsStore().outputAssets, 'invalidate').mockImplementation( mockInvalidateOutputAssets ) diff --git a/src/platform/assets/utils/assetUrlUtil.test.ts b/src/platform/assets/utils/assetUrlUtil.test.ts index bb000de6842..c313ce4b8f2 100644 --- a/src/platform/assets/utils/assetUrlUtil.test.ts +++ b/src/platform/assets/utils/assetUrlUtil.test.ts @@ -131,10 +131,6 @@ describe('getAssetFileUrl', () => { }) describe('with history-backed assets', () => { - beforeEach(() => { - vi.mocked(useFeatureFlags().flags).assetsEnabled = false - }) - it('uses preview_url, which already points at the file', () => { const asset = createAsset({ preview_url: '/api/view?filename=clip.webm&type=output&subfolder=vid' diff --git a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts index 48e06092201..8124c85286b 100644 --- a/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts +++ b/src/platform/assets/utils/resolveModelNodeFromAsset.test.ts @@ -46,7 +46,6 @@ function mockProvider( } beforeEach(() => { - vi.mocked(useFeatureFlags().flags).supportsModelTypeTags = false vi.mocked(useModelToNodeStore().getNodeProvider).mockImplementation( mockGetNodeProvider ) diff --git a/src/platform/cloud/subscription/composables/usePaymentReturnUrlLoader.test.ts b/src/platform/cloud/subscription/composables/usePaymentReturnUrlLoader.test.ts index 5cc72f16adf..9978265ed8a 100644 --- a/src/platform/cloud/subscription/composables/usePaymentReturnUrlLoader.test.ts +++ b/src/platform/cloud/subscription/composables/usePaymentReturnUrlLoader.test.ts @@ -11,7 +11,6 @@ vi.mock(import('@/composables/billing/useBillingContext')) vi.mock(import('@/composables/useFeatureFlags')) describe('usePaymentReturnUrlLoader', () => { beforeEach(() => { - vi.mocked(useFeatureFlags().flags).embeddedCheckoutEnabled = false window.history.replaceState({}, '', '/') }) diff --git a/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts b/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts index d52ca868eae..29a0a8eb9fb 100644 --- a/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts +++ b/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts @@ -124,7 +124,6 @@ function expectRekaPricingDialogProps( } beforeEach(() => { - vi.mocked(useFeatureFlags().flags).embeddedCheckoutEnabled = false Object.assign(useAuthStore(), { userId: 'user-1' }) vi.mocked(useDialogStore().closeDialog).mockImplementation(mockCloseDialog) diff --git a/src/platform/settings/composables/useSettingUI.test.ts b/src/platform/settings/composables/useSettingUI.test.ts index e024b0979e6..af6348d641c 100644 --- a/src/platform/settings/composables/useSettingUI.test.ts +++ b/src/platform/settings/composables/useSettingUI.test.ts @@ -93,8 +93,6 @@ function useSettingUI( } beforeEach(() => { - vi.mocked(useFeatureFlags().flags).partnerNodeGovernanceEnabled = false - vi.mocked(useFeatureFlags().flags).userSecretsEnabled = false vi.spyOn(usePartnerNodeGovernanceStore(), 'status', 'get').mockImplementation( () => { return env.state.partnerNodeGovernanceStatus diff --git a/src/platform/workflow/sharing/components/ShareWorkflowDialogContent.test.ts b/src/platform/workflow/sharing/components/ShareWorkflowDialogContent.test.ts index 0d701c39ebb..1971462be8e 100644 --- a/src/platform/workflow/sharing/components/ShareWorkflowDialogContent.test.ts +++ b/src/platform/workflow/sharing/components/ShareWorkflowDialogContent.test.ts @@ -132,7 +132,6 @@ describe('ShareWorkflowDialogContent', () => { const onClose = vi.fn() beforeEach(() => { - vi.mocked(useFeatureFlags().flags).comfyHubUploadEnabled = false vi.mocked(useFeatureFlags().flags).comfyHubProfileGateEnabled = true Object.assign(useWorkflowStore(), { activeWorkflow: { diff --git a/src/platform/workspace/auth/WorkspaceAuthGate.test.ts b/src/platform/workspace/auth/WorkspaceAuthGate.test.ts index 5356b96332e..0d17febbd5a 100644 --- a/src/platform/workspace/auth/WorkspaceAuthGate.test.ts +++ b/src/platform/workspace/auth/WorkspaceAuthGate.test.ts @@ -25,7 +25,6 @@ import WorkspaceAuthGate from './WorkspaceAuthGate.vue' vi.mock(import('firebase/auth'), { spy: true }) beforeEach(() => { - vi.mocked(useFeatureFlags().flags).unifiedCloudAuthEnabled = false vi.mocked(setPersistence).mockResolvedValue(undefined) vi.mocked(onAuthStateChanged).mockImplementation(vi.fn()) vi.mocked(onIdTokenChanged).mockImplementation(vi.fn()) diff --git a/src/platform/workspace/stores/useWorkspaceAuth.test.ts b/src/platform/workspace/stores/useWorkspaceAuth.test.ts index 40bec6a5394..c036c2ebb45 100644 --- a/src/platform/workspace/stores/useWorkspaceAuth.test.ts +++ b/src/platform/workspace/stores/useWorkspaceAuth.test.ts @@ -113,7 +113,6 @@ function expectedExpiresAtMs(expiresAt: string): string { } beforeEach(() => { - vi.mocked(useFeatureFlags().flags).unifiedCloudAuthEnabled = false vi.mocked(setPersistence).mockResolvedValue(undefined) vi.mocked(onAuthStateChanged).mockImplementation(vi.fn()) vi.mocked(onIdTokenChanged).mockImplementation(vi.fn()) diff --git a/src/stores/__tests__/authTokenPriority.test.ts b/src/stores/__tests__/authTokenPriority.test.ts index c76e308a85b..1d47624c4b8 100644 --- a/src/stores/__tests__/authTokenPriority.test.ts +++ b/src/stores/__tests__/authTokenPriority.test.ts @@ -45,7 +45,6 @@ describe('auth token priority chain', () => { } as Partial as MockUser beforeEach(() => { - vi.mocked(useFeatureFlags().flags).unifiedCloudAuthEnabled = false mockDistributionTypes.isCloud = true vi.mocked(vuefire.useFirebaseAuth).mockReturnValue( mockAuth as unknown as ReturnType diff --git a/src/stores/authStore.test.ts b/src/stores/authStore.test.ts index 651e399a85d..867c581cc43 100644 --- a/src/stores/authStore.test.ts +++ b/src/stores/authStore.test.ts @@ -120,7 +120,6 @@ describe('useAuthStore', () => { } as Partial as MockUser beforeEach(() => { - vi.mocked(useFeatureFlags().flags).unifiedCloudAuthEnabled = false mockResetSocket = vi.spyOn(api, 'resetSocket').mockResolvedValue(undefined) vi.stubGlobal('fetch', mockFetch) clearPreservedQuery(PRESERVED_QUERY_NAMESPACES.SHARE_AUTH) @@ -2346,7 +2345,6 @@ describe('useAuthStore in local/desktop distribution', () => { } as Partial as MockUser beforeEach(() => { - vi.mocked(useFeatureFlags().flags).unifiedCloudAuthEnabled = false mockDistributionTypes.isCloud = false mockDistributionTypes.isDesktop = false mockDistributionTypes.DISTRIBUTION = 'localhost' diff --git a/src/stores/workspace/sidebarTabStore.test.ts b/src/stores/workspace/sidebarTabStore.test.ts index fa33808bcbd..e6522deef1d 100644 --- a/src/stores/workspace/sidebarTabStore.test.ts +++ b/src/stores/workspace/sidebarTabStore.test.ts @@ -84,7 +84,6 @@ vi.mock( describe('useSidebarTabStore', () => { beforeEach(() => { - vi.mocked(useFeatureFlags().flags).assetsEnabled = false vi.mocked(useMenuItemStore().registerCommands).mockImplementation(() => {}) mockOpenModelLibraryBrowser.mockClear() })