Skip to content

Commit dabee2b

Browse files
test: finish unit testing migrate and migrateWithOptions (#1632)
## PR Checklist - [x] Addresses an existing open issue: fixes #872 - [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. 💖
1 parent 61e374f commit dabee2b

File tree

2 files changed

+265
-6
lines changed

2 files changed

+265
-6
lines changed

src/migrate/index.test.ts

Lines changed: 64 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 { migrate } 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 mockMigrateAndEnterRepository = 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 mockMigrateWithOptions = vi.fn();
2439

25-
vi.mock("./migrateAndEnterRepository.js", () => ({
26-
get migrateAndEnterRepository() {
27-
return mockMigrateAndEnterRepository;
40+
vi.mock("./migrateWithOptions.js", () => ({
41+
get migrateWithOptions() {
42+
return mockMigrateWithOptions;
2843
},
2944
}));
3045

@@ -46,4 +61,47 @@ describe("migrate", () => {
4661
options: optionsBase,
4762
});
4863
});
64+
65+
it("runs migrateWithOptions when readOptions returns inputs", async () => {
66+
mockReadOptions.mockResolvedValue({
67+
cancelled: false,
68+
options: optionsBase,
69+
});
70+
71+
const result = await migrate([]);
72+
73+
expect(result).toEqual({
74+
code: StatusCodes.Success,
75+
options: optionsBase,
76+
});
77+
expect(mockEnsureGitRepository).toHaveBeenCalled();
78+
expect(mockOutro.mock.calls).toMatchInlineSnapshot(`
79+
[
80+
[
81+
[
82+
{
83+
"label": "You may consider committing these changes:",
84+
"lines": [
85+
"git add -A",
86+
"git commit -m "migrated repo to create-typescript-app ✨",
87+
"git push",
88+
],
89+
"variant": "code",
90+
},
91+
{
92+
"label": "Be sure to:",
93+
"lines": [
94+
"- enable the GitHub apps:
95+
- Codecov (https://github.com/apps/codecov)
96+
- Renovate (https://github.com/apps/renovate)",
97+
"- populate the secrets:
98+
- ACCESS_TOKEN (a GitHub PAT with repo and workflow permissions)
99+
- NPM_TOKEN (an npm access token with automation permissions)",
100+
],
101+
},
102+
],
103+
],
104+
]
105+
`);
106+
});
49107
});
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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 { migrateWithOptions } from "./migrateWithOptions.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 mockDetectExistingContributors = vi.fn();
15+
16+
vi.mock("../steps/detectExistingContributors.js", () => ({
17+
get detectExistingContributors() {
18+
return mockDetectExistingContributors;
19+
},
20+
}));
21+
22+
const mockFinalizeDependencies = vi.fn();
23+
24+
vi.mock("../steps/finalizeDependencies.js", () => ({
25+
get finalizeDependencies() {
26+
return mockFinalizeDependencies;
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 mockPopulateCSpellDictionary = vi.fn();
39+
40+
vi.mock("../steps/populateCSpellDictionary.js", () => ({
41+
get populateCSpellDictionary() {
42+
return mockPopulateCSpellDictionary;
43+
},
44+
}));
45+
46+
vi.mock("../steps/runCleanup.js", () => ({
47+
runCleanup: vi.fn(),
48+
}));
49+
50+
vi.mock("../shared/cli/spinners.js", () => ({
51+
async withSpinner(_label: string, task: () => Promise<void>) {
52+
await task();
53+
},
54+
withSpinners: vi.fn(),
55+
}));
56+
57+
const mockGitHubAndOptions = vi.fn();
58+
59+
vi.mock("../shared/options/readOptions.js", () => ({
60+
get GitHubAndOptions() {
61+
return mockGitHubAndOptions;
62+
},
63+
}));
64+
65+
const optionsBase = {} as Options;
66+
67+
describe("migrateWithOptions", () => {
68+
it("runs initializeGitHubRepository when github is truthy", async () => {
69+
const github = {
70+
octokit: {},
71+
} as GitHub;
72+
73+
await migrateWithOptions({
74+
github,
75+
options: optionsBase,
76+
});
77+
78+
expect(mockInitializeGitHubRepository).toHaveBeenCalledWith(
79+
github.octokit,
80+
optionsBase,
81+
);
82+
});
83+
84+
it("does not run initializeGitHubRepository when github is falsy", async () => {
85+
const options = {
86+
...optionsBase,
87+
excludeAllContributors: true,
88+
};
89+
90+
await migrateWithOptions({
91+
github: undefined,
92+
options,
93+
});
94+
95+
expect(mockInitializeGitHubRepository).not.toHaveBeenCalled();
96+
});
97+
98+
it("does not run detectExistingContributors when excludeAllContributors is true", async () => {
99+
const options = {
100+
...optionsBase,
101+
excludeAllContributors: true,
102+
skipAllContributorsApi: false,
103+
};
104+
105+
await migrateWithOptions({
106+
github: undefined,
107+
options,
108+
});
109+
110+
expect(mockDetectExistingContributors).not.toHaveBeenCalled();
111+
});
112+
113+
it("does not run detectExistingContributors when skipAllContributorsApi is true", async () => {
114+
const options = {
115+
...optionsBase,
116+
excludeAllContributors: false,
117+
skipAllContributorsApi: true,
118+
};
119+
120+
await migrateWithOptions({
121+
github: undefined,
122+
options,
123+
});
124+
125+
expect(mockDetectExistingContributors).not.toHaveBeenCalled();
126+
});
127+
128+
it("runs detectExistingContributors when excludeAllContributors and skipAllContributorsApi are false", async () => {
129+
const options = {
130+
...optionsBase,
131+
excludeAllContributors: false,
132+
skipAllContributorsApi: false,
133+
};
134+
135+
await migrateWithOptions({
136+
github: undefined,
137+
options,
138+
});
139+
140+
expect(mockDetectExistingContributors).toHaveBeenCalledWith(
141+
undefined,
142+
options,
143+
);
144+
});
145+
146+
it("runs finalizeDependencies when skipInstall is false", async () => {
147+
const options = {
148+
...optionsBase,
149+
skipInstall: false,
150+
};
151+
152+
await migrateWithOptions({
153+
github: undefined,
154+
options,
155+
});
156+
157+
expect(mockFinalizeDependencies).toHaveBeenCalledWith(options);
158+
});
159+
160+
it("does not run finalizeDependencies when skipInstall is true", async () => {
161+
const options = {
162+
...optionsBase,
163+
skipInstall: true,
164+
};
165+
166+
await migrateWithOptions({
167+
github: undefined,
168+
options,
169+
});
170+
171+
expect(mockFinalizeDependencies).not.toHaveBeenCalled();
172+
});
173+
174+
it("runs populateCSpellDictionary when excludeLintSpelling is false", async () => {
175+
const options = {
176+
...optionsBase,
177+
excludeLintSpelling: false,
178+
};
179+
180+
await migrateWithOptions({
181+
github: undefined,
182+
options,
183+
});
184+
185+
expect(mockPopulateCSpellDictionary).toHaveBeenCalled();
186+
});
187+
188+
it("does not run populateCSpellDictionary when excludeLintSpelling is true", async () => {
189+
const options = {
190+
...optionsBase,
191+
excludeLintSpelling: true,
192+
};
193+
194+
await migrateWithOptions({
195+
github: undefined,
196+
options,
197+
});
198+
199+
expect(mockPopulateCSpellDictionary).not.toHaveBeenCalled();
200+
});
201+
});

0 commit comments

Comments
 (0)