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 <roo-code@z.ewheeler.org>
This commit is contained in:
Eric Wheeler 2025-06-02 19:59:56 -07:00 committed by Daniel Riccio
parent ca0afb2fbb
commit dd5410803a

View file

@ -5,6 +5,43 @@ import { ServerConfigSchema, McpHub } from "../McpHub"
import fs from "fs/promises"
import { vi, Mock } from "vitest"
// Mock fs/promises before importing anything that uses it
vi.mock("fs/promises", () => ({
default: {
access: vi.fn().mockResolvedValue(undefined),
writeFile: vi.fn().mockResolvedValue(undefined),
readFile: vi.fn().mockResolvedValue("{}"),
unlink: vi.fn().mockResolvedValue(undefined),
rename: vi.fn().mockResolvedValue(undefined),
lstat: vi.fn().mockImplementation(() =>
Promise.resolve({
isDirectory: () => true,
}),
),
mkdir: vi.fn().mockResolvedValue(undefined),
},
access: vi.fn().mockResolvedValue(undefined),
writeFile: vi.fn().mockResolvedValue(undefined),
readFile: vi.fn().mockResolvedValue("{}"),
unlink: vi.fn().mockResolvedValue(undefined),
rename: vi.fn().mockResolvedValue(undefined),
lstat: vi.fn().mockImplementation(() =>
Promise.resolve({
isDirectory: () => true,
}),
),
mkdir: vi.fn().mockResolvedValue(undefined),
}))
// Mock safeWriteJson
vi.mock("../../../utils/safeWriteJson", () => ({
safeWriteJson: vi.fn(async (filePath, data) => {
// Instead of trying to write to the file system, just call fs.writeFile mock
// This avoids the complex file locking and temp file operations
return fs.writeFile(filePath, JSON.stringify(data), "utf8")
}),
}))
vi.mock("vscode", () => ({
workspace: {
createFileSystemWatcher: vi.fn().mockReturnValue({
@ -56,6 +93,7 @@ describe("McpHub", () => {
// Mock console.error to suppress error messages during tests
console.error = vi.fn()
const mockUri: Uri = {
scheme: "file",
authority: "",