Skip to content

Commit

Permalink
fix: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kristof-mattei committed Sep 24, 2023
1 parent 84739ef commit 6708159
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: "./",
Expand Down
1 change: 0 additions & 1 deletion src/commands/rustup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions src/tests/commands/cargo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
4 changes: 0 additions & 4 deletions src/tests/commands/crates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
4 changes: 0 additions & 4 deletions src/tests/commands/cross.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
64 changes: 63 additions & 1 deletion src/tests/commands/rustup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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<string> {
if (t) {
throw new Error("OMG WTF BBQ");
}
return await Promise.resolve("foo");
}

it("throws?", async () => {
await expect(test(true)).rejects.toThrowError(/OMG/);
});
});

0 comments on commit 6708159

Please sign in to comment.