Skip to content

Commit 61e374f

Browse files
test: finish unit testing initialize and initializeWithOptions (#1631)
## PR Checklist - [x] Addresses an existing open issue: fixes #873 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview As the issue suggested, this was a fair bit of setup. But I feel better now. Also swaps out a `0` that was just coincidentally the same as the proper `StatusCodes.Success`. 💖
1 parent 79773ac commit 61e374f

File tree

3 files changed

+254
-7
lines changed

3 files changed

+254
-7
lines changed

src/initialize/index.test.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { describe, expect, it, vi } from "vitest";
22

33
import { StatusCodes } from "../shared/codes.js";
4+
import { RunOrRestoreOptions } from "../shared/runOrRestore.js";
45
import { initialize } from "./index.js";
56

67
const mockOutro = vi.fn();
78

8-
vi.mock("@clack/prompts", () => ({
9+
vi.mock("../shared/cli/outro.js", () => ({
910
get outro() {
1011
return mockOutro;
1112
},
12-
spinner: vi.fn(),
13+
}));
14+
15+
const mockEnsureGitRepository = vi.fn();
16+
17+
vi.mock("../shared/ensureGitRepository.js", () => ({
18+
get ensureGitRepository() {
19+
return mockEnsureGitRepository;
20+
},
1321
}));
1422

1523
const mockReadOptions = vi.fn();
@@ -20,11 +28,18 @@ vi.mock("../shared/options/readOptions.js", () => ({
2028
},
2129
}));
2230

23-
const mockInitializeAndEnterRepository = vi.fn();
31+
vi.mock("../shared/runOrRestore.js", () => ({
32+
async runOrRestore({ run }: RunOrRestoreOptions) {
33+
await run();
34+
return StatusCodes.Success;
35+
},
36+
}));
37+
38+
const mockInitializeWithOptions = vi.fn();
2439

25-
vi.mock("./initializeAndEnterRepository.js", () => ({
26-
get initializeAndEnterRepository() {
27-
return mockInitializeAndEnterRepository;
40+
vi.mock("./initializeWithOptions.js", () => ({
41+
get initializeWithOptions() {
42+
return mockInitializeWithOptions;
2843
},
2944
}));
3045

@@ -45,5 +60,49 @@ describe("initialize", () => {
4560
code: StatusCodes.Cancelled,
4661
options: optionsBase,
4762
});
63+
expect(mockEnsureGitRepository).not.toHaveBeenCalled();
64+
});
65+
66+
it("runs initializeWithOptions when readOptions returns inputs", async () => {
67+
mockReadOptions.mockResolvedValue({
68+
cancelled: false,
69+
options: optionsBase,
70+
});
71+
72+
const result = await initialize([]);
73+
74+
expect(result).toEqual({
75+
code: StatusCodes.Success,
76+
options: optionsBase,
77+
});
78+
expect(mockEnsureGitRepository).toHaveBeenCalled();
79+
expect(mockOutro.mock.calls).toMatchInlineSnapshot(`
80+
[
81+
[
82+
[
83+
{
84+
"label": "You may consider committing these changes:",
85+
"lines": [
86+
"git add -A",
87+
"git commit -m "feat: initialized repo ✨",
88+
"git push",
89+
],
90+
"variant": "code",
91+
},
92+
{
93+
"label": "Be sure to:",
94+
"lines": [
95+
"- enable the GitHub apps:
96+
- Codecov (https://github.com/apps/codecov)
97+
- Renovate (https://github.com/apps/renovate)",
98+
"- populate the secrets:
99+
- ACCESS_TOKEN (a GitHub PAT with repo and workflow permissions)
100+
- NPM_TOKEN (an npm access token with automation permissions)",
101+
],
102+
},
103+
],
104+
],
105+
]
106+
`);
48107
});
49108
});
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { GitHub } from "../shared/options/getGitHub.js";
4+
import { Options } from "../shared/types.js";
5+
import { initializeWithOptions } from "./initializeWithOptions.js";
6+
7+
vi.mock("../shared/cli/spinners.js", () => ({
8+
async withSpinner(_label: string, task: () => Promise<void>) {
9+
await task();
10+
},
11+
withSpinners: vi.fn(),
12+
}));
13+
14+
const mockAddOwnerAsAllContributor = vi.fn();
15+
16+
vi.mock("../steps/addOwnerAsAllContributor.js", () => ({
17+
get addOwnerAsAllContributor() {
18+
return mockAddOwnerAsAllContributor;
19+
},
20+
}));
21+
22+
const mockClearChangelog = vi.fn();
23+
24+
vi.mock("../steps/clearChangelog.js", () => ({
25+
get clearChangelog() {
26+
return mockClearChangelog;
27+
},
28+
}));
29+
30+
const mockInitializeGitHubRepository = vi.fn();
31+
32+
vi.mock("../steps/initializeGitHubRepository/index.js", () => ({
33+
get initializeGitHubRepository() {
34+
return mockInitializeGitHubRepository;
35+
},
36+
}));
37+
38+
const mockRemoveSetupScripts = vi.fn();
39+
40+
vi.mock("../steps/removeSetupScripts.js", () => ({
41+
get removeSetupScripts() {
42+
return mockRemoveSetupScripts;
43+
},
44+
}));
45+
46+
const mockResetGitTags = vi.fn();
47+
48+
vi.mock("../steps/resetGitTags.js", () => ({
49+
get resetGitTags() {
50+
return mockResetGitTags;
51+
},
52+
}));
53+
54+
const mockRunCleanup = vi.fn();
55+
56+
vi.mock("../steps/runCleanup.js", () => ({
57+
get runCleanup() {
58+
return mockRunCleanup;
59+
},
60+
}));
61+
62+
const mockUninstallPackages = vi.fn();
63+
64+
vi.mock("../steps/uninstallPackages.js", () => ({
65+
get uninstallPackages() {
66+
return mockUninstallPackages;
67+
},
68+
}));
69+
70+
const optionsBase = {} as Options;
71+
72+
describe("initializeWithOptions", () => {
73+
it("runs addOwnerAsAllContributor when excludeAllContributors is false", async () => {
74+
const options = {
75+
...optionsBase,
76+
excludeAllContributors: false,
77+
};
78+
79+
await initializeWithOptions({
80+
github: undefined,
81+
options,
82+
});
83+
84+
expect(mockAddOwnerAsAllContributor).toHaveBeenCalledWith(options);
85+
});
86+
87+
it("does not run addOwnerAsAllContributor when excludeAllContributors is true", async () => {
88+
const options = {
89+
...optionsBase,
90+
excludeAllContributors: true,
91+
};
92+
93+
await initializeWithOptions({
94+
github: undefined,
95+
options,
96+
});
97+
98+
expect(mockAddOwnerAsAllContributor).not.toHaveBeenCalled();
99+
});
100+
101+
it("runs initializeGitHubRepository when github is truthy", async () => {
102+
const github = {
103+
octokit: {},
104+
} as GitHub;
105+
106+
await initializeWithOptions({
107+
github,
108+
options: optionsBase,
109+
});
110+
111+
expect(mockInitializeGitHubRepository).toHaveBeenCalledWith(
112+
github.octokit,
113+
optionsBase,
114+
);
115+
});
116+
117+
it("does not run initializeGitHubRepository when github is falsy", async () => {
118+
const options = {
119+
...optionsBase,
120+
excludeAllContributors: true,
121+
};
122+
123+
await initializeWithOptions({
124+
github: undefined,
125+
options,
126+
});
127+
128+
expect(mockInitializeGitHubRepository).not.toHaveBeenCalled();
129+
});
130+
131+
it("runs removeSetupScripts when skipRemoval is false", async () => {
132+
const options = {
133+
...optionsBase,
134+
skipRemoval: false,
135+
};
136+
137+
await initializeWithOptions({
138+
github: undefined,
139+
options,
140+
});
141+
142+
expect(mockRemoveSetupScripts).toHaveBeenCalled();
143+
});
144+
145+
it("does not run removeSetupScripts when skipRemoval is true", async () => {
146+
const options = {
147+
...optionsBase,
148+
skipRemoval: true,
149+
};
150+
151+
await initializeWithOptions({
152+
github: undefined,
153+
options,
154+
});
155+
156+
expect(mockRemoveSetupScripts).not.toHaveBeenCalled();
157+
});
158+
159+
it("runs uninstallPackages when skipUninstall is false", async () => {
160+
const options = {
161+
...optionsBase,
162+
offline: true,
163+
skipUninstall: false,
164+
};
165+
166+
await initializeWithOptions({
167+
github: undefined,
168+
options,
169+
});
170+
171+
expect(mockUninstallPackages).toHaveBeenCalledWith(options.offline);
172+
});
173+
174+
it("does not run uninstallPackages when skipUninstall is true", async () => {
175+
const options = {
176+
...optionsBase,
177+
skipUninstall: true,
178+
};
179+
180+
await initializeWithOptions({
181+
github: undefined,
182+
options,
183+
});
184+
185+
expect(mockUninstallPackages).not.toHaveBeenCalled();
186+
});
187+
});

src/shared/runOrRestore.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import chalk from "chalk";
33
import { $ } from "execa";
44

55
import { logLine } from "./cli/lines.js";
6+
import { StatusCodes } from "./codes.js";
67

78
export interface RunOrRestoreOptions {
89
run: () => Promise<void>;
@@ -12,7 +13,7 @@ export interface RunOrRestoreOptions {
1213
export async function runOrRestore({ run, skipRestore }: RunOrRestoreOptions) {
1314
try {
1415
await run();
15-
return 0;
16+
return StatusCodes.Success;
1617
} catch (error) {
1718
logLine();
1819
console.log(error);

0 commit comments

Comments
 (0)