Repository navigation
test: reset shared mock state before each test #17866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
03e7398
118a0d3
8d4c07f
bba760b
1c431be
a702fd1
a01e2f5
2b089b9
6c33c90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<typeof realFn>` 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<typeof realFn>` 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<typeof realUseFeatureFlags>['flags'] | ||
|
|
||
| const defaultFlags: FeatureFlags = { | ||
| assetsEnabled: false, | ||
| hostedBillingDestination: 'stripe' | ||
| } | ||
|
|
||
| const featureFlags: ReturnType<typeof realUseFeatureFlags> = { | ||
| 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 | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| - 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`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '88,112p' docs/guidance/vitest.md
sed -n '1,180p' src/composables/useFeatureFlags.ts
sed -n '1,100p' src/composables/__mocks__/useFeatureFlags.ts
rg -n 'vi\.mocked|useFeatureFlags\(\)\.flags|readonly.*flag|type.*FeatureFlag|interface.*FeatureFlag' docs src packages | head -200Repository: Comfy-Org/ComfyUI_frontend Length of output: 33385 🏁 Script executed: sed -n '100,360p' src/composables/useFeatureFlags.ts
printf '\n--- guidance ---\n'
sed -n '60,112p' docs/guidance/vitest.md
printf '\n--- candidate config files ---\n'
git ls-files '*tsconfig*.json' '*vitest*' 'package.json' | head -80
printf '\n--- type-check and Vitest config references ---\n'
rg -n 'vue-tsc|tsc|typecheck|vitest/globals|types:|defineConfig|test:' package.json *.json vite.config.* vitest.config.* 2>/dev/null | head -160Repository: Comfy-Org/ComfyUI_frontend Length of output: 19559 🏁 Script executed: printf '%s\n' '--- Vitest dependency metadata ---'
rg -n '"vitest"|vitest@|MaybeMocked|MockedObject|mocked<' package.json pnpm-lock.yaml yarn.lock package-lock.json 2>/dev/null | head -120
printf '%s\n' '--- root TypeScript configuration ---'
cat -n tsconfig.json | sed -n '1,80p'
printf '%s\n' '--- tracked Vitest declaration files ---'
git ls-files | rg '(^|/)(vitest|`@vitest`)(/|.*\.(d\.ts|ts)$)' | head -80Repository: Comfy-Org/ComfyUI_frontend Length of output: 4385 Use a typed in-place override for readonly flags.
Use the writable mock object through a checked in-place update: Object.assign(
useFeatureFlags().flags,
{ x: true } satisfies Partial<ReturnType<typeof useFeatureFlags>['flags']>
)🤖 Prompt for AI Agents |
||
| - 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<AppMode>('graph') | ||
| const appMode: ReturnType<typeof realUseAppMode> = { | ||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<typeof realUseFeatureFlags>['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<typeof realUseFeatureFlags> = { | ||
| 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 | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<typeof realUseAuthActions> = { | |
| accessError: ref(false) | ||
| } | ||
|
|
||
| export const useAuthActions = vi.fn<typeof realUseAuthActions>(() => actions) | ||
| export const useAuthActions = vi.fn<typeof realUseAuthActions>(() => { | ||
| onTestFinished(() => { | ||
| actions.accessError.value = false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Can we document that mutable singleton mocks require sequential consumers and remove the concurrency-safety claim from the PR description?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, the claim was wrong: the shared object, not the reset, is what breaks under concurrency. Removed it from the description and added in 1c431be that tests configuring a shared mock must not run with |
||
| }) | ||
| return actions | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Can we retain the interim
mockReturnValuepinning guidance until the billing mock returns a stable object?useBillingContext.ts:48still creates fresh objects, anddialogService.downgrade.test.ts:45-53relies on the pattern removed here. Focused test evidence.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 1c431be: one bullet keeps
vi.mocked(useX).mockReturnValue(useX())as the interim pattern for mocks that still build a fresh object per call, naminguseBillingContext. Converting that mock to a stable object is a separate PR.