From 823eea3c73b2671628c622c9e0759a2f7eca04ea Mon Sep 17 00:00:00 2001 From: "Breno A." Date: Sun, 29 Sep 2024 03:46:59 -0300 Subject: [PATCH] fix: clear `core.summary` mock --- __tests__/comments.test.ts | 12 ++++-------- __tests__/parser.test.ts | 5 +++-- __tests__/utils.test.ts | 1 - 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/__tests__/comments.test.ts b/__tests__/comments.test.ts index d0bba61..9e9b8d6 100644 --- a/__tests__/comments.test.ts +++ b/__tests__/comments.test.ts @@ -1,13 +1,12 @@ import * as github from "@actions/github"; -import * as core from "@actions/core"; 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"; vi.mock("@actions/github"); vi.mock("@actions/core"); - describe("deleteOldComments", () => { const mockOctokit = { rest: { @@ -22,6 +21,7 @@ describe("deleteOldComments", () => { vi.clearAllMocks(); }); + it("should delete old comments with watermark", async () => { const comments = [ { id: 1, body: `${watermark} Old comment` }, @@ -82,15 +82,12 @@ describe("commentOnPullRequest", () => { beforeEach(() => { vi.clearAllMocks(); (github.getOctokit as Mock).mockReturnValue(mockOctokit); - (core.getInput as Mock).mockReturnValue("token"); github.context.payload = { pull_request: { number: 1 } }; - Object.defineProperty(github.context, "repo", { - value: { owner: "owner", repo: "repo" }, - writable: true - }); + 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({ @@ -111,7 +108,6 @@ 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( "Could not add a comment to pull request because github-token is missing!" ); diff --git a/__tests__/parser.test.ts b/__tests__/parser.test.ts index d09ca53..8d01ad0 100644 --- a/__tests__/parser.test.ts +++ b/__tests__/parser.test.ts @@ -1,11 +1,11 @@ import { parseAnalysisOutput, compareAnalysisResults } from "../src/helpers/parser.js"; -import * as core from "@actions/core"; import fs from "node:fs"; import { VMDAnalysis } from "../src/types.js"; import { describe, expect, it, vi, beforeEach, Mock } from "vitest"; +import * as core from "@actions/core"; -vi.mock("@actions/core"); vi.mock("node:fs"); +vi.spyOn(core, "setFailed"); describe("parseAnalysisOutput", () => { const resultPath = "vmd-analysis.json"; @@ -15,6 +15,7 @@ describe("parseAnalysisOutput", () => { vi.clearAllMocks(); }); + it("should parse analysis output successfully", () => { (fs.readFileSync as Mock).mockReturnValue(mockFileContent); diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index dfd8e25..7b551a5 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -6,7 +6,6 @@ import { describe, expect, it, Mock, vi } from "vitest"; vi.mock("@actions/core"); vi.mock("@actions/github"); - describe("isPullRequest", () => { it("should return true if the context is a pull request", () => { github.context.payload = { pull_request: {number: 1} };