Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions __mocks__/@datadog/browser-rum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { onTestFinished, vi } from 'vitest'
import type { DatadogRum as RealDatadogRum } from '@datadog/browser-rum'

type DatadogRum = Pick<
RealDatadogRum,
| 'getGlobalContext'
| 'getInitConfiguration'
| 'init'
| 'setGlobalContextProperty'
>

const context: ReturnType<DatadogRum['getGlobalContext']> = {}
let contextCleanupRegistered = false

export const datadogRum = vi.mockObject<DatadogRum>(
{
getGlobalContext: () => context,
getInitConfiguration: () => undefined,
init: () => {},
setGlobalContextProperty: (key, value) => {
if (!contextCleanupRegistered) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: moving the hook onto the write path is the right fix, but it does shift where the "must be inside a test" constraint bites. Vitest throws Hook onTestFinished() can only be called inside a test when there is no current test (@vitest/runner chunk-artifact.js:915), so this now throws if an adopter writes context from beforeAll or at module scope rather than from a test body.

That is a more likely pattern than the old read-path placement invited — "initialize RUM once for the whole suite" in beforeAll is a natural thing for the next adopter to write, and it would fail on the first setGlobalContextProperty call with an error that points at Vitest internals rather than at this mock. initDatadogRum.test.ts is unaffected because every initDatadogRum call happens inside a test.

Worth either a short comment here recording that writes must happen inside a test, or having the mock clear context from a beforeEach so the constraint disappears entirely. Non-blocking — the current adopter is correct as written.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No code change. This constraint is intentional and is now explicit in docs/guidance/vitest.md: shared mocks must not install module-level beforeEach; callers configure mutable mock state from beforeEach or a test so onTestFinished can bind to the owning test. The current adopter complies.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked as FE-2714. The PR merged with this thread unresolved, so the finding is carried in Linear rather than lost here. Filed automatically; close the ticket if the finding is no longer true at main.

onTestFinished(() => {
for (const key of Object.keys(context)) delete context[key]
contextCleanupRegistered = false
})
contextCleanupRegistered = true
}
context[key] = value
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: the cleanup is registered on the read path, but the write path is what dirties the shared object — so a test that only initializes leaks context into the next test.

setGlobalContextProperty mutates the module-scoped context on line 25 without registering anything. The onTestFinished that clears it lives inside getGlobalContext (line 17), so a test that drives initDatadogRum and asserts only on datadogRum.init never registers the cleanup. mockReset/restoreMocks reset the spies but do not delete keys from a plain object, so the state survives into the following test.

Reproduced at 3275da66 by prepending two tests to initDatadogRum.test.ts:

it('writes global context without reading it', async () => {
  await initDatadogRum('cloud.comfy.org')
  expect(datadogRum.setGlobalContextProperty).toHaveBeenCalled()
})

it('sees a pristine global context', () => {
  expect(datadogRum.getGlobalContext()).toEqual({})
})
FAIL  PROBE: context leak across tests > sees a pristine global context
AssertionError: expected { comfyui_frontend_version: '1.24.0' } to deeply equal {}

The current adopter suite passes only because its tests happen not to read the context after an init-only test, which makes this an order-dependent trap in shared infrastructure the PR is explicitly building for reuse.

The vue-router mock in this same PR shows the shape that works: query is module-private and reachable only through useRoute(), so every write is necessarily preceded by the call that registers cleanup. Here the two paths are independent, so registering in one doesn't cover the other.

Cheapest fix that keeps your no-setter-hooks rule is to register from the write path too:

setGlobalContextProperty: (key, value) => {
  onTestFinished(() => {
    for (const k of Object.keys(context)) delete context[k]
  })
  context[key] = value
}

Or register the reset once at module scope with a beforeEach(clearContext) in the mock file, which drops the duplication and makes the guarantee unconditional. Either is fine by me — the property I care about is that dirtying the object always arms the cleanup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bdf524d624. Cleanup is now registered by setGlobalContextProperty, the path that dirties the shared object; getGlobalContext is a read only. I also changed the no-isolation lifecycle proof so one test writes without reading and the following test asserts pristine context. The proof passes 6/6, and initDatadogRum.test.ts passes 18/18 both ordered and shuffled (seed=2343); typecheck and the file lint/format checks pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up in c140dbc1be: the write path now registers at most one cleanup per test. The callback clears both the context and registration flag. I exercised multiple writes without a read followed by a pristine-context test; the no-isolation proof remains 6/6, and the Datadog suite remains 18/18 ordered and shuffled.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked as FE-2713. The PR merged with this thread unresolved, so the finding is carried in Linear rather than lost here. Filed automatically; close the ticket if the finding is no longer true at main.

}
},
{ spy: true }
)
6 changes: 6 additions & 0 deletions __mocks__/@vueuse/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { vi } from 'vitest'
import type { useEventListener as realUseEventListener } from '@vueuse/core'

