Skip to content

Commit 4f233b9

Browse files
committed
refactor(test): improve test mocks and fs utility
- Ensure `safeReadFile` handles empty content gracefully to prevent potential errors. - Update Jest mocks for `fs` utility functions in tests to correctly spread original exports, improving test reliability. - Add comprehensive `globalState` mocking in `migrateSettings` tests for better test coverage.
1 parent bf6cc42 commit 4f233b9

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

src/__tests__/migrateSettings.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ describe("Settings Migration", () => {
4848
// Mock extension context
4949
mockContext = {
5050
globalStorageUri: { fsPath: mockStoragePath },
51+
globalState: {
52+
get: jest.fn((key: string) => {
53+
if (key === "clineCustomModes") {
54+
return "some old custom modes content"
55+
}
56+
if (key === "customInstructions") {
57+
return undefined
58+
}
59+
return undefined
60+
}),
61+
update: jest.fn().mockResolvedValue(undefined),
62+
},
5163
} as unknown as vscode.ExtensionContext
5264

5365
// Set global outputChannel for all tests

src/core/prompts/__tests__/custom-system-prompt.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const mockedFs = fs as jest.Mocked<typeof fs>
1616

1717
// Mock the fileExistsAtPath function
1818
jest.mock("../../../utils/fs", () => ({
19+
...(jest.requireActual("../../../utils/fs") as object), // Spread all original exports
1920
fileExistsAtPath: jest.fn().mockResolvedValue(true),
2021
createDirectoriesForFile: jest.fn().mockResolvedValue([]),
2122
}))

src/core/task/__tests__/Task.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ jest.mock("../../../utils/storage", () => ({
140140
}))
141141

142142
jest.mock("../../../utils/fs", () => ({
143+
...(jest.requireActual("../../../utils/fs") as object), // Spread all original exports
143144
fileExistsAtPath: jest.fn().mockImplementation((filePath) => {
144145
return filePath.includes("ui_messages.json") || filePath.includes("api_conversation_history.json")
145146
}),

src/utils/fs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function fileExistsAtPath(filePath: string): Promise<boolean> {
5252
export async function safeReadFile(filePath: string): Promise<string> {
5353
try {
5454
const content = await fs.readFile(filePath, "utf-8")
55-
return content.trim()
55+
return content ? content.trim() : ""
5656
} catch (err) {
5757
const errorCode = (err as NodeJS.ErrnoException).code
5858
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {

0 commit comments

Comments
 (0)