openclaw - ✅(Solved) Fix test(cli): logs-cli.test.ts has vitest mock hoisting pattern that mirrors #73177 [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#73324Fetched 2026-04-29 06:21:00
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
closed ×1cross-referenced ×1

src/cli/logs-cli.test.ts uses the same pre-existing vitest mock hoisting pattern that caused #73177 (qr-cli.test.ts). Top-level const declarations for mock functions are referenced inside vi.mock() factory functions. When vitest hoists those factories to the top of the file, the variables are not yet initialized, which can produce ReferenceError: Cannot access '__vi_import_X__' before initialization when the module dependency graph shifts.

Root Cause

Same as #73177: vi.mock() is hoisted by vitest before top-level const variables are initialized. Any future change that alters the import chain (e.g. adding a dependency to ../gateway/call.js, ../logging/log-tail.js, or ./gateway-rpc.js) will change module initialization order and cause the hoisted factory to execute before the captured variables are ready.

Fix Action

Fixed

PR fix notes

PR #73326: fix(test): resolve vitest mock hoisting in logs-cli.test.ts to prevent cascade CI failure

Description (problem / solution / changelog)

Fixes #73324

src/cli/logs-cli.test.ts uses the same pre-existing vitest mock hoisting pattern that caused #73177 (qr-cli.test.ts). Top-level const declarations for mock functions (callGatewayFromCli, readConfiguredLogTail, buildGatewayConnectionDetails) are referenced inside vi.mock() factory functions. When vitest hoists those factories to the top of the file, the variables are not yet initialized, which produces ReferenceError: Cannot access '__vi_import_X__' before initialization when the module dependency graph shifts (e.g. any future change that alters the import chain of ../gateway/call.js, ../logging/log-tail.js, or ./gateway-rpc.js).

Changes

  • Move the three mock function declarations into vi.hoisted() so they are available before the vi.mock() factories execute, following the same pattern used in #73206.
  • Zero runtime / business-logic change; test-only fix.

Impact

  • Prevents a future CI cascade failure similar to #73177.
  • All existing tests in logs-cli.test.ts continue to pass.

Changed files

  • src/cli/logs-cli.test.ts (modified, +40/-125)

Code Example

const callGatewayFromCli = vi.fn();
const readConfiguredLogTail = vi.fn();
const buildGatewayConnectionDetails = vi.fn(...);

vi.mock("../gateway/call.js", () => ({
  buildGatewayConnectionDetails: (...args) => buildGatewayConnectionDetails(...args),
}));

vi.mock("../logging/log-tail.js", () => ({
  readConfiguredLogTail: (...args) => readConfiguredLogTail(...args),
}));

vi.mock("./gateway-rpc.js", async () => {
  const actual = await vi.importActual(...);
  return {
    ...actual,
    callGatewayFromCli: (...args) => callGatewayFromCli(...args),
  };
});
RAW_BUFFERClick to expand / collapse

Summary

src/cli/logs-cli.test.ts uses the same pre-existing vitest mock hoisting pattern that caused #73177 (qr-cli.test.ts). Top-level const declarations for mock functions are referenced inside vi.mock() factory functions. When vitest hoists those factories to the top of the file, the variables are not yet initialized, which can produce ReferenceError: Cannot access '__vi_import_X__' before initialization when the module dependency graph shifts.

Affected code

const callGatewayFromCli = vi.fn();
const readConfiguredLogTail = vi.fn();
const buildGatewayConnectionDetails = vi.fn(...);

vi.mock("../gateway/call.js", () => ({
  buildGatewayConnectionDetails: (...args) => buildGatewayConnectionDetails(...args),
}));

vi.mock("../logging/log-tail.js", () => ({
  readConfiguredLogTail: (...args) => readConfiguredLogTail(...args),
}));

vi.mock("./gateway-rpc.js", async () => {
  const actual = await vi.importActual(...);
  return {
    ...actual,
    callGatewayFromCli: (...args) => callGatewayFromCli(...args),
  };
});

Root cause

Same as #73177: vi.mock() is hoisted by vitest before top-level const variables are initialized. Any future change that alters the import chain (e.g. adding a dependency to ../gateway/call.js, ../logging/log-tail.js, or ./gateway-rpc.js) will change module initialization order and cause the hoisted factory to execute before the captured variables are ready.

Suggested fix

Move the three mock function declarations into vi.hoisted() so they are available before the vi.mock() factories execute, following the same pattern used in #73206.

Impact

  • Prevents a future CI cascade failure similar to #73177
  • Zero runtime / business-logic change; test-only fix

extent analysis

TL;DR

Move the mock function declarations into vi.hoisted() to ensure they are initialized before vi.mock() factories execute.

Guidance

  • Identify all top-level const declarations for mock functions that are referenced inside vi.mock() factory functions.
  • Move these declarations into vi.hoisted() to make them available before the vi.mock() factories execute.
  • Verify that the ReferenceError: Cannot access '__vi_import_X__' before initialization error is resolved after applying the fix.
  • Review the import chain for any potential changes that could alter the module initialization order and cause similar issues in the future.

Example

vi.hoisted(() => {
  const callGatewayFromCli = vi.fn();
  const readConfiguredLogTail = vi.fn();
  const buildGatewayConnectionDetails = vi.fn(...);
});

Notes

This fix only applies to the specific issue caused by vitest hoisting vi.mock() factories before top-level const variables are initialized. Other issues may require different solutions.

Recommendation

Apply the suggested fix by moving the mock function declarations into vi.hoisted() to prevent future CI cascade failures and ensure test stability.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - ✅(Solved) Fix test(cli): logs-cli.test.ts has vitest mock hoisting pattern that mirrors #73177 [1 pull requests, 1 participants]