wrangler 0.0.0-de73fa2 → 0.0.0-e151223

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +12 -9
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +21 -13
  5. package/pages/functions/filepath-routing.ts +38 -28
  6. package/pages/functions/routes.ts +8 -6
  7. package/pages/functions/template-worker.ts +16 -24
  8. package/src/__tests__/dev.test.tsx +7 -3
  9. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  10. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  11. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +10 -8
  12. package/src/__tests__/helpers/mock-console.ts +56 -0
  13. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  14. package/src/__tests__/helpers/mock-kv.ts +40 -0
  15. package/src/__tests__/helpers/mock-user.ts +27 -0
  16. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  17. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  18. package/src/__tests__/index.test.ts +173 -54
  19. package/src/__tests__/jest.setup.ts +14 -2
  20. package/src/__tests__/kv.test.ts +327 -289
  21. package/src/__tests__/logout.test.ts +50 -0
  22. package/src/__tests__/publish.test.ts +1222 -0
  23. package/src/__tests__/secret.test.ts +210 -0
  24. package/src/__tests__/whoami.test.tsx +86 -0
  25. package/src/api/form_data.ts +10 -7
  26. package/src/api/preview.ts +2 -2
  27. package/src/api/worker.ts +3 -3
  28. package/src/cfetch/index.ts +13 -4
  29. package/src/cfetch/internal.ts +10 -7
  30. package/src/config.ts +162 -8
  31. package/src/dev.tsx +105 -57
  32. package/src/dialogs.tsx +2 -2
  33. package/src/index.tsx +388 -302
  34. package/src/inspect.ts +80 -23
  35. package/src/kv.tsx +1 -1
  36. package/src/module-collection.ts +5 -6
  37. package/src/npm-installer.ts +20 -0
  38. package/src/pages.tsx +42 -28
  39. package/src/paths.ts +26 -0
  40. package/src/proxy.ts +78 -10
  41. package/src/publish.ts +97 -51
  42. package/src/sites.tsx +175 -53
  43. package/src/user.tsx +51 -59
  44. package/src/whoami.tsx +69 -0
  45. package/wrangler-dist/cli.js +111330 -110202
  46. package/wrangler-dist/cli.js.map +3 -3
  47. package/src/__tests__/run-in-tmp.ts +0 -19
  48. package/src/__tests__/run-wrangler.ts +0 -32
@@ -1,20 +1,21 @@
1
- import { writeFileSync } from "fs";
1
+ import { writeFileSync } from "node:fs";
2
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2
3
  import {
3
4
  setMockResponse,
4
5
  setMockRawResponse,
5
6
  unsetAllMocks,
6
- createFetchResult,
7
- } from "./mock-cfetch";
8
- import { runWrangler } from "./run-wrangler";
9
- import { runInTempDir } from "./run-in-tmp";
10
-
11
- interface KVNamespaceInfo {
12
- title: string;
13
- id: string;
14
- }
7
+ } from "./helpers/mock-cfetch";
8
+ import { mockConsoleMethods } from "./helpers/mock-console";
9
+ import { mockKeyListRequest } from "./helpers/mock-kv";
10
+ import { runInTempDir } from "./helpers/run-in-tmp";
11
+ import { runWrangler } from "./helpers/run-wrangler";
12
+ import type { KVNamespaceInfo } from "../kv";
15
13
 
