Skip to content

Commit 0b2d425

Browse files
chore: add updated test for createWithOptions.ts (#1183)
<!-- 👋 Hi, thanks for sending a PR to create-typescript-app! 💖. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #874 - [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 This is a test file for createWithOptions. It tests the logic of createWithOptions.. This PR is an extension of a previous PR, #1119.
1 parent dce4a61 commit 0b2d425

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

src/create/createWithOptions.test.ts

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import { Octokit } from "octokit";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
import { SpinnerTask } from "../shared/cli/spinners.js";
5+
import { doesRepositoryExist } from "../shared/doesRepositoryExist.js";
6+
import { Options } from "../shared/types.js";
7+
import { addToolAllContributors } from "../steps/addToolAllContributors.js";
8+
import { finalizeDependencies } from "../steps/finalizeDependencies.js";
9+
import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js";
10+
import { runCommands } from "../steps/runCommands.js";
11+
import { createWithOptions } from "./createWithOptions.js";
12+
13+
const optionsBase: Options = {
14+
access: "public",
15+
author: "Test Author",
16+
base: "common",
17+
description: "Test Description",
18+
directory: "test-directory",
19+
email: { github: "[email protected]", npm: "[email protected]" },
20+
funding: "Test Funding",
21+
keywords: ["test", "keywords"],
22+
logo: { alt: "Test Alt", src: "test.png" },
23+
mode: "create",
24+
owner: "Test Owner",
25+
repository: "test-repo",
26+
title: "Test Title",
27+
};
28+
29+
const mock$ = vi.fn();
30+
31+
vi.mock("execa", () => ({
32+
get $() {
33+
return mock$;
34+
},
35+
}));
36+
37+
const mockOctokit = new Octokit();
38+
const github = {
39+
auth: "auth-token",
40+
octokit: mockOctokit,
41+
};
42+
43+
vi.mock("../shared/cli/spinners.js", () => ({
44+
withSpinner: async <Return>(label: string, task: SpinnerTask<Return>) => {
45+
return await task();
46+
},
47+
withSpinners: async (
48+
label: string,
49+
tasks: [string, SpinnerTask<unknown>][],
50+
) => {
51+
for (const [, task] of tasks) {
52+
await task();
53+
}
54+
},
55+
}));
56+
57+
vi.mock("../steps/writing/writeStructure.js");
58+
59+
vi.mock("../steps/writeReadme/index.js");
60+
61+
vi.mock("../steps/finalizeDependencies.js");
62+
63+
vi.mock("../steps/runCommands.js");
64+
65+
vi.mock("../shared/doesRepositoryExist.js", () => ({
66+
doesRepositoryExist: vi.fn().mockResolvedValue(true),
67+
}));
68+
69+
vi.mock("../steps/initializeGitHubRepository/index.js", () => ({
70+
initializeGitHubRepository: vi.fn().mockResolvedValue(true),
71+
}));
72+
73+
vi.mock("../shared/getGitHubUserAsAllContributor.js", () => ({
74+
getGitHubUserAsAllContributor: vi.fn().mockResolvedValue({
75+
contributions: ["code", "doc"],
76+
name: "Test User",
77+
}),
78+
}));
79+
80+
vi.mock("../steps/addToolAllContributors.js");
81+
82+
describe("createWithOptions", () => {
83+
it("calls addToolAllContributors with options when excludeAllContributors is false", async () => {
84+
const options = {
85+
...optionsBase,
86+
excludeAllContributors: false,
87+
skipAllContributorsApi: false,
88+
};
89+
90+
await createWithOptions({ github, options });
91+
expect(addToolAllContributors).toHaveBeenCalledWith(options);
92+
});
93+
94+
it("does not call addToolAllContributors when excludeAllContributors is true", async () => {
95+
const options = {
96+
...optionsBase,
97+
excludeAllContributors: true,
98+
};
99+
100+
await createWithOptions({ github, options });
101+
expect(addToolAllContributors).not.toHaveBeenCalled();
102+
});
103+
104+
it("does not call addToolAllContributors when skipAllContributorsApi is true", async () => {
105+
const options = {
106+
...optionsBase,
107+
skipAllContributorsApi: true,
108+
};
109+
110+
await createWithOptions({ github, options });
111+
expect(addToolAllContributors).not.toHaveBeenCalled();
112+
});
113+
114+
it("does not call finalizeDependencies or runCommands when skipInstall is true", async () => {
115+
const options = {
116+
...optionsBase,
117+
skipInstall: true,
118+
};
119+
120+
await createWithOptions({ github, options });
121+
expect(finalizeDependencies).not.toHaveBeenCalled();
122+
expect(runCommands).not.toHaveBeenCalled();
123+
});
124+
125+
it("calls finalizeDependencies and runCommands when skipInstall is false", async () => {
126+
const options = {
127+
...optionsBase,
128+
skipInstall: false,
129+
};
130+
131+
await createWithOptions({ github, options });
132+
133+
expect(finalizeDependencies).toHaveBeenCalledWith(options);
134+
expect(runCommands).toHaveBeenCalled();
135+
});
136+
137+
it("does not initialize GitHub repository if repository does not exist", async () => {
138+
const options = optionsBase;
139+
vi.mocked(doesRepositoryExist).mockResolvedValueOnce(false);
140+
await createWithOptions({ github, options });
141+
expect(initializeGitHubRepository).not.toHaveBeenCalled();
142+
expect(mock$).not.toHaveBeenCalled();
143+
});
144+
145+
it("executes git commands when initializing GitHub repository when doesRepositoryExist is true", async () => {
146+
const options = optionsBase;
147+
148+
vi.mocked(doesRepositoryExist).mockResolvedValueOnce(true);
149+
await createWithOptions({ github, options });
150+
151+
expect(mock$.mock.calls).toMatchInlineSnapshot(`
152+
[
153+
[
154+
[
155+
"git remote add origin https://github.com/",
156+
"/",
157+
"",
158+
],
159+
"Test Owner",
160+
"test-repo",
161+
],
162+
[
163+
[
164+
"git add -A",
165+
],
166+
],
167+
[
168+
[
169+
"git commit --message ",
170+
"",
171+
],
172+
"feat: initialized repo ✨",
173+
],
174+
[
175+
[
176+
"git push -u origin main --force",
177+
],
178+
],
179+
]
180+
`);
181+
});
182+
183+
it("calls doesRepositoryExist with github and options when doesRepositoryExist is true", async () => {
184+
const options = optionsBase;
185+
vi.mocked(doesRepositoryExist).mockResolvedValueOnce(true);
186+
187+
await createWithOptions({ github, options });
188+
189+
expect(doesRepositoryExist).toHaveBeenCalledWith(github.octokit, options);
190+
expect(initializeGitHubRepository).toHaveBeenCalledWith(
191+
github.octokit,
192+
options,
193+
);
194+
});
195+
});

0 commit comments

Comments
 (0)