Reviewing Unit Tests for ComfyUI_frontend
Overview
Review for behavior and current repo rules, not motion. Compare to authoritative rules, not prior diffs or legacy snippets.
Review Workflow
- Identify the test type: component, store, composable, util, or bugfix regression.
- Name the behavior the test proves. If you cannot say it in one sentence, request changes.
- Open the authoritative doc section before judging structure.
- Scan the red flags below.
- State the verdict first. Name the failure mode. Cite the doc or rule.
Source of Truth / Precedence
When docs and examples conflict, use this order:
- Explicit repo rules, lint rules, and note blocks.
docs/testing/vitest-patterns.md
- Rule sections in
docs/testing/unit-testing.md, docs/testing/store-testing.md, and docs/testing/component-testing.md
- Example snippets
- Prior diffs
Apply these repo-specific clarifications:
docs/testing/component-testing.md starts with the authoritative rule: new component tests use @testing-library/vue with @testing-library/user-event. The @vue/test-utils snippets below it are legacy examples.
docs/testing/store-testing.md still contains as any examples. Treat them as legacy snippets, not approval for new or edited test code.
- If docs conflict, prefer the stricter newer rule and call out the doc ambiguity. Do not approve through it.
- Motion != fix.
30-Second Red Flags
| If you see... |
Failure mode |
Default action |
New @vue/test-utils import in a new component test |
legacy test API |
Request changes |
vi.mock('vue-i18n', ...) |
mocked i18n |
Request changes |
as any, @ts-expect-error, as Mock, as ReturnType<typeof vi.fn>, as unknown as X |
unnecessary cast or type escape |
Request changes unless the author proves no safer type exists |
getXMock(), renamed wrapper, or helper that only returns a mocked value |
alias-by-renaming |
Request changes |
beforeEach recreates the return object for a module-mocked composable or service |
shared mock setup drift |
Request changes |
| Assertions only check defaults, mock plumbing, or CSS hooks |
non-behavioral test |
Request changes |
| Bugfix test has no proof it fails on pre-fix code |
unproven regression |
Request changes |
Rationalization Table
| Excuse |
Reality |
| "I restructured the mocks" |
If the indirection stayed, nothing improved. Flag alias-by-renaming. |
| "The docs do it" |
Rule, note, and lint beat legacy snippet. Compare to the current rule, not the nearest example. |
| "TypeScript required the cast" |
vi.mocked() usually narrows mock methods. Assertion-only references need no cast. |
"Putting it in beforeEach is DRY" |
Recreating module mock state in hooks hides singleton behavior and drifts from the documented pattern. |
| "It is only a nit" |
Explicit repo-rule violations are never nits. |
| "No behavior changed, just cleanup" |
Motion != fix. Ask what behavior got stronger. |
| "Mental revert is enough" |
For bugfix tests, establish red on pre-fix code or ask the author to show it. |
Mocking Rules
- Fail helpers that do not remove repeated setup, encode domain meaning, or simplify assertions. Barely earning the abstraction is not enough.
- For composables with reactive or singleton state, define stable mock state inside the
vi.mock() factory. Access it per test via the composable itself. See docs/testing/unit-testing.md "Mocking Composables with Reactive State".
- This does not ban local test data builders or per-test
vi.spyOn(...).
- Mock seams, not the project-owned module you are trying to exercise. For store tests, prefer real Pinia plus
createTestingPinia({ stubActions: false }) per docs/testing/vitest-patterns.md and docs/testing/store-testing.md.
Alias-by-Renaming
// Before
const mockAdd = vi.hoisted(() => vi.fn())
// After: same indirection, new name
function getToastAddMock() {
return useToast().add
}
If the wrapper only renames or relays a mocked value, fail it. Inline the lookup at the call site or fetch the singleton mock via the documented pattern.
vi.mocked() Scope
| Use case |
vi.mocked() required? |
.mockReturnValue, .mockResolvedValue, .mockImplementation |
Yes |
.mock.calls, .mock.results |
Yes |
expect(fn).toHaveBeenCalled() |
No |
expect(fn).toHaveBeenCalledWith(...) |
No |
- Flag casts whenever
vi.mocked() would narrow correctly.
- Do not add
vi.mocked() around assertion-only references just for style.
Reset Hygiene
- Flag per-mock
mockClear() or mockReset() when vi.clearAllMocks() or vi.resetAllMocks() already runs in the relevant hook chain.
- Review for redundancy or broken state management. Do not bikeshed
clearAllMocks vs resetAllMocks unless behavior depends on it.
Third-Party Seams
- Distinguish trivial hooks from behavior-rich APIs.
- Mocking single-method third-party hooks like
primevue/usetoast is usually acceptable.
- That exception does not justify mocking behavior-rich third-party modules.
vue-i18n
Test-Body Rules
| Smell |
Review bar |
| Change-detector test |
Reject. Default values alone prove nothing. |
| Mock-only assertion |
Accept collaborator-call assertions only when the call is the meaningful external effect and the test also exercises the triggering behavior. |
| Non-behavioral assertion |
Reject tests that only check classes, utility hooks, or styling internals. |
New component test using @vue/test-utils |
Request changes. Use @testing-library/vue plus @testing-library/user-event. |
any, as any, or @ts-expect-error in new or edited test code |
Request changes unless the author proves no safer type exists. Legacy doc snippets do not authorize it. |
Bugfix Regression Proof
For fix: PRs or bugfix diffs:
- Identify the production change that fixes the bug.
- Verify the new test fails on pre-fix code, or ask the author to show it.
- If the test passes on broken code, request changes.
A regression test that never proves red does not pin the bug.
Review Output Rules
- State verdict before procedural questions.
- Do not lead with approval language like
LGTM, just one nit or approve and move on?.
- Name the failure mode directly:
alias-by-renaming, unnecessary cast, mocked i18n, mock-only assertion, unproven regression.
- Link the authoritative doc section in the review comment.
- If an explicit repo rule, lint rule, or authoritative doc note is violated, do not downgrade it to "minor deviation" or "nit".
Quick Reference
Key Files to Read
1---2name: reviewing-unit-tests3description: Use when reviewing Vitest unit-test diffs in ComfyUI_frontend, especially new mocks, store tests, component tests, or bugfix regression tests.4---56# Reviewing Unit Tests for ComfyUI_frontend78## Overview910Review for behavior and current repo rules, not motion. Compare to authoritative rules, not prior diffs or legacy snippets.1112## Review Workflow13141. Identify the test type: component, store, composable, util, or bugfix regression.152. Name the behavior the test proves. If you cannot say it in one sentence, request changes.163. Open the authoritative doc section before judging structure.174. Scan the red flags below.185. State the verdict first. Name the failure mode. Cite the doc or rule.1920## Source of Truth / Precedence2122When docs and examples conflict, use this order:23241. Explicit repo rules, lint rules, and note blocks.252. [`docs/testing/vitest-patterns.md`](../../../docs/testing/vitest-patterns.md)263. Rule sections in [`docs/testing/unit-testing.md`](../../../docs/testing/unit-testing.md), [`docs/testing/store-testing.md`](../../../docs/testing/store-testing.md), and [`docs/testing/component-testing.md`](../../../docs/testing/component-testing.md)274. Example snippets285. Prior diffs2930Apply these repo-specific clarifications:3132- [`docs/testing/component-testing.md`](../../../docs/testing/component-testing.md) starts with the authoritative rule: new component tests use `@testing-library/vue` with `@testing-library/user-event`. The `@vue/test-utils` snippets below it are legacy examples.33- [`docs/testing/store-testing.md`](../../../docs/testing/store-testing.md) still contains `as any` examples. Treat them as legacy snippets, not approval for new or edited test code.34- If docs conflict, prefer the stricter newer rule and call out the doc ambiguity. Do not approve through it.35- Motion != fix.3637## 30-Second Red Flags3839| If you see... | Failure mode | Default action |40| ----------------------------------------------------------------------------------------- | ------------------------------- | ------------------------------------------------------------- |41| New `@vue/test-utils` import in a new component test | legacy test API | Request changes |42| `vi.mock('vue-i18n', ...)` | mocked i18n | Request changes |43| `as any`, `@ts-expect-error`, `as Mock`, `as ReturnType<typeof vi.fn>`, `as unknown as X` | unnecessary cast or type escape | Request changes unless the author proves no safer type exists |44| `getXMock()`, renamed wrapper, or helper that only returns a mocked value | alias-by-renaming | Request changes |45| `beforeEach` recreates the return object for a module-mocked composable or service | shared mock setup drift | Request changes |46| Assertions only check defaults, mock plumbing, or CSS hooks | non-behavioral test | Request changes |47| Bugfix test has no proof it fails on pre-fix code | unproven regression | Request changes |4849## Rationalization Table5051| Excuse | Reality |52| ----------------------------------- | ------------------------------------------------------------------------------------------------------ |53| "I restructured the mocks" | If the indirection stayed, nothing improved. Flag `alias-by-renaming`. |54| "The docs do it" | Rule, note, and lint beat legacy snippet. Compare to the current rule, not the nearest example. |55| "TypeScript required the cast" | `vi.mocked()` usually narrows mock methods. Assertion-only references need no cast. |56| "Putting it in `beforeEach` is DRY" | Recreating module mock state in hooks hides singleton behavior and drifts from the documented pattern. |57| "It is only a nit" | Explicit repo-rule violations are never nits. |58| "No behavior changed, just cleanup" | Motion != fix. Ask what behavior got stronger. |59| "Mental revert is enough" | For bugfix tests, establish red on pre-fix code or ask the author to show it. |6061## Mocking Rules6263- Fail helpers that do not remove repeated setup, encode domain meaning, or simplify assertions. Barely earning the abstraction is not enough.64- For composables with reactive or singleton state, define stable mock state inside the `vi.mock()` factory. Access it per test via the composable itself. See [`docs/testing/unit-testing.md`](../../../docs/testing/unit-testing.md) "Mocking Composables with Reactive State".65- This does not ban local test data builders or per-test `vi.spyOn(...)`.66- Mock seams, not the project-owned module you are trying to exercise. For store tests, prefer real Pinia plus `createTestingPinia({ stubActions: false })` per [`docs/testing/vitest-patterns.md`](../../../docs/testing/vitest-patterns.md) and [`docs/testing/store-testing.md`](../../../docs/testing/store-testing.md).6768### Alias-by-Renaming6970```ts71// Before72const mockAdd = vi.hoisted(() => vi.fn())7374// After: same indirection, new name75function getToastAddMock() {76 return useToast().add77}78```7980If the wrapper only renames or relays a mocked value, fail it. Inline the lookup at the call site or fetch the singleton mock via the documented pattern.8182### `vi.mocked()` Scope8384| Use case | `vi.mocked()` required? |85| --------------------------------------------------------------- | ----------------------- |86| `.mockReturnValue`, `.mockResolvedValue`, `.mockImplementation` | Yes |87| `.mock.calls`, `.mock.results` | Yes |88| `expect(fn).toHaveBeenCalled()` | No |89| `expect(fn).toHaveBeenCalledWith(...)` | No |9091- Flag casts whenever `vi.mocked()` would narrow correctly.92- Do not add `vi.mocked()` around assertion-only references just for style.9394### Reset Hygiene9596- Flag per-mock `mockClear()` or `mockReset()` when `vi.clearAllMocks()` or `vi.resetAllMocks()` already runs in the relevant hook chain.97- Review for redundancy or broken state management. Do not bikeshed `clearAllMocks` vs `resetAllMocks` unless behavior depends on it.9899### Third-Party Seams100101- Distinguish trivial hooks from behavior-rich APIs.102- Mocking single-method third-party hooks like `primevue/usetoast` is usually acceptable.103- That exception does not justify mocking behavior-rich third-party modules.104105### `vue-i18n`106107- Never mock `vue-i18n` in component tests.108- Use real `createI18n` per [`docs/testing/vitest-patterns.md`](../../../docs/testing/vitest-patterns.md) and the shared [`testI18n`](../../../src/components/searchbox/v2/__test__/testUtils.ts) setup.109110## Test-Body Rules111112| Smell | Review bar |113| ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |114| Change-detector test | Reject. Default values alone prove nothing. |115| Mock-only assertion | Accept collaborator-call assertions only when the call is the meaningful external effect and the test also exercises the triggering behavior. |116| Non-behavioral assertion | Reject tests that only check classes, utility hooks, or styling internals. |117| New component test using `@vue/test-utils` | Request changes. Use `@testing-library/vue` plus `@testing-library/user-event`. |118| `any`, `as any`, or `@ts-expect-error` in new or edited test code | Request changes unless the author proves no safer type exists. Legacy doc snippets do not authorize it. |119120## Bugfix Regression Proof121122For `fix:` PRs or bugfix diffs:1231241. Identify the production change that fixes the bug.1252. Verify the new test fails on pre-fix code, or ask the author to show it.1263. If the test passes on broken code, request changes.127128A regression test that never proves red does not pin the bug.129130## Review Output Rules131132- State verdict before procedural questions.133- Do not lead with approval language like `LGTM, just one nit` or `approve and move on?`.134- Name the failure mode directly: `alias-by-renaming`, `unnecessary cast`, `mocked i18n`, `mock-only assertion`, `unproven regression`.135- Link the authoritative doc section in the review comment.136- If an explicit repo rule, lint rule, or authoritative doc note is violated, do not downgrade it to "minor deviation" or "nit".137138## Quick Reference139140| When you see... | Read this |141| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |142| New `vi.mock(...)` for a composable | [`docs/testing/unit-testing.md`](../../../docs/testing/unit-testing.md) -> "Mocking Composables with Reactive State" |143| New store test or store mock | [`docs/testing/vitest-patterns.md`](../../../docs/testing/vitest-patterns.md) setup + [`docs/testing/store-testing.md`](../../../docs/testing/store-testing.md) |144| New component test | Top note in [`docs/testing/component-testing.md`](../../../docs/testing/component-testing.md) |145| `vue-i18n` in a component test | [`docs/testing/vitest-patterns.md`](../../../docs/testing/vitest-patterns.md) + [`src/components/searchbox/v2/__test__/testUtils.ts`](../../../src/components/searchbox/v2/__test__/testUtils.ts) |146| Cast around a mock | [`docs/guidance/typescript.md`](../../../docs/guidance/typescript.md) -> "Type Assertion Hierarchy" |147148## Key Files to Read149150| Purpose | Path |151| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------- |152| Composable mocking patterns | [`docs/testing/unit-testing.md`](../../../docs/testing/unit-testing.md) |153| Store testing patterns | [`docs/testing/store-testing.md`](../../../docs/testing/store-testing.md) |154| Repo-wide Vitest setup defaults | [`docs/testing/vitest-patterns.md`](../../../docs/testing/vitest-patterns.md) |155| Component testing rule for new tests | [`docs/testing/component-testing.md`](../../../docs/testing/component-testing.md) |156| Real i18n setup | [`src/components/searchbox/v2/__test__/testUtils.ts`](../../../src/components/searchbox/v2/__test__/testUtils.ts) |