Skip to content

Commit

Permalink
test: add coverage for auto merge feature (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide authored Jan 9, 2025
1 parent 8700220 commit ca569c6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
28 changes: 22 additions & 6 deletions e2e/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ describe("e2e tests", () => {
);

// Pull request was created with the corrects params
expect(fakeGitHub.pullRequestHandler).toHaveBeenCalledTimes(1);
const request = fakeGitHub.pullRequestHandler.mock
expect(fakeGitHub.createPullRequestHandler).toHaveBeenCalledTimes(1);
const request = fakeGitHub.createPullRequestHandler.mock
.calls[0][0] as CompletedRequest;
expect(request.path).toEqual(
expect.stringMatching(/bazelbuild\/bazel-central-registry/)
Expand All @@ -491,6 +491,22 @@ describe("e2e tests", () => {
`https://github.com/${testOrg}/${repo}/releases/tag/${tag}`
)
);

// Pull request was updated to enable auto merge
expect(fakeGitHub.updatePullRequestHandler).toHaveBeenCalledTimes(1);
const updateRequest = fakeGitHub.updatePullRequestHandler.mock
.calls[0][0] as CompletedRequest;
expect(updateRequest.path).toEqual(
expect.stringMatching(
/repos\/bazelbuild\/bazel-central-registry\/pulls\/\d+/
)
);
const updateBody = (await updateRequest.body.getJson()) as any;
expect(updateBody).toEqual(
expect.objectContaining({
allow_auto_merge: true,
})
);
});

test("happy path with multiple modules", async () => {
Expand Down Expand Up @@ -536,8 +552,8 @@ describe("e2e tests", () => {
expect(messages.length).toEqual(0);

// One pull requests was created with the corrects params
expect(fakeGitHub.pullRequestHandler).toHaveBeenCalledTimes(1);
let request = fakeGitHub.pullRequestHandler.mock
expect(fakeGitHub.createPullRequestHandler).toHaveBeenCalledTimes(1);
let request = fakeGitHub.createPullRequestHandler.mock
.calls[0][0] as CompletedRequest;
expect(request.path).toEqual(
expect.stringMatching(/bazelbuild\/bazel-central-registry/)
Expand Down Expand Up @@ -644,8 +660,8 @@ describe("e2e tests", () => {
expect(response.status).toEqual(200);

// Pull request points to releaser's BCR fork
expect(fakeGitHub.pullRequestHandler).toHaveBeenCalled();
const request = fakeGitHub.pullRequestHandler.mock
expect(fakeGitHub.createPullRequestHandler).toHaveBeenCalled();
const request = fakeGitHub.createPullRequestHandler.mock
.calls[0][0] as CompletedRequest;
const body = (await request.body.getJson()) as any;
expect(body).toEqual(
Expand Down
29 changes: 24 additions & 5 deletions e2e/stubs/fake-github.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { User } from "@octokit/webhooks-types";
import { randomUUID } from "crypto";
import * as mockttp from "mockttp";
import { randomInt } from "node:crypto";
import url from "node:url";
import { StubbedServer } from "./stubbed-server";

Expand All @@ -18,7 +19,8 @@ export class FakeGitHub implements StubbedServer {
{ owner: string; repo: string; sourceOwner?: string; sourceRepo?: string }
>();

public readonly pullRequestHandler = jest.fn();
public readonly createPullRequestHandler = jest.fn();
public readonly updatePullRequestHandler = jest.fn();
public readonly installationTokenHandler = jest.fn();

public constructor() {
Expand All @@ -38,14 +40,16 @@ export class FakeGitHub implements StubbedServer {
this.setupGetOwnedReposHandler(),
this.setupGetRepoHandler(),
this.setupCreatePullHandler(),
this.setupUpdatePullHandler(),
this.setupAppHandler(),
]);
}

public async reset(): Promise<void> {
this.server.reset();
this.clearMockedData();
this.pullRequestHandler.mockReset();
this.createPullRequestHandler.mockReset();
this.updatePullRequestHandler.mockReset();
this.installationTokenHandler.mockReset();
await this.setupHandlers();
}
Expand Down Expand Up @@ -262,16 +266,31 @@ export class FakeGitHub implements StubbedServer {
}

private async setupCreatePullHandler(): Promise<void> {
this.pullRequestHandler.mockImplementation((request) => {
this.createPullRequestHandler.mockImplementation((request) => {
return {
statusCode: 201,
body: "{}",
json: {
number: randomInt(100),
},
};
});

await this.server
.forPost("/repos/bazelbuild/bazel-central-registry/pulls")
.thenCallback((request) => this.pullRequestHandler(request));
.thenCallback((request) => this.createPullRequestHandler(request));
}

private async setupUpdatePullHandler(): Promise<void> {
this.updatePullRequestHandler.mockImplementation((request) => {
return {
statusCode: 200,
json: {},
};
});

await this.server
.forPatch(/\/repos\/bazelbuild\/bazel-central-registry\/pulls\/\d+$/)
.thenCallback((request) => this.updatePullRequestHandler(request));
}

private async setupAppHandler(): Promise<void> {
Expand Down

0 comments on commit ca569c6

Please sign in to comment.