Skip to content

Commit 7edda69

Browse files
author
Eric Wheeler
committed
test: update tests to work with safeWriteJson
Updated tests to work with safeWriteJson instead of direct fs.writeFile calls: - Updated importExport.test.ts to expect safeWriteJson calls instead of fs.writeFile - Fixed McpHub.test.ts by properly mocking fs/promises module: - Moved jest.mock() to the top of the file before any imports - Added mock implementations for all fs functions used by safeWriteJson - Updated the test setup to work with the mocked fs module All tests now pass successfully. Signed-off-by: Eric Wheeler <[email protected]>
1 parent f4128be commit 7edda69

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/core/config/__tests__/importExport.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,10 @@ describe("importExport", () => {
407407
contextProxy: mockContextProxy,
408408
})
409409

410-
expect(fs.writeFile).toHaveBeenCalledWith(
411-
"/mock/path/roo-code-settings.json",
412-
JSON.stringify({ providerProfiles: mockProviderProfiles, globalSettings: mockGlobalSettings }, null, 2),
413-
"utf-8",
414-
)
410+
expect(safeWriteJson).toHaveBeenCalledWith("/mock/path/roo-code-settings.json", {
411+
providerProfiles: mockProviderProfiles,
412+
globalSettings: mockGlobalSettings,
413+
})
415414
})
416415

417416
it("should handle errors during the export process", async () => {

src/services/mcp/__tests__/McpHub.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,35 @@ import type { ClineProvider } from "../../../core/webview/ClineProvider"
33
import type { ExtensionContext, Uri } from "vscode"
44
import { ServerConfigSchema } from "../McpHub"
55

6-
const fs = require("fs/promises")
76
const { McpHub } = require("../McpHub")
7+
const path = require("path")
8+
9+
// Mock fs/promises before importing anything that uses it
10+
jest.mock("fs/promises", () => ({
11+
access: jest.fn().mockResolvedValue(undefined),
12+
writeFile: jest.fn().mockResolvedValue(undefined),
13+
readFile: jest.fn().mockResolvedValue("{}"),
14+
unlink: jest.fn().mockResolvedValue(undefined),
15+
rename: jest.fn().mockResolvedValue(undefined),
16+
lstat: jest.fn().mockImplementation(() =>
17+
Promise.resolve({
18+
isDirectory: () => true,
19+
}),
20+
),
21+
mkdir: jest.fn().mockResolvedValue(undefined),
22+
}))
23+
24+
// Import the mocked fs
25+
const fs = require("fs/promises")
26+
27+
// Mock safeWriteJson
28+
jest.mock("../../../utils/safeWriteJson", () => ({
29+
safeWriteJson: jest.fn(async (filePath, data) => {
30+
// Instead of trying to write to the file system, just call fs.writeFile mock
31+
// This avoids the complex file locking and temp file operations
32+
return fs.writeFile(filePath, JSON.stringify(data), "utf8")
33+
}),
34+
}))
835

936
jest.mock("vscode", () => ({
1037
workspace: {
@@ -43,6 +70,9 @@ describe("McpHub", () => {
4370
// Mock console.error to suppress error messages during tests
4471
console.error = jest.fn()
4572

73+
// Reset the mock implementations before each test
74+
jest.clearAllMocks()
75+
4676
const mockUri: Uri = {
4777
scheme: "file",
4878
authority: "",

0 commit comments

Comments
 (0)