diff --git a/.github/linters/tsconfig.json b/.github/linters/tsconfig.json index a34cf90..4a2af0c 100644 --- a/.github/linters/tsconfig.json +++ b/.github/linters/tsconfig.json @@ -4,6 +4,6 @@ "compilerOptions": { "noEmit": true }, - "include": ["../../__tests__/**/*", "../../src/**/*"], + "include": ["../../__tests__/**/*", "../../__mocks__/**/*", "../../src/**/*"], "exclude": ["../../dist", "../../node_modules", "../../coverage", "*.json"] } diff --git a/__mocks__/@actions/github.ts b/__mocks__/@actions/github.ts new file mode 100644 index 0000000..39618cc --- /dev/null +++ b/__mocks__/@actions/github.ts @@ -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, + }, + }, +}; diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index fe8df29..f7c37eb 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -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; +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; describe("parseConfig", () => { it("parses the config file", async () => { diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index b953be7..b64ca1c 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -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"); diff --git a/tsconfig.json b/tsconfig.json index 2f26aac..877d8f1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,5 +15,11 @@ "skipLibCheck": true, "newLine": "lf" }, - "exclude": ["./dist", "./node_modules", "./__tests__", "./coverage"] + "exclude": [ + "./dist", + "./node_modules", + "./__tests__", + "./__mocks__", + "./coverage" + ] }