Skip to content

Commit dd54108

Browse files
Eric Wheelerdaniel-lxs
authored andcommitted
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 ca0afb2 commit dd54108

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@ import { ServerConfigSchema, McpHub } from "../McpHub"
55
import fs from "fs/promises"
66
import { vi, Mock } from "vitest"
77

8+
// Mock fs/promises before importing anything that uses it
9+
vi.mock("fs/promises", () => ({
10+
default: {
11+
access: vi.fn().mockResolvedValue(undefined),
12+
writeFile: vi.fn().mockResolvedValue(undefined),
13+
readFile: vi.fn().mockResolvedValue("{}"),
14+
unlink: vi.fn().mockResolvedValue(undefined),
15+
rename: vi.fn().mockResolvedValue(undefined),
16+
lstat: vi.fn().mockImplementation(() =>
17+
Promise.resolve({
18+
isDirectory: () => true,
19+
}),
20+
),
21+
mkdir: vi.fn().mockResolvedValue(undefined),
22+
},
23+
access: vi.fn().mockResolvedValue(undefined),
24+
writeFile: vi.fn().mockResolvedValue(undefined),
25+
readFile: vi.fn().mockResolvedValue("{}"),
26+
unlink: vi.fn().mockResolvedValue(undefined),
27+
rename: vi.fn().mockResolvedValue(undefined),
28+
lstat: vi.fn().mockImplementation(() =>
29+
Promise.resolve({
30+
isDirectory: () => true,
31+
}),
32+
),
33+
mkdir: vi.fn().mockResolvedValue(undefined),
34+
}))
35+
36+
// Mock safeWriteJson
37+
vi.mock("../../../utils/safeWriteJson", () => ({
38+
safeWriteJson: vi.fn(async (filePath, data) => {
39+
// Instead of trying to write to the file system, just call fs.writeFile mock
40+
// This avoids the complex file locking and temp file operations
41+
return fs.writeFile(filePath, JSON.stringify(data), "utf8")
42+
}),
43+
}))
44+
845
vi.mock("vscode", () => ({
946
workspace: {
1047
createFileSystemWatcher: vi.fn().mockReturnValue({
@@ -56,6 +93,7 @@ describe("McpHub", () => {
5693
// Mock console.error to suppress error messages during tests
5794
console.error = vi.fn()
5895

96+
5997
const mockUri: Uri = {
6098
scheme: "file",
6199
authority: "",

0 commit comments

Comments
 (0)