export const useEventListener = vi.fn<typeof realUseEventListener>(() =>
vi.fn()
)
8 changes: 8 additions & 0 deletions __mocks__/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { vi } from 'vitest'
import type realAxios from 'axios'

const axios: Pick<typeof realAxios, 'get'> = {
get: vi.fn()
}

export default axios
24 changes: 24 additions & 0 deletions __mocks__/vue-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { onTestFinished, vi } from 'vitest'
import type { LocationQuery, Router } from 'vue-router'

type VueRouterMock = {
useRoute: () => { query: LocationQuery }
useRouter: () => Pick<Router, 'replace'>
}

const query: LocationQuery = {}
const router: Pick<Router, 'replace'> = {
replace: vi.fn(async () => {})
}

const vueRouter: VueRouterMock = {
useRoute: vi.fn(() => {
onTestFinished(() => {
for (const key of Object.keys(query)) delete query[key]
})
return { query }
}),
useRouter: vi.fn(() => router)
}

export const { useRoute, useRouter } = vueRouter
7 changes: 7 additions & 0 deletions src/__mocks__/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { vi } from 'vitest'
import type { st as realSt, t as realT } from '@/i18n'

export const t = vi.fn<typeof realT>((key) => String(key))
export const st = vi.fn<typeof realSt>(
(key, fallbackMessage) => fallbackMessage || key
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import userEvent from '@testing-library/user-event'
import { render, screen } from '@testing-library/vue'
import PrimeVue from 'primevue/config'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ref } from 'vue'
import { computed, ref } from 'vue'
import { createI18n } from 'vue-i18n'

import type { MissingPackGroup } from '@/components/rightSidePanel/errors/useErrorGroups'
import { useManagerState } from '@/workbench/extensions/manager/composables/useManagerState'
import { useComfyManagerStore } from '@/workbench/extensions/manager/stores/comfyManagerStore'

import MissingPackGroupRow from './MissingPackGroupRow.vue'

const mockInstallAllPacks = vi.fn()
const mockIsInstalling = ref(false)
const mockShouldShowManagerButtons = { value: false }
const mockOpenManager = vi.fn()
const mockShouldShowManagerButtons = ref(false)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const mockMissingNodePacks = ref<Array<{ id: string; name: string }>>([])
const mockIsLoading = ref(false)

Expand All @@ -39,17 +39,7 @@ vi.mock<unknown>(
})
)

vi.mock<unknown>(
import('@/workbench/extensions/manager/composables/useManagerState'),

() => ({
useManagerState: () => ({
isNewManagerUI: { value: false },
shouldShowManagerButtons: mockShouldShowManagerButtons,
openManager: mockOpenManager
})
})
)
vi.mock(import('@/workbench/extensions/manager/composables/useManagerState'))

