Skip to content

Commit

Permalink
refactor: create manual mock for @actions/github
Browse files Browse the repository at this point in the history
  • Loading branch information
moltinginstar committed Apr 8, 2024
1 parent b831cb6 commit 31149c6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .github/linters/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"compilerOptions": {
"noEmit": true
},
"include": ["../../__tests__/**/*", "../../src/**/*"],
"include": ["../../__tests__/**/*", "../../__mocks__/**/*", "../../src/**/*"],
"exclude": ["../../dist", "../../node_modules", "../../coverage", "*.json"]
}
76 changes: 76 additions & 0 deletions __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export const getOctokit = jest.fn(() => ({
rest: {
pulls: {
listFiles: jest.fn(async ({ page }) => {
if (page === 1) {
return Promise.resolve({
data: Array.from({ length: 100 }, (_, i) => ({
filename: `src/app1/file${i}.ts`,
})),
});
} else if (page === 2) {
return Promise.resolve({
data: Array.from({ length: 100 }, (_, i) => ({
filename: `src/app2/file${i}.ts`,
})),
});
} else if (page === 3) {
return Promise.resolve({
data: Array.from({ length: 85 }, (_, i) => ({
filename: `src/lib1/file${i}.ts`,
})),
});
} else {
return Promise.resolve({ data: [] });
}
}),

requestReviewers: jest.fn(),
},

issues: {
createComment: jest.fn(),
},

repos: {
getContent: jest.fn(async ({ path }) => {
if (path === ".github/codeowners.yaml") {
return Promise.resolve({
data: {
type: "file",
content: Buffer.from("src/**:\n - owner1").toString("base64"),
},
});
} else if (path === ".github/invalid.yaml") {
return Promise.resolve({
data: {
type: "file",
content: Buffer.from("[invalid yaml").toString("base64"),
},
});
} else {
return Promise.resolve({ data: [] });
}
}),
},
},
}));

export const context = {
sha: "sha",

repo: {
owner: "owner",
repo: "repo",
},

payload: {
sender: {
login: "prAuthor",
},

pull_request: {
number: 1,
},
},
};
81 changes: 2 additions & 79 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,9 @@ import * as core from "@actions/core";
import * as github from "../src/github";
import { getOctokit } from "@actions/github";

let debugMock: jest.SpiedFunction<typeof core.debug>;
jest.mock("@actions/github");

jest.mock("@actions/github", () => ({
getOctokit: jest.fn(() => ({
rest: {
pulls: {
listFiles: jest.fn(async ({ page }) => {
if (page === 1) {
return Promise.resolve({
data: Array.from({ length: 100 }, (_, i) => ({
filename: `src/app1/file${i}.ts`,
})),
});
} else if (page === 2) {
return Promise.resolve({
data: Array.from({ length: 100 }, (_, i) => ({
filename: `src/app2/file${i}.ts`,
})),
});
} else if (page === 3) {
return Promise.resolve({
data: Array.from({ length: 85 }, (_, i) => ({
filename: `src/lib1/file${i}.ts`,
})),
});
} else {
return Promise.resolve({ data: [] });
}
}),

requestReviewers: jest.fn(),
},

issues: {
createComment: jest.fn(),
},

repos: {
getContent: jest.fn(async ({ path }) => {
if (path === ".github/codeowners.yaml") {
return Promise.resolve({
data: {
type: "file",
content: Buffer.from("src/**:\n - owner1").toString("base64"),
},
});
} else if (path === ".github/invalid.yaml") {
return Promise.resolve({
data: {
type: "file",
content: Buffer.from("[invalid yaml").toString("base64"),
},
});
} else {
return Promise.resolve({ data: [] });
}
}),
},
},
})),

context: {
sha: "sha",

repo: {
owner: "owner",
repo: "repo",
},

payload: {
sender: {
login: "prAuthor",
},

pull_request: {
number: 1,
},
},
},
}));
let debugMock: jest.SpiedFunction<typeof core.debug>;

describe("parseConfig", () => {
it("parses the config file", async () => {
Expand Down
54 changes: 1 addition & 53 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,7 @@
import * as core from "@actions/core";
import * as main from "../src/main";

jest.mock("@actions/github", () => ({
getOctokit: jest.fn(() => ({
rest: {
pulls: {
listFiles: jest
.fn()
.mockResolvedValueOnce({
data: [
{ filename: "src/index.ts" },
{ filename: "src/main.ts" },
{ filename: "src/match.ts" },
{ filename: "README.md" },
],
})
.mockResolvedValue({ data: [] }),

requestReviewers: jest.fn(),
},

issues: {
createComment: jest.fn(),
},

repos: {
getContent: jest.fn().mockResolvedValue({
data: {
type: "file",
content: Buffer.from("src/**:\n - owner1").toString("base64"),
},
}),
},
},
})),

context: {
sha: "sha",

repo: {
owner: "owner",
repo: "repo",
},

payload: {
sender: {
login: "prAuthor",
},

pull_request: {
number: 1,
},
},
},
}));
jest.mock("@actions/github");

const runMock = jest.spyOn(main, "run");

Expand Down
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
"skipLibCheck": true,
"newLine": "lf"
},
"exclude": ["./dist", "./node_modules", "./__tests__", "./coverage"]
"exclude": [
"./dist",
"./node_modules",
"./__tests__",
"./__mocks__",
"./coverage"
]
}

0 comments on commit 31149c6

Please sign in to comment.