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 939d9f9 commit 823eea3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
12 changes: 4 additions & 8 deletions __tests__/comments.test.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -22,6 +21,7 @@ describe("deleteOldComments", () => {
vi.clearAllMocks();
});


it("should delete old comments with watermark", async () => {
const comments = [
{ id: 1, body: `${watermark} Old comment` },
Expand Down Expand Up @@ -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({
Expand All @@ -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!"
);
Expand Down
5 changes: 3 additions & 2 deletions __tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -15,6 +15,7 @@ describe("parseAnalysisOutput", () => {
vi.clearAllMocks();
});


it("should parse analysis output successfully", () => {
(fs.readFileSync as Mock).mockReturnValue(mockFileContent);

Expand Down
1 change: 0 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} };
Expand Down

0 comments on commit 823eea3

Please sign in to comment.