Skip to content

Commit

Permalink
refactor(tests): switch to jest.unstable_mockModule in comment.test.ts
Browse files Browse the repository at this point in the history
Refactor tests to use jest.unstable_mockModule for mocking dependencies.
  • Loading branch information
gentlementlegen committed Nov 2, 2024
1 parent a8e063f commit 11529fb
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions tests/comment.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { RestEndpointMethodTypes } from "@octokit/rest";
import { Logs } from "@ubiquity-os/ubiquity-os-logger";
import { collectLinkedPullRequests } from "../src/helpers/collect-linked-pulls";
import { parseIssueUrl } from "../src/helpers/github-url";
import { remindAssigneesForIssue } from "../src/helpers/remind-and-remove";
import { createStructuredMetadata } from "../src/helpers/structured-metadata";
import { ListIssueForRepo } from "../src/types/github-types";
import { ContextPlugin } from "../src/types/plugin-input";

jest.mock("../src/helpers/collect-linked-pulls");
jest.mock("../src/helpers/github-url");
jest.mock("../src/helpers/structured-metadata");
jest.unstable_mockModule("../src/helpers/collect-linked-pulls", () => []);
jest.unstable_mockModule("../src/helpers/github-url", () => ({
parseIssueUrl: jest.fn(() => ({
repo: "repo",
owner: "owner",
issue_number: 1,
})),
}));
jest.unstable_mockModule("../src/helpers/structured-metadata", () => ({
createStructuredMetadata: jest.fn(() => ""),
}));

describe("remindAssigneesForIssue", () => {
let context: ContextPlugin;
Expand Down Expand Up @@ -36,25 +42,33 @@ describe("remindAssigneesForIssue", () => {
html_url: "https://github.com/owner/repo/issues/1",
assignees: [{ login: "ubiquity-os", id: 1 }],
} as unknown as ListIssueForRepo;

(parseIssueUrl as jest.Mock).mockReturnValue({
repo: "repo",
owner: "owner",
issue_number: 1,
});

(collectLinkedPullRequests as jest.Mock).mockResolvedValue([]);
(createStructuredMetadata as jest.Mock).mockReturnValue({});
});

it("should post a comment to the parent issue if posting to the pull request fails", async () => {
context.config.pullRequestRequired = true;
(collectLinkedPullRequests as jest.Mock).mockResolvedValue([{ url: "https://github.com/owner/repo/pull/1" }]);
jest.unstable_mockModule("../src/helpers/collect-linked-pulls", () => {
return {
collectLinkedPullRequests: jest.fn(() => [
{
url: "https://github.com/owner/repo/pull/1",
body: "",
id: "1",
login: "ubiquity-os",
number: 1,
state: "OPEN",
title: "title",
},
]),
};
});

const mockedError = new Error("Failed to post comment");

(context.octokit.rest.issues.createComment as unknown as jest.Mock).mockRejectedValueOnce(mockedError).mockResolvedValueOnce({});
(context.octokit.rest.issues.createComment as jest.MockedFunction<typeof context.octokit.rest.issues.createComment>)
.mockRejectedValueOnce(mockedError)
.mockResolvedValueOnce({} as unknown as RestEndpointMethodTypes["issues"]["createComment"]["response"]);

const { remindAssigneesForIssue } = await import("../src/helpers/remind-and-remove");
await remindAssigneesForIssue(context, issue);

expect(context.octokit.rest.issues.createComment).toHaveBeenCalledWith(
Expand Down

0 comments on commit 11529fb

Please sign in to comment.