/
pages.test.ts
329 lines (281 loc) · 11 KB
/
pages.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { writeFileSync } from "node:fs";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import type { Project, Deployment } from "../pages";
import type { File, FormData } from "undici";
describe("pages", () => {
runInTempDir();
const std = mockConsoleMethods();
function endEventLoop() {
return new Promise((resolve) => setImmediate(resolve));
}
it("should should display a list of available subcommands, for pages with no subcommand", async () => {
await runWrangler("pages");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"wrangler pages
⚡️ Configure Cloudflare Pages
Commands:
wrangler pages dev [directory] [-- command] 🧑💻 Develop your full-stack Pages application locally
wrangler pages project ⚡️ Interact with your Pages projects
wrangler pages deployment 🚀 Interact with the deployments of a project
wrangler pages publish [directory] 🆙 Publish a directory of static assets as a Pages deployment
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
--legacy-env Use legacy environments [boolean]
🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
`);
});
describe("beta message for subcommands", () => {
it("should display for pages:dev", async () => {
await expect(
runWrangler("pages dev")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Must specify a directory of static assets to serve or a command to run."`
);
expect(std.out).toMatchInlineSnapshot(`
"🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new.[0m"
`);
});
it("should display for pages:functions:build", async () => {
await expect(runWrangler("pages functions build")).rejects.toThrowError();
expect(std.out).toMatchInlineSnapshot(`
"🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new.[0m"
`);
});
});
describe("project list", () => {
mockAccountId();
mockApiToken();
afterEach(() => {
unsetAllMocks();
});
function mockListRequest(projects: unknown[]) {
const requests = { count: 0 };
setMockResponse(
"/accounts/:accountId/pages/projects",
([_url, accountId], init, query) => {
requests.count++;
expect(accountId).toEqual("some-account-id");
expect(query.get("per_page")).toEqual("10");
expect(query.get("page")).toEqual(`${requests.count}`);
expect(init).toEqual({});
const pageSize = Number(query.get("per_page"));
const page = Number(query.get("page"));
return projects.slice((page - 1) * pageSize, page * pageSize);
}
);
return requests;
}
it("should make request to list projects", async () => {
const projects: Project[] = [
{
name: "dogs",
subdomain: "docs.pages.dev",
domains: ["dogs.pages.dev"],
source: {
type: "github",
},
latest_deployment: {
modified_on: "2021-11-17T14:52:26.133835Z",
},
created_on: "2021-11-17T14:52:26.133835Z",
production_branch: "main",
},
{
name: "cats",
subdomain: "cats.pages.dev",
domains: ["cats.pages.dev", "kitten.com"],
latest_deployment: {
modified_on: "2021-11-17T14:52:26.133835Z",
},
created_on: "2021-11-17T14:52:26.133835Z",
production_branch: "main",
},
];
const requests = mockListRequest(projects);
await runWrangler("pages project list");
expect(requests.count).toBe(1);
});
it("should make multiple requests for paginated results", async () => {
const projects: Project[] = [];
for (let i = 0; i < 15; i++) {
projects.push({
name: "dogs" + i,
subdomain: i + "dogs.pages.dev",
domains: [i + "dogs.pages.dev"],
source: {
type: "github",
},
latest_deployment: {
modified_on: "2021-11-17T14:52:26.133835Z",
},
created_on: "2021-11-17T14:52:26.133835Z",
production_branch: "main",
});
}
const requests = mockListRequest(projects);
await runWrangler("pages project list");
expect(requests.count).toEqual(2);
});
});
describe("project create", () => {
mockAccountId();
mockApiToken();
afterEach(() => {
unsetAllMocks();
});
it("should create a project with a production branch", async () => {
setMockResponse(
"/accounts/:accountId/pages/projects",
([_url, accountId], init) => {
expect(accountId).toEqual("some-account-id");
expect(init.method).toEqual("POST");
const body = JSON.parse(init.body as string);
expect(body).toEqual({
name: "a-new-project",
production_branch: "main",
});
return {
name: "a-new-project",
subdomain: "a-new-project.pages.dev",
production_branch: "main",
};
}
);
await runWrangler(
"pages project create a-new-project --production-branch=main"
);
expect(std.out).toMatchInlineSnapshot(`
"✨ Successfully created the 'a-new-project' project. It will be available at https://a-new-project.pages.dev/ once you create your first deployment.
To deploy a folder of assets, run 'wrangler pages publish [directory]'."
`);
});
});
describe("deployment list", () => {
mockAccountId();
mockApiToken();
afterEach(() => {
unsetAllMocks();
});
function mockListRequest(deployments: unknown[]) {
const requests = { count: 0 };
setMockResponse(
"/accounts/:accountId/pages/projects/:project/deployments",
([_url, accountId, project]) => {
requests.count++;
expect(project).toEqual("images");
expect(accountId).toEqual("some-account-id");
return deployments;
}
);
return requests;
}
it("should make request to list deployments", async () => {
const deployments: Deployment[] = [
{
id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
url: "https://87bbc8fe.images.pages.dev",
environment: "preview",
latest_stage: {
ended_on: "2021-11-17T14:52:26.133835Z",
status: "success",
},
deployment_trigger: {
metadata: {
branch: "main",
commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
},
},
project_name: "images",
},
];
const requests = mockListRequest(deployments);
await runWrangler("pages deployment list --project-name=images");
expect(requests.count).toBe(1);
});
});
describe("deployment create", () => {
let actualProcessEnvCI: string | undefined;
mockAccountId();
mockApiToken();
runInTempDir();
beforeEach(() => {
actualProcessEnvCI = process.env.CI;
process.env.CI = "true";
});
afterEach(() => {
unsetAllMocks();
process.env.CI = actualProcessEnvCI;
});
it("should be aliased with 'wrangler pages publish'", async () => {
await runWrangler("pages publish --help");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"wrangler pages publish [directory]
🆙 Publish a directory of static assets as a Pages deployment
Positionals:
directory The directory of Pages Functions [string] [default: \\"functions\\"]
Flags:
-c, --config Path to .toml configuration file [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
--legacy-env Use legacy environments [boolean]
Options:
--project-name The name of the project you want to list deployments for [string]
--branch The branch of the project you want to list deployments for [string]
--commit-hash The branch of the project you want to list deployments for [string]
--commit-message The branch of the project you want to list deployments for [string]
--commit-dirty The branch of the project you want to list deployments for [boolean]
🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
`);
});
it("should upload a directory of files", async () => {
writeFileSync("logo.png", "foobar");
setMockResponse(
"/accounts/:accountId/pages/projects/foo/file",
async ([_url, accountId], init) => {
expect(accountId).toEqual("some-account-id");
expect(init.method).toEqual("POST");
const body = init.body as FormData;
const logoPNGFile = body.get("file") as File;
expect(await logoPNGFile.text()).toEqual("foobar");
expect(logoPNGFile.name).toEqual("logo.png");
return {
id: "2082190357cfd3617ccfe04f340c6247",
};
}
);
setMockResponse(
"/accounts/:accountId/pages/projects/foo/deployments",
async ([_url, accountId], init) => {
expect(accountId).toEqual("some-account-id");
expect(init.method).toEqual("POST");
const body = init.body as FormData;
const manifest = JSON.parse(body.get("manifest") as string);
expect(manifest).toMatchInlineSnapshot(`
Object {
"/logo.png": "2082190357cfd3617ccfe04f340c6247",
}
`);
return {
url: "https://abcxyz.foo.pages.dev/",
};
}
);
await runWrangler("pages publish . --project-name=foo");
// TODO: Unmounting somehow loses this output
// expect(std.out).toMatchInlineSnapshot(`
// "✨ Success! Uploaded 1 files (TIMINGS)
// ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
// `);
});
});
});