Skip to content

Commit

Permalink
fix: clear core.summary mock
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Sep 29, 2024
1 parent f627f09 commit 15e5a9e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 139 deletions.
93 changes: 3 additions & 90 deletions __tests__/comments.test.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,17 @@
import * as github from "@actions/github";
import { GitHub } from "@actions/github/lib/utils.js";
import { commentOnPullRequest, deleteOldComments } from "../src/github/comments.js";
import { watermark } from "../src/templates/commentTemplate.js";
import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
import * as core from "@actions/core";
import { commentOnPullRequest } from "../src/github/comments.js";
import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("@actions/github");
vi.mock("@actions/core");
describe("deleteOldComments", () => {
const mockOctokit = {
rest: {
issues: {
listComments: vi.fn(),
deleteComment: vi.fn()
}
}
};

beforeEach(() => {
vi.clearAllMocks();
});


it("should delete old comments with watermark", async () => {
const comments = [
{ id: 1, body: `${watermark} Old comment` },
{ id: 2, body: "Some other comment" }
];
(mockOctokit.rest.issues.listComments as Mock).mockResolvedValue({
data: comments
});

await deleteOldComments(
mockOctokit as unknown as InstanceType<typeof GitHub>,
"owner",
"repo",
1
);

expect(mockOctokit.rest.issues.deleteComment).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
comment_id: 1
});
expect(mockOctokit.rest.issues.deleteComment).not.toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
comment_id: 2
});
});

it("should not delete comments without watermark", async () => {
const comments = [
{ id: 1, body: "Some other comment" },
{ id: 2, body: "Another comment" }
];
(mockOctokit.rest.issues.listComments as Mock).mockResolvedValue({
data: comments
});

await deleteOldComments(
mockOctokit as unknown as InstanceType<typeof GitHub>,
"owner",
"repo",
1
);

expect(mockOctokit.rest.issues.deleteComment).not.toHaveBeenCalled();
});
});

describe("commentOnPullRequest", () => {
const mockOctokit = {
rest: {
issues: {
createComment: vi.fn()
}
}
};

beforeEach(() => {
vi.clearAllMocks();
(github.getOctokit as Mock).mockReturnValue(mockOctokit);
github.context.payload = { pull_request: { number: 1 } };
process.env.GITHUB_REPOSITORY = "owner/repo";
});

it("should create a comment on pull request", async () => {
(core.getInput as Mock).mockReturnValue("token");
await commentOnPullRequest("Test comment");

expect(mockOctokit.rest.issues.createComment).toHaveBeenCalledWith({
owner: "owner",
repo: "repo",
issue_number: 1,
body: "Test comment"
});
});

it("should throw an error if no pull request found", async () => {
github.context.payload = {};

Expand All @@ -107,8 +21,7 @@ describe("commentOnPullRequest", () => {
});

it("should throw an error if github-token is missing", async () => {
(core.getInput as Mock).mockReturnValue("");
await expect(commentOnPullRequest("Test comment")).rejects.toThrow(
await expect(commentOnPullRequest("Test comment", "")).rejects.toThrow(
"Could not add a comment to pull request because github-token is missing!"
);
});
Expand Down
1 change: 0 additions & 1 deletion __tests__/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fs from "node:fs";
import path from "node:path";
import { describe, expect, it, Mock, vi, beforeEach } from "vitest";

vi.mock("@actions/core");
vi.mock("execa");
vi.mock("node:fs");
vi.mock("node:path");
Expand Down
47 changes: 2 additions & 45 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { isPullRequest, getPath, readActionInputs, ActionInputs } from "../src/github/utils.js";
import * as core from "@actions/core";
import { isPullRequest, getPath, ActionInputs } from "../src/github/utils.js";
import * as github from "@actions/github";
import Path from "node:path";
import { describe, expect, it, Mock, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";

vi.mock("@actions/core");
vi.mock("@actions/github");
describe("isPullRequest", () => {
it("should return true if the context is a pull request", () => {
Expand Down Expand Up @@ -51,44 +49,3 @@ describe("getPath", () => {
});
});

describe("readActionInputs", () => {
it("should read and return action inputs", () => {
(core.getInput as Mock).mockImplementation((name: string) => {
const inputs: { [key: string]: string } = {
version: "latest",
skipInstall: "false",
packageManager: "npm",
runArgs: "",
entryPoint: "",
srcDir: "src",
commentsEnabled: "true",
skipBots: "false",
relativeMode: "false"
};
return inputs[name];
});

(core.getBooleanInput as Mock).mockImplementation((name: string) => {
const inputs: { [key: string]: boolean } = {
commentsEnabled: true,
skipBots: false,
relativeMode: false
};
return inputs[name];
});

const expectedInputs: ActionInputs = {
version: "latest",
skipInstall: false,
packageManager: "npm",
runArgs: "",
entryPoint: "",
srcDir: "src",
commentsEnabled: true,
skipBots: false,
relativeMode: false
};

expect(readActionInputs()).toEqual(expectedInputs);
});
});
8 changes: 5 additions & 3 deletions src/github/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ export async function deleteOldComments(
}
}

export async function commentOnPullRequest(commentBody: string): Promise<void> {
export async function commentOnPullRequest(
commentBody: string,
token: string = core.getInput("github-token")
): Promise<void> {
if (!github.context.payload.pull_request) {
throw new Error("No pull request found in the context!");
}

if (core.getInput("github-token") === "") {
if (token === "") {
throw new Error(
"Could not add a comment to pull request because github-token is missing!"
);
}

const token: string = core.getInput("github-token");
const octokit: InstanceType<typeof GitHub> = github.getOctokit(token);
const { owner, repo } = github.context.repo;
const pull_number: number = github.context.payload.pull_request.number;
Expand Down

0 comments on commit 15e5a9e

Please sign in to comment.