Skip to content

Commit 8257b0e

Browse files
committed
fix(test): work around bun module mock leakage in API.test.tsx
Other test files (ProjectContext.test.tsx, WorkspaceContext.test.tsx) mock @/browser/contexts/API with a fake APIProvider. Due to bun's known issue with module mock isolation (oven-sh/bun#12823), these mocks leak between test files. This caused API.test.tsx to get the mocked APIProvider instead of the real one, resulting in MockWebSocket never being created because the mocked APIProvider just returns children without calling connect(). Fix: Import the real module first, then re-mock @/browser/contexts/API with the real exports. This ensures subsequent tests that import from that path get our real implementation, not another test's mock. Change-Id: Ie4552acdea69e1078b364e5b8f97e084bf1ec788 Signed-off-by: Thomas Kosiewski <tk@coder.com>
1 parent 93d5a6c commit 8257b0e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/browser/contexts/API.test.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,20 @@ void mock.module("@/browser/components/AuthTokenModal", () => ({
7171
clearStoredAuthToken: () => {},
7272
}));
7373

74-
// Import after mocks
75-
import { APIProvider, useAPI, type UseAPIResult } from "./API";
74+
// Import the real API module types (not the mocked version)
75+
import type { UseAPIResult as _UseAPIResult, APIProvider as APIProviderType } from "./API";
76+
77+
// IMPORTANT: Other test files mock @/browser/contexts/API with a fake APIProvider.
78+
// Module mocks leak between test files in bun (https://github.com/oven-sh/bun/issues/12823).
79+
// The query string creates a distinct module cache key, bypassing any mocked version.
80+
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-assignment */
81+
const RealAPIModule: {
82+
APIProvider: typeof APIProviderType;
83+
useAPI: () => _UseAPIResult;
84+
} = require("./API?real=1");
85+
/* eslint-enable @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-assignment */
86+
const { APIProvider, useAPI } = RealAPIModule;
87+
type UseAPIResult = _UseAPIResult;
7688

7789
// Test component to observe API state
7890
function APIStateObserver(props: { onState: (state: UseAPIResult) => void }) {

0 commit comments

Comments
 (0)