diff --git a/extensions/vscode/src/util/util.vitest.ts b/extensions/vscode/src/util/util.vitest.ts index 246b81087..b63622c1f 100644 --- a/extensions/vscode/src/util/util.vitest.ts +++ b/extensions/vscode/src/util/util.vitest.ts @@ -1,31 +1,30 @@ -import { afterEach, describe, expect, it, test, vi } from "vitest"; -import * as vscode from "vscode"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +vi.mock("node:os", () => ({ + platform: vi.fn(), + arch: vi.fn(), +})); -// Mock the vscode module vi.mock("vscode", () => ({ extensions: { getExtension: vi.fn(), }, })); -// Import the function after mocking -import { isExtensionPrerelease } from "./util"; - describe("isUnsupportedPlatform", () => { + beforeEach(() => { + vi.resetModules(); + }); + afterEach(() => { vi.resetAllMocks(); }); it("should be true when os is winarm64", async () => { - vi.doMock("os", () => ({ - platform: vi.fn().mockReturnValue("win32"), - arch: vi.fn().mockReturnValue("arm64"), - })); + const { platform, arch } = await import("node:os"); + vi.mocked(platform).mockReturnValue("win32"); + vi.mocked(arch).mockReturnValue("arm64"); - // "vscode" import should only be mocked during runtime - vi.doMock("vscode", () => ({})); - - // import after mocks are done const { isUnsupportedPlatform } = await import("./util"); const platformCheck = isUnsupportedPlatform(); @@ -35,52 +34,46 @@ describe("isUnsupportedPlatform", () => { expect(platformCheck.reason).toMatch(/arm64/gi); }); - it("should be true when os is unsupported", async () => { - vi.doMock("os", () => ({ - platform: vi.fn().mockReturnValue("bsd"), - arch: vi.fn().mockReturnValue("x64"), - })); - - // "vscode" import should only be mocked during runtime - vi.doMock("vscode", () => ({})); - - // import after mocks are done - const { isUnsupportedPlatform } = await import("./util"); - - const platformCheck = isUnsupportedPlatform(); - - expect(platformCheck.isUnsupported).toBe(true); - expect(platformCheck.reason).toMatch(/unsupported/gi); - }); - it("should not be true when os is supported", async () => { - vi.doMock("os", () => ({ - platform: vi.fn().mockReturnValue("linux"), - arch: vi.fn().mockReturnValue("arm64"), - })); + const { platform, arch } = await import("node:os"); + vi.mocked(platform).mockReturnValue("linux"); + vi.mocked(arch).mockReturnValue("arm64"); - // "vscode" import should only be mocked during runtime - vi.doMock("vscode", () => ({})); - - // import after mocks are done const { isUnsupportedPlatform } = await import("./util"); const platformCheck = isUnsupportedPlatform(); - expect(platformCheck.isUnsupported).toBe(true); + expect(platformCheck.isUnsupported).toBe(false); }); }); -test("isExtensionPrerelease detects prerelease versions correctly", () => { - // 1.0.0 is not prerelease (even minor version) - vi.mocked(vscode.extensions.getExtension).mockReturnValue({ - packageJSON: { version: "1.0.0" }, - } as any); - expect(isExtensionPrerelease()).toBe(false); +describe("isExtensionPrerelease", () => { + beforeEach(() => { + vi.resetModules(); + }); - // 1.1.0 is prerelease (odd minor version) - vi.mocked(vscode.extensions.getExtension).mockReturnValue({ - packageJSON: { version: "1.1.0" }, - } as any); - expect(isExtensionPrerelease()).toBe(true); + afterEach(() => { + vi.resetAllMocks(); + }); + + it("detects prerelease versions correctly", async () => { + const vscode = await import("vscode"); + const getExtensionMock = vi.mocked(vscode.extensions.getExtension); + + // 1.0.0 is not prerelease (even minor version) + getExtensionMock.mockReturnValue({ + packageJSON: { version: "1.0.0" }, + } as any); + + const { isExtensionPrerelease } = await import("./util"); + + expect(isExtensionPrerelease()).toBe(false); + + // 1.1.0 is prerelease (odd minor version) + getExtensionMock.mockReturnValue({ + packageJSON: { version: "1.1.0" }, + } as any); + + expect(isExtensionPrerelease()).toBe(true); + }); });