Skip to content

Commit c6ac4fb

Browse files
chore: a bit more unit testing (#684)
## PR Checklist - [x] Addresses an existing open issue: fixes #471 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Gets through the rest of the relatively straightforward unit tests. At this point I think we can consider #471 to be resolved. Unit test coverage is >89%.
1 parent 951790a commit c6ac4fb

10 files changed

+108
-27
lines changed

src/initialize/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { outro } from "../shared/cli/outro.js";
2-
import { getGitDefaultSettings } from "../shared/getDefaultSettings.js";
2+
import { getGitDefaultSettings } from "../shared/getGitDefaultSettings.js";
33
import { readInputs } from "../shared/inputs.js";
44
import { runOrRestore } from "../shared/runOrRestore.js";
55
import { initializeWithValues } from "./initializeWithValues.js";

src/migrate/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { augmentValuesWithNpmInfo } from "../shared/augmentValuesWithNpmInfo.js";
22
import { outro } from "../shared/cli/outro.js";
3-
import { getGitDefaultSettings } from "../shared/getDefaultSettings.js";
3+
import { getGitDefaultSettings } from "../shared/getGitDefaultSettings.js";
44
import { readInputs } from "../shared/inputs.js";
55
import { runOrRestore } from "../shared/runOrRestore.js";
66
import { migrateWithValues } from "./migrateWithValues.js";

src/shared/getDefaultSettings.test.ts renamed to src/shared/getGitDefaultSettings.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { describe, expect, it, vi } from "vitest";
22

3-
import { getGitDefaultSettings } from "./getDefaultSettings.js";
4-
5-
vi.mock("./cli/lines.js");
3+
import { getGitDefaultSettings } from "./getGitDefaultSettings.js";
64

75
const mockGitRemoteOriginUrl = vi.fn();
86

@@ -12,17 +10,19 @@ vi.mock("git-remote-origin-url", () => ({
1210
},
1311
}));
1412

