diff --git a/jest.config.ts b/jest.config.ts index 2a31b155..509616a2 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -90,7 +90,7 @@ const config: Config = { reporters: ["default"], // Automatically reset mock state between every test - // resetMocks: false, + resetMocks: true, // Reset the module registry before running each individual test // resetModules: false, @@ -99,7 +99,7 @@ const config: Config = { // resolver: undefined, // Automatically restore mock state between every test - // restoreMocks: false, + restoreMocks: true, // The root directory that Jest should scan for tests and modules within rootDir: "./", diff --git a/src/commands/rustup.ts b/src/commands/rustup.ts index 0dc1ffdb..51ba663e 100644 --- a/src/commands/rustup.ts +++ b/src/commands/rustup.ts @@ -55,7 +55,6 @@ export class RustUp { switch (process.platform) { case "darwin": case "linux": { - // eslint-disable-line prettier/prettier const rustupSh = await tc.downloadTool("https://sh.rustup.rs"); // While the `rustup-init.sh` is properly executed as is, diff --git a/src/tests/commands/cargo.test.ts b/src/tests/commands/cargo.test.ts index 56f31330..89d44e30 100644 --- a/src/tests/commands/cargo.test.ts +++ b/src/tests/commands/cargo.test.ts @@ -11,10 +11,6 @@ jest.mock("@actions/exec"); jest.mock("@actions/cache"); describe("cargo", () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - it("Cargo", async () => { const spy = jest.spyOn(io, "which").mockResolvedValue("/home/user/.cargo/bin/cargo"); diff --git a/src/tests/commands/crates.test.ts b/src/tests/commands/crates.test.ts index f9b532a9..33945a91 100644 --- a/src/tests/commands/crates.test.ts +++ b/src/tests/commands/crates.test.ts @@ -8,10 +8,6 @@ import { type CratesIO } from "schema"; jest.mock("@actions/http-client"); describe("resolveVersion", () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - it("resolves", async () => { const version = "1.0.107"; diff --git a/src/tests/commands/cross.test.ts b/src/tests/commands/cross.test.ts index d425b67b..49211c68 100644 --- a/src/tests/commands/cross.test.ts +++ b/src/tests/commands/cross.test.ts @@ -6,10 +6,6 @@ import { Cross } from "core"; jest.mock("@actions/exec"); describe("cross", () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - it("Cross", async () => { const spy = jest.spyOn(io, "which").mockResolvedValue("/home/user/.cargo/bin/cross"); diff --git a/src/tests/commands/rustup.test.ts b/src/tests/commands/rustup.test.ts index 33117614..f91ec747 100644 --- a/src/tests/commands/rustup.test.ts +++ b/src/tests/commands/rustup.test.ts @@ -5,8 +5,18 @@ import { RustUp } from "core"; jest.mock("@actions/io"); describe("rustup", () => { + const originalPlatform = process.platform; + beforeEach(() => { - jest.resetAllMocks(); + Object.defineProperty(process, "platform", { + value: "linux", + }); + }); + + afterEach(function () { + Object.defineProperty(process, "platform", { + value: originalPlatform, + }); }); it("get", async () => { @@ -16,4 +26,56 @@ describe("rustup", () => { expect(spy).toHaveBeenCalledTimes(1); }); + + // it("getOrInstall", async () => { + // const spy = jest.spyOn(io, "which").mockResolvedValue("/home/user/.cargo/bin/rustup"); + + // await expect(RustUp.getOrInstall()).resolves.toEqual({ path: "/home/user/.cargo/bin/rustup" }); + + // expect(spy).toHaveBeenCalledTimes(1); + // }); + + it("getOrInstall install", async () => { + // prepare instance to return after installation + const prepared = jest.spyOn(io, "which").mockResolvedValueOnce("/home/user/.cargo/bin/rustup"); + const rustup = await RustUp.get(); + prepared.mockClear(); + + // actual test + const spy1 = jest.spyOn(io, "which").mockRejectedValue(new Error("Could not find path to rustup")); + const spy2 = jest.spyOn(RustUp, "install").mockResolvedValue(rustup); + + await expect(RustUp.getOrInstall()).resolves.toEqual({ path: "/home/user/.cargo/bin/rustup" }); + + expect(spy1).toHaveBeenCalledTimes(1); + expect(spy2).toHaveBeenCalledTimes(1); + }); + + it("install", async () => { + Object.defineProperty(process, "platform", { + value: "sunos", + }); + + expect.assertions(1); + + // await expect(RustUp.install()).rejects.toThrow(/Unknown platform/); + try { + const r = await RustUp.install(); + + console.log(r); + } catch (error: unknown) { + expect((error as Error)?.message).toMatch(/Unknown platform/); + } + }); + + async function test(t: boolean): Promise { + if (t) { + throw new Error("OMG WTF BBQ"); + } + return await Promise.resolve("foo"); + } + + it("throws?", async () => { + await expect(test(true)).rejects.toThrowError(/OMG/); + }); });