forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.ts
320 lines (272 loc) · 13.5 KB
/
index.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
import { vi } from "vitest";
import { logPossibleBugMessage } from "..";
import { getPackageManager } from "../package-manager";
import { updateCheck } from "../update-check";
import { endEventLoop } from "./helpers/end-event-loop";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import { writeWorkerSource } from "./helpers/write-worker-source";
import { writeWranglerConfig } from "./helpers/write-wrangler-config";
import type { PackageManager } from "../package-manager";
import type { Mock } from "vitest";
describe("wrangler", () => {
let mockPackageManager: PackageManager;
runInTempDir();
beforeEach(() => {
mockPackageManager = {
cwd: process.cwd(),
// @ts-expect-error we're making a fake package manager here
type: "mockpm",
addDevDeps: vi.fn(),
install: vi.fn(),
};
(getPackageManager as Mock).mockResolvedValue(mockPackageManager);
});
const std = mockConsoleMethods();
describe("no command", () => {
it("should display a list of available commands", async () => {
await runWrangler();
expect(std.out).toMatchInlineSnapshot(`
"wrangler
COMMANDS
wrangler docs [search..] 📚 Open Wrangler's command documentation in your browser
wrangler init [name] 📥 Initialize a basic Worker
wrangler dev [script] 👂 Start a local server for developing your Worker
wrangler deploy [script] 🆙 Deploy a Worker to Cloudflare [aliases: publish]
wrangler deployments 🚢 List and view the current and past deployments for your Worker
wrangler rollback [version-id] 🔙 Rollback a deployment for a Worker
wrangler versions 🫧 List, view, upload and deploy Versions of your Worker to Cloudflare
wrangler triggers 🎯 Updates the triggers of your current deployment
wrangler delete [script] 🗑 Delete a Worker from Cloudflare
wrangler tail [worker] 🦚 Start a log tailing session for a Worker
wrangler secret 🤫 Generate a secret that can be referenced in a Worker
wrangler types [path] 📝 Generate types from bindings and module rules in configuration
wrangler kv 🗂️ Manage Workers KV Namespaces
wrangler queues 🇶 Manage Workers Queues
wrangler r2 📦 Manage R2 buckets & objects
wrangler d1 🗄 Manage Workers D1 databases
wrangler vectorize 🧮 Manage Vectorize indexes [open beta]
wrangler hyperdrive 🚀 Manage Hyperdrive databases
wrangler pages ⚡️ Configure Cloudflare Pages
wrangler mtls-certificate 🪪 Manage certificates used for mTLS connections
wrangler pubsub 📮 Manage Pub/Sub brokers [private beta]
wrangler dispatch-namespace 🏗️ Manage dispatch namespaces
wrangler ai 🤖 Manage AI models
wrangler workflows 🔁 Manage Workflows [open-beta]
wrangler login 🔓 Login to Cloudflare
wrangler logout 🚪 Logout from Cloudflare
wrangler whoami 🕵️ Retrieve your user information
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});
describe("invalid command", () => {
it("should display an error", async () => {
await expect(
runWrangler("invalid-command")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Unknown argument: invalid-command]`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler
COMMANDS
wrangler docs [search..] 📚 Open Wrangler's command documentation in your browser
wrangler init [name] 📥 Initialize a basic Worker
wrangler dev [script] 👂 Start a local server for developing your Worker
wrangler deploy [script] 🆙 Deploy a Worker to Cloudflare [aliases: publish]
wrangler deployments 🚢 List and view the current and past deployments for your Worker
wrangler rollback [version-id] 🔙 Rollback a deployment for a Worker
wrangler versions 🫧 List, view, upload and deploy Versions of your Worker to Cloudflare
wrangler triggers 🎯 Updates the triggers of your current deployment
wrangler delete [script] 🗑 Delete a Worker from Cloudflare
wrangler tail [worker] 🦚 Start a log tailing session for a Worker
wrangler secret 🤫 Generate a secret that can be referenced in a Worker
wrangler types [path] 📝 Generate types from bindings and module rules in configuration
wrangler kv 🗂️ Manage Workers KV Namespaces
wrangler queues 🇶 Manage Workers Queues
wrangler r2 📦 Manage R2 buckets & objects
wrangler d1 🗄 Manage Workers D1 databases
wrangler vectorize 🧮 Manage Vectorize indexes [open beta]
wrangler hyperdrive 🚀 Manage Hyperdrive databases
wrangler pages ⚡️ Configure Cloudflare Pages
wrangler mtls-certificate 🪪 Manage certificates used for mTLS connections
wrangler pubsub 📮 Manage Pub/Sub brokers [private beta]
wrangler dispatch-namespace 🏗️ Manage dispatch namespaces
wrangler ai 🤖 Manage AI models
wrangler workflows 🔁 Manage Workflows [open-beta]
wrangler login 🔓 Login to Cloudflare
wrangler logout 🚪 Logout from Cloudflare
wrangler whoami 🕵️ Retrieve your user information
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mUnknown argument: invalid-command[0m
"
`);
});
});
describe("global options", () => {
it("should display an error if duplicated --env or --config arguments are provided", async () => {
await expect(
runWrangler("--env prod -e prod")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The argument "--env" expects a single value, but received multiple: ["prod","prod"].]`
);
await expect(
runWrangler("--config=wrangler.toml -c example")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The argument "--config" expects a single value, but received multiple: ["wrangler.toml","example"].]`
);
});
});
describe("preview", () => {
it("should throw an error if the deprecated command is used with positional arguments", async () => {
await expect(runWrangler("preview GET")).rejects
.toThrowErrorMatchingInlineSnapshot(`
[Error: Deprecation:
The \`wrangler preview\` command has been deprecated.
Try using \`wrangler dev\` to to try out a worker during development.
]
`);
await expect(runWrangler(`preview GET "SomeBody"`)).rejects
.toThrowErrorMatchingInlineSnapshot(`
[Error: Deprecation:
The \`wrangler preview\` command has been deprecated.
Try using \`wrangler dev\` to to try out a worker during development.
]
`);
});
});
describe("subcommand implicit help ran on incomplete command execution", () => {
it("no subcommand for 'secret' should display a list of available subcommands", async () => {
await runWrangler("secret");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"wrangler secret
🤫 Generate a secret that can be referenced in a Worker
COMMANDS
wrangler secret put <key> Create or update a secret variable for a Worker
wrangler secret delete <key> Delete a secret variable from a Worker
wrangler secret list List all secrets for a Worker
wrangler secret bulk [json] Bulk upload secrets for a Worker
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("no subcommand 'kv namespace' should display a list of available subcommands", async () => {
await runWrangler("kv namespace");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"wrangler kv namespace
Interact with your Workers KV Namespaces
COMMANDS
wrangler kv namespace create <namespace> Create a new namespace
wrangler kv namespace list Output a list of all KV namespaces associated with your account id
wrangler kv namespace delete Delete a given namespace.
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("no subcommand 'kv key' should display a list of available subcommands", async () => {
await runWrangler("kv key");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"wrangler kv key
Individually manage Workers KV key-value pairs
COMMANDS
wrangler kv key put <key> [value] Write a single key/value pair to the given namespace
wrangler kv key list Output a list of all keys in a given namespace
wrangler kv key get <key> Read a single value by key from the given namespace
wrangler kv key delete <key> Remove a single key value pair from the given namespace
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("no subcommand 'kv bulk' should display a list of available subcommands", async () => {
await runWrangler("kv bulk");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"wrangler kv bulk
Interact with multiple Workers KV key-value pairs at once
COMMANDS
wrangler kv bulk put <filename> Upload multiple key-value pairs to a namespace
wrangler kv bulk delete <filename> Delete multiple key-value pairs from a namespace
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("no subcommand 'r2' should display a list of available subcommands", async () => {
await runWrangler("r2");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"wrangler r2
📦 Manage R2 buckets & objects
COMMANDS
wrangler r2 object Manage R2 objects
wrangler r2 bucket Manage R2 buckets
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
});
it("should print a deprecation message for 'build' and then try to run `deploy --dry-run --outdir`", async () => {
writeWranglerConfig({
main: "index.js",
});
writeWorkerSource();
await runWrangler("build");
await endEventLoop();
expect(std.out).toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mDeprecation: \`wrangler build\` has been deprecated.[0m
Please refer to [4mhttps://developers.cloudflare.com/workers/wrangler/migration/deprecations/#build[0m
for more information.
Attempting to run \`wrangler deploy --dry-run --outdir=dist\` for you instead:
Total Upload: xx KiB / gzip: xx KiB
--dry-run: exiting now."
`);
});
describe("logPossibleBugMessage()", () => {
it("should display a 'possible bug' message", async () => {
await logPossibleBugMessage();
expect(std.out).toMatchInlineSnapshot(
`"[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose[0m"`
);
});
it("should display a 'try updating' message if there is one available", async () => {
(updateCheck as Mock).mockImplementation(async () => "123.123.123");
await logPossibleBugMessage();
expect(std.out).toMatchInlineSnapshot(`
"[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose[0m
Note that there is a newer version of Wrangler available (123.123.123). Consider checking whether upgrading resolves this error."
`);
});
});
});