From 15e5a9ebd1b13d29be2e9389bc247d98b5244399 Mon Sep 17 00:00:00 2001 From: "Breno A." Date: Sun, 29 Sep 2024 04:13:50 -0300 Subject: [PATCH] fix: clear `core.summary` mock --- __tests__/comments.test.ts | 93 ++------------------------------------ __tests__/package.test.ts | 1 - __tests__/utils.test.ts | 47 +------------------ src/github/comments.ts | 8 ++-- 4 files changed, 10 insertions(+), 139 deletions(-) diff --git a/__tests__/comments.test.ts b/__tests__/comments.test.ts index 9e9b8d6..54bba17 100644 --- a/__tests__/comments.test.ts +++ b/__tests__/comments.test.ts @@ -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, - "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, - "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 = {}; @@ -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!" ); }); diff --git a/__tests__/package.test.ts b/__tests__/package.test.ts index 5e9c8c5..648c9e8 100644 --- a/__tests__/package.test.ts +++ b/__tests__/package.test.ts @@ -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"); diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 7b551a5..1d9be71 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -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", () => { @@ -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); - }); -}); diff --git a/src/github/comments.ts b/src/github/comments.ts index 4996414..67ce48d 100644 --- a/src/github/comments.ts +++ b/src/github/comments.ts @@ -28,18 +28,20 @@ export async function deleteOldComments( } } -export async function commentOnPullRequest(commentBody: string): Promise { +export async function commentOnPullRequest( + commentBody: string, + token: string = core.getInput("github-token") +): Promise { 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 = github.getOctokit(token); const { owner, repo } = github.context.repo; const pull_number: number = github.context.payload.pull_request.number;