13+
vi.mock("../shared/cli/lines.js");
14+
1515
describe("getDefaultSettings", () => {
1616
it("returns the retrieved owner and repository when gitRemoteOriginUrl succeeds", async () => {
17-
mockGitRemoteOriginUrl.mockResolvedValue("https://github.com/abc/def");
17+
mockGitRemoteOriginUrl.mockResolvedValueOnce("https://github.com/abc/def");
1818

1919
const settings = await getGitDefaultSettings();
2020

2121
expect(settings).toEqual({ owner: "abc", repository: "def" });
2222
});
2323

2424
it("returns arbitrary owner and repository defaults when gitRemoteOriginUrl rejects", async () => {
25-
mockGitRemoteOriginUrl.mockRejectedValue(new Error("Oh no!"));
25+
mockGitRemoteOriginUrl.mockRejectedValueOnce(new Error("Oh no!"));
2626

2727
const settings = await getGitDefaultSettings();
2828

src/shared/getOctokit.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Octokit } from "octokit";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
import { getOctokit } from "./getOctokit.js";
5+
6+
const mock$ = vi.fn();
7+
8+
vi.mock("execa", () => ({
9+
get $() {
10+
return mock$;
11+
},
12+
}));
13+
14+
vi.mock("octokit");
15+
16+
describe("getOctokit", () => {
17+
it("throws an error when gh auth status fails", async () => {
18+
mock$.mockRejectedValueOnce(new Error("Oh no!"));
19+
20+
await expect(getOctokit).rejects.toMatchInlineSnapshot(
21+
"[Error: GitHub authentication failed.]",
22+
);
23+
});
24+
25+
it("returns a new Octokit when gh auth status succeeds", async () => {
26+
const auth = "abc123";
27+
mock$.mockResolvedValueOnce({}).mockResolvedValueOnce({ stdout: auth });
28+
29+
const actual = await getOctokit();
30+
31+
expect(actual).toEqual(new Octokit({ auth }));
32+
});
33+
});

src/shared/getOctokit.ts

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
import chalk from "chalk";
21
import { $ } from "execa";
32
import { Octokit } from "octokit";
43

5-
import { runOrSkip } from "./cli/runOrSkip.js";
4+
export async function getOctokit(): Promise<Octokit | undefined> {
5+
try {
6+
await $`gh auth status`;
7+
} catch (error) {
8+
throw new Error("GitHub authentication failed.", {
9+
cause: (error as Error).message,
10+
});
11+
}
612

7-
export async function getOctokit(
8-
skipApi: boolean,
9-
): Promise<Octokit | undefined> {
10-
return runOrSkip("Checking GitHub authentication", skipApi, async () => {
11-
try {
12-
await $`gh auth status`;
13-
} catch (error) {
14-
console.error();
15-
console.error(chalk.red((error as Error).message));
16-
console.error();
17-
throw new Error("GitHub authentication failed.");
18-
}
13+
const auth = (await $`gh auth token`).stdout.trim();
1914

20-
const auth = (await $`gh auth token`).stdout.trim();
21-
22-
return new Octokit({ auth });
23-
});
15+
return new Octokit({ auth });
2416
}

src/shared/inputs.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Octokit } from "octokit";
33
import { titleCase } from "title-case";
44

55
import { allArgOptions } from "./args.js";
6+
import { runOrSkip } from "./cli/runOrSkip.js";
67
import { ensureRepositoryExists } from "./ensureRepositoryExists.js";
78
import { getOctokit } from "./getOctokit.js";
89
import { getPrefillOrPromptedValue } from "./getPrefillOrPromptedValue.js";
@@ -71,7 +72,11 @@ export async function readInputs({
7172
"What owner or user will the repository be under?",
7273
))));
7374

74-
const octokit = await getOctokit(!!values["skip-github-api"]);
75+
const octokit = await runOrSkip(
76+
"Checking GitHub authentication",
77+
!!values["skip-github-api"],
78+
getOctokit,
79+
);
7580

7681
const repository =
7782
(values.repository as string | undefined) ??
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { detectExistingContributors } from "./detectExistingContributors.js";
4+
5+
const mockGetAllContributorsForRepository = vi.fn();
6+
7+
vi.mock("all-contributors-for-repository", () => ({
8+
get getAllContributorsForRepository() {
9+
return mockGetAllContributorsForRepository;
10+
},
11+
}));
12+
13+
const mock$ = vi.fn();
14+
15+
vi.mock("execa", () => ({
16+
get $() {
17+
return mock$;
18+
},
19+
}));
20+
21+
describe("detectExistingContributors", () => {
22+
it("runs npx all-contributors add for each contributor and contribution type", async () => {
23+
mockGetAllContributorsForRepository.mockResolvedValue({
24+
username: ["bug", "docs"],
25+
});
26+
27+
await detectExistingContributors();
28+
29+
expect(mock$.mock.calls).toMatchInlineSnapshot(`
30+
[
31+
[
32+
[
33+
"npx all-contributors add ",
34+
" ",
35+
"",
36+
],
37+
"username",
38+
"0,1",
39+
],
40+
]
41+
`);
42+
});
43+
});

src/steps/initializeBranchProtectionSettings.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ const createMockOctokit = (request: SpyInstance) =>
1111
const stubValues = { owner: "", repository: "" };
1212

1313
describe("migrateBranchProtectionSettings", () => {
14+
it("does not throw when the request receives a non-error response", async () => {
15+
const mockRequest = vi.fn().mockResolvedValue({ status: 200 });
16+
17+
await initializeBranchProtectionSettings(
18+
createMockOctokit(mockRequest),
19+
stubValues,
20+
);
21+
});
22+
1423
it("returns false when the request receives a 403 response", async () => {
1524
const mockRequest = vi.fn().mockRejectedValue({ status: 403 });
1625

src/steps/initializeRepositorySettings.ts

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export async function initializeRepositorySettings(
1111
octokit: Octokit,
1212
{ description, owner, repository }: MigrateRepositoryValues,
1313
) {
14-
console.log("Will update repo settings");
1514
await octokit.rest.repos.update({
1615
allow_auto_merge: true,
1716
allow_rebase_merge: false,

0 commit comments

Comments
 (0)