Skip to content

Commit 18c4455

Browse files
committed
refactor: declare path mock at the top of the module
1 parent be4ef5e commit 18c4455

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/integrations/diagnostics/__tests__/diagnostics.test.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import * as vscode from "vscode"
22
import { diagnosticsToProblemsString } from ".."
33

4+
// Mock path module
5+
jest.mock("path", () => ({
6+
relative: jest.fn((cwd, fullPath) => {
7+
// Handle the specific case already present
8+
if (cwd === "/project/root" && fullPath === "/project/root/src/utils/file.ts") {
9+
return "src/utils/file.ts"
10+
}
11+
// Handle the test cases with /path/to as cwd
12+
if (cwd === "/path/to") {
13+
// Simple relative path calculation for the test cases
14+
return fullPath.replace(cwd + "/", "")
15+
}
16+
// Fallback for other cases (can be adjusted if needed)
17+
return fullPath
18+
}),
19+
}))
20+
421
// Mock vscode module
522
jest.mock("vscode", () => ({
623
Uri: {
@@ -130,7 +147,7 @@ describe("diagnosticsToProblemsString", () => {
130147
// Verify the output contains the expected directory indicator
131148
expect(result).toContain("(directory)")
132149
expect(result).toContain("Directory diagnostic message")
133-
expect(result).toMatch(/directory\n- \[test Error\] 1 \| \(directory\) : Directory diagnostic message/)
150+
expect(result).toMatch(/directory\/\n- \[test Error\] 1 \| \(directory\) : Directory diagnostic message/)
134151
})
135152

136153
it("should correctly handle multiple diagnostics for the same file", async () => {
@@ -340,16 +357,6 @@ describe("diagnosticsToProblemsString", () => {
340357
}
341358
vscode.workspace.openTextDocument = jest.fn().mockResolvedValue(mockDocument)
342359

343-
// Mock path.relative to return the expected relative path
344-
jest.mock("path", () => ({
345-
relative: jest.fn((cwd, fullPath) => {
346-
if (cwd === "/project/root" && fullPath === "/project/root/src/utils/file.ts") {
347-
return "src/utils/file.ts"
348-
}
349-
return fullPath
350-
}),
351-
}))
352-
353360
// Call the function with cwd set to the project root
354361
const result = await diagnosticsToProblemsString(
355362
[[fileUri, [diagnostic]]],

0 commit comments

Comments
 (0)