Skip to content

Commit

Permalink
Stubbing octokit in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Jun 21, 2024
1 parent ca75018 commit 69ca623
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 17 deletions.
19 changes: 18 additions & 1 deletion __tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import * as nock from "nock";
import core from "@actions/core";
import * as github from "@actions/github";
import type { Octokit } from "@octokit/core";
import type { PaginateInterface } from "@octokit/plugin-paginate-rest";
import type { Api } from "@octokit/plugin-rest-endpoint-methods/dist-types/types";
import nock from "nock";
import nodeFetch from "node-fetch";

nock.disableNetConnect();

type GitHub = Api & Octokit & { paginate: PaginateInterface };

jest.mock("../src/octokit", () => ({
octokit: (): GitHub =>
github.getOctokit(core.getInput("repo-token"), {
request: {
fetch: nodeFetch,
},
}),
}));
11 changes: 10 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ export default {
coverageDirectory: "coverage",
coverageProvider: "babel",
setupFiles: ["<rootDir>/__tests__/setup.ts"],
preset: "ts-jest/presets/default",
transform: {
// eslint-disable-next-line @typescript-eslint/naming-convention -- The key is a glob.
"^.+\\.[jt]s$": [
"ts-jest",
{
tsconfig: "<rootDir>/test.tsconfig.json",
},
],
},
transformIgnorePatterns: ["node_modules/(?!node-fetch)/"],
resetMocks: true,
testMatch: ["<rootDir>/__tests__/**/*.test.ts"],
};
112 changes: 97 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"jest": "^29.7.0",
"mocked-env": "^1.3.5",
"nock": "^13.5.4",
"node-fetch": "^3.3.2",
"prettier": "^3.3.2",
"rimraf": "^5.0.7",
"ts-jest": "^29.1.5",
Expand Down
6 changes: 6 additions & 0 deletions test.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"allowJs": true
}
}

0 comments on commit 69ca623

Please sign in to comment.