16
14
  describe("wrangler", () => {
15
+ mockAccountId();
16
+ mockApiToken();
17
17
  runInTempDir();
18
+ const std = mockConsoleMethods();
18
19
 
19
20
  afterEach(() => {
20
21
  unsetAllMocks();
@@ -36,11 +37,13 @@ describe("wrangler", () => {
36
37
  }
37
38
 
38
39
  it("should error if no namespace is given", async () => {
39
- const { error, stdout, stderr } = await runWrangler(
40
- "kv:namespace create"
40
+ await expect(
41
+ runWrangler("kv:namespace create")
42
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
43
+ `"Not enough non-option arguments: got 0, need at least 1"`
41
44
  );
42
- expect(stdout).toMatchInlineSnapshot(`""`);
43
- expect(stderr).toMatchInlineSnapshot(`
45
+ expect(std.out).toMatchInlineSnapshot(`""`);
46
+ expect(std.err).toMatchInlineSnapshot(`
44
47
  "wrangler kv:namespace create <namespace>
45
48
 
46
49
  Create a new namespace
@@ -60,17 +63,16 @@ describe("wrangler", () => {
60
63
 
61
64
  Not enough non-option arguments: got 0, need at least 1"
62
65
  `);
63
- expect(error).toMatchInlineSnapshot(
64
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
65
- );
66
66
  });
67
67
 
68
68
  it("should error if the namespace to create contains spaces", async () => {
69
- const { error, stdout, stderr } = await runWrangler(
70
- "kv:namespace create abc def ghi"
69
+ await expect(
70
+ runWrangler("kv:namespace create abc def ghi")
71
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
72
+ `"Unexpected additional positional arguments \\"def ghi\\"."`
71
73
  );
72
- expect(stdout).toMatchInlineSnapshot(`""`);
73
- expect(stderr).toMatchInlineSnapshot(`
74
+ expect(std.out).toMatchInlineSnapshot(`""`);
75
+ expect(std.err).toMatchInlineSnapshot(`
74
76
  "wrangler kv:namespace create <namespace>
75
77
 
76
78
  Create a new namespace
@@ -90,17 +92,17 @@ describe("wrangler", () => {
90
92
 
91
93
  Unexpected additional positional arguments \\"def ghi\\"."
92
94
  `);
93
- expect(error).toMatchInlineSnapshot(
94
- `[Error: Unexpected additional positional arguments "def ghi".]`
95
- );
96
95
  });
97
96
 
98
97
  it("should error if the namespace to create is not valid", async () => {
99
- const { error, stdout, stderr } = await runWrangler(
100
- "kv:namespace create abc-def"
98
+ await expect(
99
+ runWrangler("kv:namespace create abc-def")
100
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
101
+ `"The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."`
101
102
  );
102
- expect(stdout).toMatchInlineSnapshot(`""`);
103
- expect(stderr).toMatchInlineSnapshot(`
103
+
104
+ expect(std.out).toMatchInlineSnapshot(`""`);
105
+ expect(std.err).toMatchInlineSnapshot(`
104
106
  "wrangler kv:namespace create <namespace>
105
107
 
106
108
  Create a new namespace
@@ -120,17 +122,12 @@ describe("wrangler", () => {
120
122
 
121
123
  The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."
122
124
  `);
123
- expect(error).toMatchInlineSnapshot(
124
- `[Error: The namespace binding name "abc-def" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.]`
125
- );
126
125
  });
127
126
 
128
127
  it("should create a namespace", async () => {
129
128
  mockCreateRequest("worker-UnitTestNamespace");
130
- const { stdout } = await runWrangler(
131
- "kv:namespace create UnitTestNamespace"
132
- );
133
- expect(stdout).toMatchInlineSnapshot(`
129
+ await runWrangler("kv:namespace create UnitTestNamespace");
130
+ expect(std.out).toMatchInlineSnapshot(`
134
131
  "🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
135
132
  ✨ Success!
136
133
  Add the following to your configuration file in your kv_namespaces array:
@@ -140,10 +137,8 @@ describe("wrangler", () => {
140
137
 
141
138
  it("should create a preview namespace if configured to do so", async () => {
142
139
  mockCreateRequest("worker-UnitTestNamespace_preview");
143
- const { stdout } = await runWrangler(
144
- "kv:namespace create UnitTestNamespace --preview"
145
- );
146
- expect(stdout).toMatchInlineSnapshot(`
140
+ await runWrangler("kv:namespace create UnitTestNamespace --preview");
141
+ expect(std.out).toMatchInlineSnapshot(`
147
142
  "🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
148
143
  ✨ Success!
149
144
  Add the following to your configuration file in your kv_namespaces array:
@@ -154,10 +149,8 @@ describe("wrangler", () => {
154
149
  it("should create a namespace using configured worker name", async () => {
155
150
  writeFileSync("./wrangler.toml", 'name = "otherWorker"', "utf-8");
156
151
  mockCreateRequest("otherWorker-UnitTestNamespace");
157
- const { stdout } = await runWrangler(
158
- "kv:namespace create UnitTestNamespace"
159
- );
160
- expect(stdout).toMatchInlineSnapshot(`
152
+ await runWrangler("kv:namespace create UnitTestNamespace");
153
+ expect(std.out).toMatchInlineSnapshot(`
161
154
  "🌀 Creating namespace with title \\"otherWorker-UnitTestNamespace\\"
162
155
  ✨ Success!
163
156
  Add the following to your configuration file in your kv_namespaces array:
@@ -167,10 +160,10 @@ describe("wrangler", () => {
167
160
 
168
161
  it("should create a namespace in an environment if configured to do so", async () => {
169
162
  mockCreateRequest("worker-customEnv-UnitTestNamespace");
170
- const { stdout } = await runWrangler(
163
+ await runWrangler(
171
164
  "kv:namespace create UnitTestNamespace --env customEnv"
172
165
  );
173
- expect(stdout).toMatchInlineSnapshot(`
166
+ expect(std.out).toMatchInlineSnapshot(`
174
167
  "🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
175
168
  ✨ Success!
176
169
  Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
@@ -206,12 +199,10 @@ describe("wrangler", () => {
206
199
  { title: "title-2", id: "id-2" },
207
200
  ];
208
201
  mockListRequest(kvNamespaces);
209
- const { error, stdout, stderr } = await runWrangler(
210
- "kv:namespace list"
211
- );
212
- expect(error).toMatchInlineSnapshot(`undefined`);
213
- expect(stderr).toMatchInlineSnapshot(`""`);
214
- const namespaces = JSON.parse(stdout);
202
+ await runWrangler("kv:namespace list");
203
+
204
+ expect(std.err).toMatchInlineSnapshot(`""`);
205
+ const namespaces = JSON.parse(std.out);
215
206
  expect(namespaces).toEqual(kvNamespaces);
216
207
  });
217
208
 
@@ -222,8 +213,8 @@ describe("wrangler", () => {
222
213
  kvNamespaces.push({ title: "title-" + i, id: "id-" + i });
223
214
  }
224
215
  const requests = mockListRequest(kvNamespaces);
225
- const { stdout } = await runWrangler("kv:namespace list");
226
- const namespaces = JSON.parse(stdout);
216
+ await runWrangler("kv:namespace list");
217
+ const namespaces = JSON.parse(std.out);
227
218
  expect(namespaces).toEqual(kvNamespaces);
228
219
  expect(requests.count).toEqual(6);
229
220
  });
@@ -273,10 +264,12 @@ describe("wrangler", () => {
273
264
 
274
265
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
275
266
  writeWranglerConfig();
276
- const { stderr } = await runWrangler(
277
- `kv:namespace delete --binding otherBinding`
278
- );
279
- expect(stderr).toMatchInlineSnapshot(`
267
+ await expect(runWrangler("kv:namespace delete --binding otherBinding"))
268
+ .rejects.toThrowErrorMatchingInlineSnapshot(`
269
+ "Not able to delete namespace.
270
+ A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
271
+ `);
272
+ expect(std.err).toMatchInlineSnapshot(`
280
273
  "wrangler kv:namespace delete
281
274
 
282
275
  Deletes a given namespace.
@@ -301,12 +294,12 @@ describe("wrangler", () => {
301
294
  it("should delete a namespace specified by binding name in a given environment", async () => {
302
295
  writeWranglerConfig();
303
296
  const requests = mockDeleteRequest("env-bound-id");
304
- const { stdout, stderr, error } = await runWrangler(
305
- `kv:namespace delete --binding someBinding --env some-environment --preview false`
297
+ await runWrangler(
298
+ "kv:namespace delete --binding someBinding --env some-environment --preview false"
306
299
  );
307
- expect(stdout).toMatchInlineSnapshot(`""`);
308
- expect(stderr).toMatchInlineSnapshot(`""`);
309
- expect(error).toMatchInlineSnapshot(`undefined`);
300
+
301
+ expect(std.out).toMatchInlineSnapshot(`""`);
302
+ expect(std.err).toMatchInlineSnapshot(`""`);
310
303
  expect(requests.count).toEqual(1);
311
304
  });
312
305
 
@@ -362,28 +355,29 @@ describe("wrangler", () => {
362
355
  "my-key",
363
356
  "my-value"
364
357
  );
365
- const { error, stderr, stdout } = await runWrangler(
358
+
359
+ await runWrangler(
366
360
  "kv:key put my-key my-value --namespace-id some-namespace-id"
367
361
  );
362
+
368
363
  expect(requests.count).toEqual(1);
369
- expect(stdout).toMatchInlineSnapshot(
364
+ expect(std.out).toMatchInlineSnapshot(
370
365
  `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
371
366
  );
372
- expect(stderr).toMatchInlineSnapshot(`""`);
373
- expect(error).toMatchInlineSnapshot(`undefined`);
367
+ expect(std.err).toMatchInlineSnapshot(`""`);
374
368
  });
375
369
 
376
370
  it("should put a key in a given namespace specified by binding", async () => {
377
371
  writeWranglerConfig();
378
372
  const requests = mockKeyPutRequest("bound-id", "my-key", "my-value");
379
- const { error, stderr, stdout } = await runWrangler(
373
+ await runWrangler(
380
374
  "kv:key put my-key my-value --binding someBinding --preview false"
381
375
  );
382
- expect(stdout).toMatchInlineSnapshot(
376
+
377
+ expect(std.out).toMatchInlineSnapshot(
383
378
  `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace bound-id"`
384
379
  );
385
- expect(stderr).toMatchInlineSnapshot(`""`);
386
- expect(error).toMatchInlineSnapshot(`undefined`);
380
+ expect(std.err).toMatchInlineSnapshot(`""`);
387
381
  expect(requests.count).toEqual(1);
388
382
  });
389
383
 
@@ -394,14 +388,15 @@ describe("wrangler", () => {
394
388
  "my-key",
395
389
  "my-value"
396
390
  );
397
- const { error, stderr, stdout } = await runWrangler(
391
+
392
+ await runWrangler(
398
393
  "kv:key put my-key my-value --binding someBinding --preview"
399
394
  );
400
- expect(stdout).toMatchInlineSnapshot(
395
+
396
+ expect(std.out).toMatchInlineSnapshot(
401
397
  `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace preview-bound-id"`
402
398
  );
403
- expect(stderr).toMatchInlineSnapshot(`""`);
404
- expect(error).toMatchInlineSnapshot(`undefined`);
399
+ expect(std.err).toMatchInlineSnapshot(`""`);
405
400
  expect(requests.count).toEqual(1);
406
401
  });
407
402
 
@@ -413,15 +408,14 @@ describe("wrangler", () => {
413
408
  10,
414
409
  20
415
410
  );
416
- const { error, stderr, stdout } = await runWrangler(
411
+ await runWrangler(
417
412
  "kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
418
413
  );
419
414
  expect(requests.count).toEqual(1);
420
- expect(stdout).toMatchInlineSnapshot(
415
+ expect(std.out).toMatchInlineSnapshot(
421
416
  `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
422
417
  );
423
- expect(stderr).toMatchInlineSnapshot(`""`);
424
- expect(error).toMatchInlineSnapshot(`undefined`);
418
+ expect(std.err).toMatchInlineSnapshot(`""`);
425
419
  });
426
420
 
427
421
  it("should put a key to the specified environment in a given namespace", async () => {
@@ -431,14 +425,13 @@ describe("wrangler", () => {
431
425
  "my-key",
432
426
  "my-value"
433
427
  );
434
- const { error, stderr, stdout } = await runWrangler(
428
+ await runWrangler(
435
429
  "kv:key put my-key my-value --binding someBinding --env some-environment --preview false"
436
430
  );
437
- expect(stdout).toMatchInlineSnapshot(
431
+ expect(std.out).toMatchInlineSnapshot(
438
432
  `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace env-bound-id"`
439
433
  );
440
- expect(stderr).toMatchInlineSnapshot(`""`);
441
- expect(error).toMatchInlineSnapshot(`undefined`);
434
+ expect(std.err).toMatchInlineSnapshot(`""`);
442
435
  expect(requests.count).toEqual(1);
443
436
  });
444
437
 
@@ -449,22 +442,25 @@ describe("wrangler", () => {
449
442
  "my-key",
450
443
  "file-contents"
451
444
  );
452
- const { error, stderr, stdout } = await runWrangler(
445
+ await runWrangler(
453
446
  "kv:key put my-key --namespace-id some-namespace-id --path foo.txt"
454
447
  );
455
- expect(stdout).toMatchInlineSnapshot(
448
+ expect(std.out).toMatchInlineSnapshot(
456
449
  `"writing the contents of foo.txt to the key \\"my-key\\" on namespace some-namespace-id"`
457
450
  );
458
- expect(stderr).toMatchInlineSnapshot(`""`);
459
- expect(error).toMatchInlineSnapshot(`undefined`);
451
+ expect(std.err).toMatchInlineSnapshot(`""`);
460
452
  expect(requests.count).toEqual(1);
461
453
  });
462
454
 
463
455
  it("should error if no key is provided", async () => {
464
- const { error, stdout, stderr } = await runWrangler("kv:key put");
456
+ await expect(
457
+ runWrangler("kv:key put")
458
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
459
+ `"Not enough non-option arguments: got 0, need at least 1"`
460
+ );
465
461
 
466
- expect(stdout).toMatchInlineSnapshot(`""`);
467
- expect(stderr).toMatchInlineSnapshot(`
462
+ expect(std.out).toMatchInlineSnapshot(`""`);
463
+ expect(std.err).toMatchInlineSnapshot(`
468
464
  "wrangler kv:key put <key> [value]
469
465
 
470
466
  Writes a single key/value pair to the given namespace.
@@ -490,18 +486,17 @@ describe("wrangler", () => {
490
486
 
491
487
  Not enough non-option arguments: got 0, need at least 1"
492
488
  `);
493
- expect(error).toMatchInlineSnapshot(
494
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
495
- );
496
489
  });
497
490
 
498
491
  it("should error if no binding nor namespace is provided", async () => {
499
- const { error, stdout, stderr } = await runWrangler(
500
- "kv:key put foo bar"
492
+ await expect(
493
+ runWrangler("kv:key put foo bar")
494
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
495
+ `"Exactly one of the arguments binding and namespace-id is required"`
501
496
  );
502
497
 
503
- expect(stdout).toMatchInlineSnapshot(`""`);
504
- expect(stderr).toMatchInlineSnapshot(`
498
+ expect(std.out).toMatchInlineSnapshot(`""`);
499
+ expect(std.err).toMatchInlineSnapshot(`
505
500
  "wrangler kv:key put <key> [value]
506
501
 
507
502
  Writes a single key/value pair to the given namespace.
@@ -527,18 +522,17 @@ describe("wrangler", () => {
527
522
 
528
523
  Exactly one of the arguments binding and namespace-id is required"
529
524
  `);
530
- expect(error).toMatchInlineSnapshot(
531
- `[Error: Exactly one of the arguments binding and namespace-id is required]`
532
- );
533
525
  });
534
526
 
535
527
  it("should error if both binding and namespace is provided", async () => {
536
- const { error, stdout, stderr } = await runWrangler(
537
- "kv:key put foo bar --binding x --namespace-id y"
528
+ await expect(
529
+ runWrangler("kv:key put foo bar --binding x --namespace-id y")
530
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
531
+ `"Arguments binding and namespace-id are mutually exclusive"`
538
532
  );
539
533
 
540
- expect(stdout).toMatchInlineSnapshot(`""`);
541
- expect(stderr).toMatchInlineSnapshot(`
534
+ expect(std.out).toMatchInlineSnapshot(`""`);
535
+ expect(std.err).toMatchInlineSnapshot(`
542
536
  "wrangler kv:key put <key> [value]
543
537
 
544
538
  Writes a single key/value pair to the given namespace.
@@ -564,18 +558,17 @@ describe("wrangler", () => {
564
558
 
565
559
  Arguments binding and namespace-id are mutually exclusive"
566
560
  `);
567
- expect(error).toMatchInlineSnapshot(
568
- `[Error: Arguments binding and namespace-id are mutually exclusive]`
569
- );
570
561
  });
571
562
 
572
563
  it("should error if no value nor path is provided", async () => {
573
- const { error, stdout, stderr } = await runWrangler(
574
- "kv:key put key --namespace-id 12345"
564
+ await expect(
565
+ runWrangler("kv:key put key --namespace-id 12345")
566
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
567
+ `"Exactly one of the arguments value and path is required"`
575
568
  );
576
569
 
577
- expect(stdout).toMatchInlineSnapshot(`""`);
578
- expect(stderr).toMatchInlineSnapshot(`
570
+ expect(std.out).toMatchInlineSnapshot(`""`);
571
+ expect(std.err).toMatchInlineSnapshot(`
579
572
  "wrangler kv:key put <key> [value]
580
573
 
581
574
  Writes a single key/value pair to the given namespace.
@@ -601,18 +594,17 @@ describe("wrangler", () => {
601
594
 
602
595
  Exactly one of the arguments value and path is required"
603
596
  `);
604
- expect(error).toMatchInlineSnapshot(
605
- `[Error: Exactly one of the arguments value and path is required]`
606
- );
607
597
  });
608
598
 
609
599
  it("should error if both value and path is provided", async () => {
610
- const { error, stdout, stderr } = await runWrangler(
611
- "kv:key put key value --path xyz --namespace-id 12345"
600
+ await expect(
601
+ runWrangler("kv:key put key value --path xyz --namespace-id 12345")
602
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
603
+ `"Arguments value and path are mutually exclusive"`
612
604
  );
613
605
 
614
- expect(stdout).toMatchInlineSnapshot(`""`);
615
- expect(stderr).toMatchInlineSnapshot(`
606
+ expect(std.out).toMatchInlineSnapshot(`""`);
607
+ expect(std.err).toMatchInlineSnapshot(`
616
608
  "wrangler kv:key put <key> [value]
617
609
 
618
610
  Writes a single key/value pair to the given namespace.
@@ -638,23 +630,23 @@ describe("wrangler", () => {
638
630
 
639
631
  Arguments value and path are mutually exclusive"
640
632
  `);
641
- expect(error).toMatchInlineSnapshot(
642
- `[Error: Arguments value and path are mutually exclusive]`
643
- );
644
633
  });
645
634
 
646
635
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
647
636
  writeWranglerConfig();
648
- const { error, stdout, stderr } = await runWrangler(
649
- `kv:key put key value --binding otherBinding`
650
- );
651
- expect(stdout).toMatchInlineSnapshot(`""`);
652
- expect(stderr).toMatchInlineSnapshot(
637
+ await expect(
638
+ runWrangler("kv:key put key value --binding otherBinding")
639
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
653
640
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
654
641
  );
655
- expect(error).toMatchInlineSnapshot(
656
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
657
- );
642
+
643
+ expect(std.out).toMatchInlineSnapshot(`""`);
644
+ expect(std.err).toMatchInlineSnapshot(`
645
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
646
+
647
+ %s
648
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
649
+ `);
658
650
  });
659
651
 
660
652
  it("should error if a given binding has both preview and non-preview and --preview is not specified", async () => {
@@ -664,64 +656,46 @@ describe("wrangler", () => {
664
656
  "my-key",
665
657
  "my-value"
666
658
  );
667
- const { error, stderr, stdout } = await runWrangler(
668
- "kv:key put my-key my-value --binding someBinding"
669
- );
670
- expect(stdout).toMatchInlineSnapshot(`""`);
671
- expect(stderr).toMatchInlineSnapshot(
659
+ await expect(
660
+ runWrangler("kv:key put my-key my-value --binding someBinding")
661
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
672
662
  `"someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace."`
673
663
  );
674
- expect(error).toMatchInlineSnapshot(
675
- `[Error: someBinding has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.]`
676
- );
664
+ expect(std.out).toMatchInlineSnapshot(`""`);
665
+ expect(std.err).toMatchInlineSnapshot(`
666
+ "someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
667
+
668
+ %s
669
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
670
+ `);
677
671
  expect(requests.count).toEqual(0);
678
672
  });
679
673
  });
680
674
 
681
675
  describe("list", () => {
682
- function mockKeyListRequest(
683
- expectedNamespaceId: string,
684
- expectedKeys: string[],
685
- keysPerRequest = 1000
686
- ) {
687
- const requests = { count: 0 };
688
- setMockRawResponse(
689
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/keys",
690
- ([_url, accountId, namespaceId], _init, query) => {
691
- requests.count++;
692
- expect(accountId).toEqual("some-account-id");
693
- expect(namespaceId).toEqual(expectedNamespaceId);
694
- if (expectedKeys.length <= keysPerRequest) {
695
- return createFetchResult(expectedKeys);
696
- } else {
697
- const start = parseInt(query.get("cursor") ?? "0") || 0;
698
- const end = start + keysPerRequest;
699
- const cursor = end < expectedKeys.length ? end : undefined;
700
- return createFetchResult(
701
- expectedKeys.slice(start, end),
702
- true,
703
- [],
704
- [],
705
- { cursor }
706
- );
707
- }
708
- }
709
- );
710
- return requests;
711
- }
712
-
713
676
  it("should list the keys of a namespace specified by namespace-id", async () => {
714
677
  const keys = ["key-1", "key-2", "key-3"];
715
678
  mockKeyListRequest("some-namespace-id", keys);
716
- const { error, stdout, stderr } = await runWrangler(
717
- "kv:key list --namespace-id some-namespace-id"
718
- );
719
- expect(error).toMatchInlineSnapshot(`undefined`);
720
- expect(stderr).toMatchInlineSnapshot(`""`);
721
- expect(stdout).toMatchInlineSnapshot(`
722
- "key-1
723
- key-2
724
- key-3"
679
+ await runWrangler("kv:key list --namespace-id some-namespace-id");
680
+ expect(std.err).toMatchInlineSnapshot(`""`);
681
+ expect(std.out).toMatchInlineSnapshot(`
682
+ "[
683
+ {
684
+ \\"name\\": \\"key-1\\",
685
+ \\"expiration\\": 123456789,
686
+ \\"metadata\\": {}
687
+ },
688
+ {
689
+ \\"name\\": \\"key-2\\",
690
+ \\"expiration\\": 123456789,
691
+ \\"metadata\\": {}
692
+ },
693
+ {
694
+ \\"name\\": \\"key-3\\",
695
+ \\"expiration\\": 123456789,
696
+ \\"metadata\\": {}
697
+ }
698
+ ]"
725
699
  `);
726
700
  });
727
701
 
@@ -729,15 +703,27 @@ describe("wrangler", () => {
729
703
  writeWranglerConfig();
730
704
  const keys = ["key-1", "key-2", "key-3"];
731
705
  mockKeyListRequest("bound-id", keys);
732
- const { error, stdout, stderr } = await runWrangler(
733
- "kv:key list --binding someBinding"
734
- );
735
- expect(error).toMatchInlineSnapshot(`undefined`);
736
- expect(stderr).toMatchInlineSnapshot(`""`);
737
- expect(stdout).toMatchInlineSnapshot(`
738
- "key-1
739
- key-2
740
- key-3"
706
+
707
+ await runWrangler("kv:key list --binding someBinding");
708
+ expect(std.err).toMatchInlineSnapshot(`""`);
709
+ expect(std.out).toMatchInlineSnapshot(`
710
+ "[
711
+ {
712
+ \\"name\\": \\"key-1\\",
713
+ \\"expiration\\": 123456789,
714
+ \\"metadata\\": {}
715
+ },
716
+ {
717
+ \\"name\\": \\"key-2\\",
718
+ \\"expiration\\": 123456789,
719
+ \\"metadata\\": {}
720
+ },
721
+ {
722
+ \\"name\\": \\"key-3\\",
723
+ \\"expiration\\": 123456789,
724
+ \\"metadata\\": {}
725
+ }
726
+ ]"
741
727
  `);
742
728
  });
743
729
 
@@ -745,15 +731,26 @@ describe("wrangler", () => {
745
731
  writeWranglerConfig();
746
732
  const keys = ["key-1", "key-2", "key-3"];
747
733
  mockKeyListRequest("preview-bound-id", keys);
748
- const { error, stdout, stderr } = await runWrangler(
749
- "kv:key list --binding someBinding --preview"
750
- );
751
- expect(error).toMatchInlineSnapshot(`undefined`);
752
- expect(stderr).toMatchInlineSnapshot(`""`);
753
- expect(stdout).toMatchInlineSnapshot(`
754
- "key-1
755
- key-2
756
- key-3"
734
+ await runWrangler("kv:key list --binding someBinding --preview");
735
+ expect(std.err).toMatchInlineSnapshot(`""`);
736
+ expect(std.out).toMatchInlineSnapshot(`
737
+ "[
738
+ {
739
+ \\"name\\": \\"key-1\\",
740
+ \\"expiration\\": 123456789,
741
+ \\"metadata\\": {}
742
+ },
743
+ {
744
+ \\"name\\": \\"key-2\\",
745
+ \\"expiration\\": 123456789,
746
+ \\"metadata\\": {}
747
+ },
748
+ {
749
+ \\"name\\": \\"key-3\\",
750
+ \\"expiration\\": 123456789,
751
+ \\"metadata\\": {}
752
+ }
753
+ ]"
757
754
  `);
758
755
  });
759
756
 
@@ -761,15 +758,28 @@ describe("wrangler", () => {
761
758
  writeWranglerConfig();
762
759
  const keys = ["key-1", "key-2", "key-3"];
763
760
  mockKeyListRequest("env-bound-id", keys);
764
- const { error, stdout, stderr } = await runWrangler(
761
+ await runWrangler(
765
762
  "kv:key list --binding someBinding --env some-environment"
766
763
  );
767
- expect(error).toMatchInlineSnapshot(`undefined`);
768
- expect(stderr).toMatchInlineSnapshot(`""`);
769
- expect(stdout).toMatchInlineSnapshot(`
770
- "key-1
771
- key-2
772
- key-3"
764
+ expect(std.err).toMatchInlineSnapshot(`""`);
765
+ expect(std.out).toMatchInlineSnapshot(`
766
+ "[
767
+ {
768
+ \\"name\\": \\"key-1\\",
769
+ \\"expiration\\": 123456789,
770
+ \\"metadata\\": {}
771
+ },
772
+ {
773
+ \\"name\\": \\"key-2\\",
774
+ \\"expiration\\": 123456789,
775
+ \\"metadata\\": {}
776
+ },
777
+ {
778
+ \\"name\\": \\"key-3\\",
779
+ \\"expiration\\": 123456789,
780
+ \\"metadata\\": {}
781
+ }
782
+ ]"
773
783
  `);
774
784
  });
775
785
 
@@ -777,47 +787,77 @@ describe("wrangler", () => {
777
787
  writeWranglerConfig();
778
788
  const keys = ["key-1", "key-2", "key-3"];
779
789
  mockKeyListRequest("preview-env-bound-id", keys);
780
- const { error, stdout, stderr } = await runWrangler(
790
+ await runWrangler(
781
791
  "kv:key list --binding someBinding --preview --env some-environment"
782
792
  );
783
- expect(error).toMatchInlineSnapshot(`undefined`);
784
- expect(stderr).toMatchInlineSnapshot(`""`);
785
- expect(stdout).toMatchInlineSnapshot(`
786
- "key-1
787
- key-2
788
- key-3"
793
+ expect(std.err).toMatchInlineSnapshot(`""`);
794
+ expect(std.out).toMatchInlineSnapshot(`
795
+ "[
796
+ {
797
+ \\"name\\": \\"key-1\\",
798
+ \\"expiration\\": 123456789,
799
+ \\"metadata\\": {}
800
+ },
801
+ {
802
+ \\"name\\": \\"key-2\\",
803
+ \\"expiration\\": 123456789,
804
+ \\"metadata\\": {}
805
+ },
806
+ {
807
+ \\"name\\": \\"key-3\\",
808
+ \\"expiration\\": 123456789,
809
+ \\"metadata\\": {}
810
+ }
811
+ ]"
789
812
  `);
790
813
  });
791
814
 
792
- it("should make multiple requests for paginated results", async () => {
793
- // Create a lot of mock keys, so that the fetch requests will be paginated
794
- const keys: string[] = [];
795
- for (let i = 0; i < 550; i++) {
796
- keys.push("key-" + i);
797
- }
798
- // Ask for the keys in pages of size 100.
799
- const requests = mockKeyListRequest("some-namespace-id", keys, 100);
800
- const { stdout, stderr, error } = await runWrangler(
801
- "kv:key list --namespace-id some-namespace-id --limit 100"
802
- );
803
- expect(error).toMatchInlineSnapshot(`undefined`);
804
- expect(stderr).toMatchInlineSnapshot(`""`);
805
- expect(stdout).toEqual(keys.join("\n"));
806
- expect(requests.count).toEqual(6);
807
- });
815
+ // We'll run the next test with variations on the cursor
816
+ // that's returned on cloudflare's API after all results
817
+ // have been drained.
818
+ for (const blankCursorValue of [undefined, null, ""] as [
819
+ undefined,
820
+ null,
821
+ ""
822
+ ]) {
823
+ describe(`cursor - ${blankCursorValue}`, () => {
824
+ it("should make multiple requests for paginated results", async () => {
825
+ // Create a lot of mock keys, so that the fetch requests will be paginated
826
+ const keys: string[] = [];
827
+ for (let i = 0; i < 550; i++) {
828
+ keys.push("key-" + i);
829
+ }
830
+ // Ask for the keys in pages of size 100.
831
+ const requests = mockKeyListRequest(
832
+ "some-namespace-id",
833
+ keys,
834
+ 100,
835
+ blankCursorValue
836
+ );
837
+ await runWrangler(
838
+ "kv:key list --namespace-id some-namespace-id --limit 100"
839
+ );
840
+ expect(std.err).toMatchInlineSnapshot(`""`);
841
+ expect(JSON.parse(std.out).map((k) => k.name)).toEqual(keys);
842
+ expect(requests.count).toEqual(6);
843
+ });
844
+ });
845
+ }
808
846
 
809
847
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
810
848
  writeWranglerConfig();
811
- const { error, stdout, stderr } = await runWrangler(
812
- "kv:key list --binding otherBinding"
813
- );
814
- expect(error).toMatchInlineSnapshot(
815
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
816
- );
817
- expect(stderr).toMatchInlineSnapshot(
849
+ await expect(
850
+ runWrangler("kv:key list --binding otherBinding")
851
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
818
852
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
819
853
  );
820
- expect(stdout).toMatchInlineSnapshot(`""`);
854
+ expect(std.err).toMatchInlineSnapshot(`
855
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
856
+
857
+ %s
858
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
859
+ `);
860
+ expect(std.out).toMatchInlineSnapshot(`""`);
821
861
  });
822
862
  });
823
863
 
@@ -847,24 +887,20 @@ describe("wrangler", () => {
847
887
  "my-key",
848
888
  "my-value"
849
889
  );
850
- const { error, stderr, stdout } = await runWrangler(
851
- "kv:key get my-key --namespace-id some-namespace-id"
852
- );
853
- expect(stdout).toMatchInlineSnapshot(`"my-value"`);
854
- expect(stderr).toMatchInlineSnapshot(`""`);
855
- expect(error).toMatchInlineSnapshot(`undefined`);
890
+ await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
891
+ expect(std.out).toMatchInlineSnapshot(`"my-value"`);
892
+ expect(std.err).toMatchInlineSnapshot(`""`);
856
893
  expect(requests.count).toEqual(1);
857
894
  });
858
895
 
859
896
  it("should get a key in a given namespace specified by binding", async () => {
860
897
  writeWranglerConfig();
861
898
  const requests = mockKeyGetRequest("bound-id", "my-key", "my-value");
862
- const { error, stderr, stdout } = await runWrangler(
899
+ await runWrangler(
863
900
  "kv:key get my-key --binding someBinding --preview false"
864
901
  );
865
- expect(stdout).toMatchInlineSnapshot(`"my-value"`);
866
- expect(stderr).toMatchInlineSnapshot(`""`);
867
- expect(error).toMatchInlineSnapshot(`undefined`);
902
+ expect(std.out).toMatchInlineSnapshot(`"my-value"`);
903
+ expect(std.err).toMatchInlineSnapshot(`""`);
868
904
  expect(requests.count).toEqual(1);
869
905
  });
870
906
 
@@ -875,12 +911,9 @@ describe("wrangler", () => {
875
911
  "my-key",
876
912
  "my-value"
877
913
  );
878
- const { error, stderr, stdout } = await runWrangler(
879
- "kv:key get my-key --binding someBinding --preview"
880
- );
881
- expect(stdout).toMatchInlineSnapshot(`"my-value"`);
882
- expect(stderr).toMatchInlineSnapshot(`""`);
883
- expect(error).toMatchInlineSnapshot(`undefined`);
914
+ await runWrangler("kv:key get my-key --binding someBinding --preview");
915
+ expect(std.out).toMatchInlineSnapshot(`"my-value"`);
916
+ expect(std.err).toMatchInlineSnapshot(`""`);
884
917
  expect(requests.count).toEqual(1);
885
918
  });
886
919
 
@@ -891,20 +924,22 @@ describe("wrangler", () => {
891
924
  "my-key",
892
925
  "my-value"
893
926
  );
894
- const { error, stderr, stdout } = await runWrangler(
927
+ await runWrangler(
895
928
  "kv:key get my-key my-value --binding someBinding --env some-environment --preview false"
896
929
  );
897
- expect(stdout).toMatchInlineSnapshot(`"my-value"`);
898
- expect(stderr).toMatchInlineSnapshot(`""`);
899
- expect(error).toMatchInlineSnapshot(`undefined`);
930
+ expect(std.out).toMatchInlineSnapshot(`"my-value"`);
931
+ expect(std.err).toMatchInlineSnapshot(`""`);
900
932
  expect(requests.count).toEqual(1);
901
933
  });
902
934
 
903
935
  it("should error if no key is provided", async () => {
904
- const { error, stdout, stderr } = await runWrangler("kv:key get");
905
-
906
- expect(stdout).toMatchInlineSnapshot(`""`);
907
- expect(stderr).toMatchInlineSnapshot(`
936
+ await expect(
937
+ runWrangler("kv:key get")
938
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
939
+ `"Not enough non-option arguments: got 0, need at least 1"`
940
+ );
941
+ expect(std.out).toMatchInlineSnapshot(`""`);
942
+ expect(std.err).toMatchInlineSnapshot(`
908
943
  "wrangler kv:key get <key>
909
944
 
910
945
  Reads a single value by key from the given namespace.
@@ -926,16 +961,16 @@ describe("wrangler", () => {
926
961
 
927
962
  Not enough non-option arguments: got 0, need at least 1"
928
963
  `);
929
- expect(error).toMatchInlineSnapshot(
930
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
931
- );
932
964
  });
933
965
 
934
966
  it("should error if no binding nor namespace is provided", async () => {
935
- const { error, stdout, stderr } = await runWrangler("kv:key get foo");
936
-
937
- expect(stdout).toMatchInlineSnapshot(`""`);
938
- expect(stderr).toMatchInlineSnapshot(`
967
+ await expect(
968
+ runWrangler("kv:key get foo")
969
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
970
+ `"Exactly one of the arguments binding and namespace-id is required"`
971
+ );
972
+ expect(std.out).toMatchInlineSnapshot(`""`);
973
+ expect(std.err).toMatchInlineSnapshot(`
939
974
  "wrangler kv:key get <key>
940
975
 
941
976
  Reads a single value by key from the given namespace.
@@ -957,18 +992,17 @@ describe("wrangler", () => {
957
992
 
958
993
  Exactly one of the arguments binding and namespace-id is required"
959
994
  `);
960
- expect(error).toMatchInlineSnapshot(
961
- `[Error: Exactly one of the arguments binding and namespace-id is required]`
962
- );
963
995
  });
964
996
 
965
997
  it("should error if both binding and namespace is provided", async () => {
966
- const { error, stdout, stderr } = await runWrangler(
967
- "kv:key get foo --binding x --namespace-id y"
998
+ await expect(
999
+ runWrangler("kv:key get foo --binding x --namespace-id y")
1000
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1001
+ `"Arguments binding and namespace-id are mutually exclusive"`
968
1002
  );
969
1003
 
970
- expect(stdout).toMatchInlineSnapshot(`""`);
971
- expect(stderr).toMatchInlineSnapshot(`
1004
+ expect(std.out).toMatchInlineSnapshot(`""`);
1005
+ expect(std.err).toMatchInlineSnapshot(`
972
1006
  "wrangler kv:key get <key>
973
1007
 
974
1008
  Reads a single value by key from the given namespace.
@@ -990,23 +1024,22 @@ describe("wrangler", () => {
990
1024
 
991
1025
  Arguments binding and namespace-id are mutually exclusive"
992
1026
  `);
993
- expect(error).toMatchInlineSnapshot(
994
- `[Error: Arguments binding and namespace-id are mutually exclusive]`
995
- );
996
1027
  });
997
1028
 
998
1029
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
999
1030
  writeWranglerConfig();
1000
- const { error, stdout, stderr } = await runWrangler(
1001
- `kv:key get key --binding otherBinding`
1002
- );
1003
- expect(stdout).toMatchInlineSnapshot(`""`);
1004
- expect(stderr).toMatchInlineSnapshot(
1031
+ await expect(
1032
+ runWrangler("kv:key get key --binding otherBinding")
1033
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1005
1034
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1006
1035
  );
1007
- expect(error).toMatchInlineSnapshot(
1008
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
1009
- );
1036
+ expect(std.out).toMatchInlineSnapshot(`""`);
1037
+ expect(std.err).toMatchInlineSnapshot(`
1038
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1039
+
1040
+ %s
1041
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1042
+ `);
1010
1043
  });
1011
1044
  });
1012
1045
 
@@ -1058,25 +1091,30 @@ describe("wrangler", () => {
1058
1091
 
1059
1092
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
1060
1093
  writeWranglerConfig();
1061
- const { stderr } = await runWrangler(
1062
- `kv:key delete --binding otherBinding someKey`
1063
- );
1064
- expect(stderr).toMatchInlineSnapshot(
1094
+ await expect(
1095
+ runWrangler(`kv:key delete --binding otherBinding someKey`)
1096
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1065
1097
  `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1066
1098
  );
1099
+
1100
+ expect(std.err).toMatchInlineSnapshot(`
1101
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1102
+
1103
+ %s
1104
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1105
+ `);
1067
1106
  });
1068
1107
 
1069
1108
  it("should delete a key in a namespace specified by binding name in a given environment", async () => {
1070
1109
  writeWranglerConfig();
1071
1110
  const requests = mockDeleteRequest("env-bound-id", "someKey");
1072
- const { stdout, stderr, error } = await runWrangler(
1111
+ await runWrangler(
1073
1112
  `kv:key delete --binding someBinding --env some-environment --preview false someKey`
1074
1113
  );
1075
- expect(stdout).toMatchInlineSnapshot(
1114
+ expect(std.out).toMatchInlineSnapshot(
1076
1115
  `"deleting the key \\"someKey\\" on namespace env-bound-id"`
1077
1116
  );
1078
- expect(stderr).toMatchInlineSnapshot(`""`);
1079
- expect(error).toMatchInlineSnapshot(`undefined`);
1117
+ expect(std.err).toMatchInlineSnapshot(`""`);
1080
1118
  expect(requests.count).toEqual(1);
1081
1119
  });
1082
1120