vi.mock<unknown>(
import('@/workbench/extensions/manager/types/comfyManagerTypes'),
Expand Down Expand Up @@ -133,6 +123,9 @@ describe('MissingPackGroupRow', () => {
beforeEach(() => {
vi.mocked(useComfyManagerStore().isPackInstalled).mockReturnValue(false)
mockShouldShowManagerButtons.value = false
useManagerState().shouldShowManagerButtons = computed(
() => mockShouldShowManagerButtons.value
)
mockIsInstalling.value = false
mockMissingNodePacks.value = []
mockIsLoading.value = false
Expand Down
22 changes: 5 additions & 17 deletions src/composables/queue/useJobMenu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Ref } from 'vue'

import type { JobListItem } from '@/composables/queue/useJobList'
import type { MenuEntry } from '@/composables/queue/useJobMenu'
import { useMediaAssetActions } from '@/platform/assets/composables/useMediaAssetActions'

vi.mock(import('@/platform/distribution/types'), () => ({
isCloud: false
Expand All @@ -35,12 +36,7 @@ vi.mock(import('@/composables/useCopyToClipboard'), () => ({
})
}))

const stMock = vi.fn((_: string, fallback?: string) => fallback ?? _)
const tMock = vi.fn((key: string) => `i18n:${key}`)
vi.mock(import('@/i18n'), () => ({
st: (...args: Parameters<typeof stMock>) => stMock(...args),
t: (...args: Parameters<typeof tMock>) => tMock(...args)
}))
vi.mock(import('@/i18n'))

const mapTaskOutputToAssetItemMock = vi.fn()
vi.mock(import('@/platform/assets/composables/media/assetMappers'), () => ({
Expand All @@ -50,15 +46,7 @@ vi.mock(import('@/platform/assets/composables/media/assetMappers'), () => ({
) => mapTaskOutputToAssetItemMock(taskItem, output)
}))

const mediaAssetActionsMock = {
deleteAssets: vi.fn()
}
vi.mock<unknown>(
import('@/platform/assets/composables/useMediaAssetActions'),
() => ({
useMediaAssetActions: () => mediaAssetActionsMock
})
)
vi.mock(import('@/platform/assets/composables/useMediaAssetActions'))

let settingStoreMock: ReturnType<typeof useSettingStore>

Expand Down Expand Up @@ -163,7 +151,7 @@ describe('useJobMenu', () => {
vi.mocked(queueStoreMock.update).mockResolvedValue(undefined)
vi.mocked(queueStoreMock.delete).mockResolvedValue(undefined)
cancelJobMock.mockResolvedValue(undefined)
mediaAssetActionsMock.deleteAssets.mockResolvedValue(false)
vi.mocked(useMediaAssetActions().deleteAssets).mockResolvedValue(false)
mapTaskOutputToAssetItemMock.mockImplementation((task, output) => ({
task,
output
Expand Down Expand Up @@ -682,7 +670,7 @@ describe('useJobMenu', () => {
})

it('does not refresh queue when delete cancelled', async () => {
mediaAssetActionsMock.deleteAssets.mockResolvedValue(false)
vi.mocked(useMediaAssetActions().deleteAssets).mockResolvedValue(false)
const { jobMenuEntries } = mountJobMenu()
setCurrentItem(
createJobItem({
Expand Down
4 changes: 1 addition & 3 deletions src/composables/useServerLogs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ vi.mock<unknown>(import('@/scripts/api'), () => ({
}
}))

vi.mock(import('@vueuse/core'), () => ({
useEventListener: vi.fn().mockReturnValue(vi.fn())
}))
vi.mock(import('@vueuse/core'))

describe('useServerLogs', () => {
it('should initialize with empty logs array', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { vi } from 'vitest'
import type { useMediaAssetActions as realUseMediaAssetActions } from '../useMediaAssetActions'

type MediaAssetActions = ReturnType<typeof realUseMediaAssetActions>

const mediaAssetActions = vi.mockObject<MediaAssetActions>(
{
downloadAssets: () => {},
deleteAssets: async () => false,
copyJobId: async () => {},
addWorkflow: async () => {},
addMultipleToWorkflow: async () => {},
openWorkflow: async () => {},
openMultipleWorkflows: async () => {},
exportWorkflow: async () => {},
exportMultipleWorkflows: async () => {}
},
{ spy: true }
)

export const useMediaAssetActions = vi.fn(() => mediaAssetActions)
Loading
Loading