wrangler 0.0.13 → 0.0.17

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 (67) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +20 -11
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +112 -28
  5. package/pages/functions/filepath-routing.ts +44 -51
  6. package/pages/functions/routes.ts +11 -18
  7. package/pages/functions/template-worker.ts +3 -9
  8. package/src/__tests__/dev.test.tsx +42 -5
  9. package/src/__tests__/guess-worker-format.test.ts +66 -0
  10. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  11. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  12. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  13. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  14. package/src/__tests__/helpers/mock-bin.ts +36 -0
  15. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +43 -9
  16. package/src/__tests__/helpers/mock-console.ts +62 -0
  17. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  18. package/src/__tests__/helpers/mock-kv.ts +40 -0
  19. package/src/__tests__/helpers/mock-user.ts +27 -0
  20. package/src/__tests__/helpers/mock-web-socket.ts +37 -0
  21. package/src/__tests__/{run-in-tmp.ts → helpers/run-in-tmp.ts} +1 -1
  22. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  23. package/src/__tests__/helpers/write-wrangler-toml.ts +20 -0
  24. package/src/__tests__/index.test.ts +418 -71
  25. package/src/__tests__/jest.setup.ts +30 -2
  26. package/src/__tests__/kv.test.ts +147 -252
  27. package/src/__tests__/logout.test.ts +50 -0
  28. package/src/__tests__/package-manager.test.ts +206 -0
  29. package/src/__tests__/publish.test.ts +1136 -291
  30. package/src/__tests__/r2.test.ts +206 -0
  31. package/src/__tests__/secret.test.ts +210 -0
  32. package/src/__tests__/sentry.test.ts +146 -0
  33. package/src/__tests__/tail.test.ts +246 -0
  34. package/src/__tests__/whoami.test.tsx +6 -47
  35. package/src/api/form_data.ts +75 -25
  36. package/src/api/preview.ts +2 -2
  37. package/src/api/worker.ts +34 -15
  38. package/src/bundle.ts +127 -0
  39. package/src/cfetch/index.ts +7 -15
  40. package/src/cfetch/internal.ts +41 -6
  41. package/src/cli.ts +10 -0
  42. package/src/config.ts +125 -95
  43. package/src/dev.tsx +300 -193
  44. package/src/dialogs.tsx +2 -2
  45. package/src/guess-worker-format.ts +68 -0
  46. package/src/index.tsx +578 -192
  47. package/src/inspect.ts +29 -10
  48. package/src/kv.tsx +23 -17
  49. package/src/module-collection.ts +32 -12
  50. package/src/open-in-browser.ts +13 -0
  51. package/src/package-manager.ts +120 -0
  52. package/src/pages.tsx +28 -23
  53. package/src/paths.ts +26 -0
  54. package/src/proxy.ts +88 -14
  55. package/src/publish.ts +260 -297
  56. package/src/r2.ts +50 -0
  57. package/src/reporting.ts +115 -0
  58. package/src/sites.tsx +28 -27
  59. package/src/tail.tsx +178 -9
  60. package/src/user.tsx +58 -44
  61. package/templates/new-worker.js +15 -0
  62. package/templates/new-worker.ts +15 -0
  63. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  64. package/wrangler-dist/cli.js +124315 -104677
  65. package/wrangler-dist/cli.js.map +3 -3
  66. package/src/__tests__/mock-console.ts +0 -34
  67. package/src/__tests__/run-wrangler.ts +0 -8
@@ -1,16 +1,20 @@
1
- import { writeFileSync } from "fs";
2
- import type { KVNamespaceInfo } from "../kv";
1
+ import { writeFileSync } from "node:fs";
2
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
3
3
  import {
4
4
  setMockResponse,
5
- setMockRawResponse,
6
5
  unsetAllMocks,
7
- createFetchResult,
8
- } from "./mock-cfetch";
9
- import { runWrangler } from "./run-wrangler";
10
- import { runInTempDir } from "./run-in-tmp";
11
- import { mockConsoleMethods } from "./mock-console";
6
+ unsetMockFetchKVGetValues,
7
+ setMockFetchKVGetValue,
8
+ } from "./helpers/mock-cfetch";
9
+ import { mockConsoleMethods } from "./helpers/mock-console";
10
+ import { mockKeyListRequest } from "./helpers/mock-kv";
11
+ import { runInTempDir } from "./helpers/run-in-tmp";
12
+ import { runWrangler } from "./helpers/run-wrangler";
13
+ import type { KVNamespaceInfo, NamespaceKeyInfo } from "../kv";
12
14
 
13
15
  describe("wrangler", () => {
16
+ mockAccountId();
17
+ mockApiToken();
14
18
  runInTempDir();
15
19
  const std = mockConsoleMethods();
16
20
 
@@ -34,12 +38,11 @@ describe("wrangler", () => {
34
38
  }
35
39
 
36
40
  it("should error if no namespace is given", async () => {
37
- let error: Error | undefined;
38
- try {
39
- await runWrangler("kv:namespace create");
40
- } catch (e) {
41
- error = e;
42
- }
41
+ await expect(
42
+ runWrangler("kv:namespace create")
43
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
44
+ `"Not enough non-option arguments: got 0, need at least 1"`
45
+ );
43
46
  expect(std.out).toMatchInlineSnapshot(`""`);
44
47
  expect(std.err).toMatchInlineSnapshot(`
45
48
  "wrangler kv:namespace create <namespace>
@@ -61,18 +64,14 @@ describe("wrangler", () => {
61
64
 
62
65
  Not enough non-option arguments: got 0, need at least 1"
63
66
  `);
64
- expect(error).toMatchInlineSnapshot(
65
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
66
- );
67
67
  });
68
68
 
69
69
  it("should error if the namespace to create contains spaces", async () => {
70
- let error: Error | undefined;
71
- try {
72
- await runWrangler("kv:namespace create abc def ghi");
73
- } catch (e) {
74
- error = e;
75
- }
70
+ await expect(
71
+ runWrangler("kv:namespace create abc def ghi")
72
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
73
+ `"Unexpected additional positional arguments \\"def ghi\\"."`
74
+ );
76
75
  expect(std.out).toMatchInlineSnapshot(`""`);
77
76
  expect(std.err).toMatchInlineSnapshot(`
78
77
  "wrangler kv:namespace create <namespace>
@@ -94,18 +93,14 @@ describe("wrangler", () => {
94
93
 
95
94
  Unexpected additional positional arguments \\"def ghi\\"."
96
95
  `);
97
- expect(error).toMatchInlineSnapshot(
98
- `[Error: Unexpected additional positional arguments "def ghi".]`
99
- );
100
96
  });
101
97
 
102
98
  it("should error if the namespace to create is not valid", async () => {
103
- let error: Error | undefined;
104
- try {
105
- await runWrangler("kv:namespace create abc-def");
106
- } catch (e) {
107
- error = e;
108
- }
99
+ await expect(
100
+ runWrangler("kv:namespace create abc-def")
101
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
102
+ `"The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."`
103
+ );
109
104
 
110
105
  expect(std.out).toMatchInlineSnapshot(`""`);
111
106
  expect(std.err).toMatchInlineSnapshot(`
@@ -128,9 +123,6 @@ describe("wrangler", () => {
128
123
 
129
124
  The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."
130
125
  `);
131
- expect(error).toMatchInlineSnapshot(
132
- `[Error: The namespace binding name "abc-def" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.]`
133
- );
134
126
  });
135
127
 
136
128
  it("should create a namespace", async () => {
@@ -273,16 +265,11 @@ describe("wrangler", () => {
273
265
 
274
266
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
275
267
  writeWranglerConfig();
276
- let error: Error | undefined;
277
- try {
278
- await runWrangler("kv:namespace delete --binding otherBinding");
279
- } catch (e) {
280
- error = e;
281
- }
282
- expect(error).toMatchInlineSnapshot(`
283
- [Error: Not able to delete namespace.
284
- A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]
285
- `);
268
+ await expect(runWrangler("kv:namespace delete --binding otherBinding"))
269
+ .rejects.toThrowErrorMatchingInlineSnapshot(`
270
+ "Not able to delete namespace.
271
+ A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
272
+ `);
286
273
  expect(std.err).toMatchInlineSnapshot(`
287
274
  "wrangler kv:namespace delete
288
275
 
@@ -422,21 +409,14 @@ describe("wrangler", () => {
422
409
  10,
423
410
  20
424
411
  );
425
- let error: Error | undefined;
426
- try {
427
- await runWrangler(
428
- "kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
429
- );
430
- } catch (e) {
431
- error = e;
432
- }
433
-
412
+ await runWrangler(
413
+ "kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
414
+ );
434
415
  expect(requests.count).toEqual(1);
435
416
  expect(std.out).toMatchInlineSnapshot(
436
417
  `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
437
418
  );
438
419
  expect(std.err).toMatchInlineSnapshot(`""`);
439
- expect(error).toMatchInlineSnapshot(`undefined`);
440
420
  });
441
421
 
442
422
  it("should put a key to the specified environment in a given namespace", async () => {
@@ -474,12 +454,11 @@ describe("wrangler", () => {
474
454
  });
475
455
 
476
456
  it("should error if no key is provided", async () => {
477
- let error: Error | undefined;
478
- try {
479
- await runWrangler("kv:key put");
480
- } catch (e) {
481
- error = e;
482
- }
457
+ await expect(
458
+ runWrangler("kv:key put")
459
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
460
+ `"Not enough non-option arguments: got 0, need at least 1"`
461
+ );
483
462
 
484
463
  expect(std.out).toMatchInlineSnapshot(`""`);
485
464
  expect(std.err).toMatchInlineSnapshot(`
@@ -508,18 +487,14 @@ describe("wrangler", () => {
508
487
 
509
488
  Not enough non-option arguments: got 0, need at least 1"
510
489
  `);
511
- expect(error).toMatchInlineSnapshot(
512
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
513
- );
514
490
  });
515
491
 
516
492
  it("should error if no binding nor namespace is provided", async () => {
517
- let error: Error | undefined;
518
- try {
519
- await runWrangler("kv:key put foo bar");
520
- } catch (e) {
521
- error = e;
522
- }
493
+ await expect(
494
+ runWrangler("kv:key put foo bar")
495
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
496
+ `"Exactly one of the arguments binding and namespace-id is required"`
497
+ );
523
498
 
524
499
  expect(std.out).toMatchInlineSnapshot(`""`);
525
500
  expect(std.err).toMatchInlineSnapshot(`
@@ -548,18 +523,14 @@ describe("wrangler", () => {
548
523
 
549
524
  Exactly one of the arguments binding and namespace-id is required"
550
525
  `);
551
- expect(error).toMatchInlineSnapshot(
552
- `[Error: Exactly one of the arguments binding and namespace-id is required]`
553
- );
554
526
  });
555
527
 
556
528
  it("should error if both binding and namespace is provided", async () => {
557
- let error: Error | undefined;
558
- try {
559
- await runWrangler("kv:key put foo bar --binding x --namespace-id y");
560
- } catch (e) {
561
- error = e;
562
- }
529
+ await expect(
530
+ runWrangler("kv:key put foo bar --binding x --namespace-id y")
531
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
532
+ `"Arguments binding and namespace-id are mutually exclusive"`
533
+ );
563
534
 
564
535
  expect(std.out).toMatchInlineSnapshot(`""`);
565
536
  expect(std.err).toMatchInlineSnapshot(`
@@ -588,18 +559,14 @@ describe("wrangler", () => {
588
559
 
589
560
  Arguments binding and namespace-id are mutually exclusive"
590
561
  `);
591
- expect(error).toMatchInlineSnapshot(
592
- `[Error: Arguments binding and namespace-id are mutually exclusive]`
593
- );
594
562
  });
595
563
 
596
564
  it("should error if no value nor path is provided", async () => {
597
- let error: Error | undefined;
598
- try {
599
- await runWrangler("kv:key put key --namespace-id 12345");
600
- } catch (e) {
601
- error = e;
602
- }
565
+ await expect(
566
+ runWrangler("kv:key put key --namespace-id 12345")
567
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
568
+ `"Exactly one of the arguments value and path is required"`
569
+ );
603
570
 
604
571
  expect(std.out).toMatchInlineSnapshot(`""`);
605
572
  expect(std.err).toMatchInlineSnapshot(`
@@ -628,20 +595,14 @@ describe("wrangler", () => {
628
595
 
629
596
  Exactly one of the arguments value and path is required"
630
597
  `);
631
- expect(error).toMatchInlineSnapshot(
632
- `[Error: Exactly one of the arguments value and path is required]`
633
- );
634
598
  });
635
599
 
636
600
  it("should error if both value and path is provided", async () => {
637
- let error: Error | undefined;
638
- try {
639
- await runWrangler(
640
- "kv:key put key value --path xyz --namespace-id 12345"
641
- );
642
- } catch (e) {
643
- error = e;
644
- }
601
+ await expect(
602
+ runWrangler("kv:key put key value --path xyz --namespace-id 12345")
603
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
604
+ `"Arguments value and path are mutually exclusive"`
605
+ );
645
606
 
646
607
  expect(std.out).toMatchInlineSnapshot(`""`);
647
608
  expect(std.err).toMatchInlineSnapshot(`
@@ -670,19 +631,15 @@ describe("wrangler", () => {
670
631
 
671
632
  Arguments value and path are mutually exclusive"
672
633
  `);
673
- expect(error).toMatchInlineSnapshot(
674
- `[Error: Arguments value and path are mutually exclusive]`
675
- );
676
634
  });
677
635
 
678
636
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
679
637
  writeWranglerConfig();
680
- let error: Error | undefined;
681
- try {
682
- await runWrangler("kv:key put key value --binding otherBinding");
683
- } catch (e) {
684
- error = e;
685
- }
638
+ await expect(
639
+ runWrangler("kv:key put key value --binding otherBinding")
640
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
641
+ `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
642
+ );
686
643
 
687
644
  expect(std.out).toMatchInlineSnapshot(`""`);
688
645
  expect(std.err).toMatchInlineSnapshot(`
@@ -691,9 +648,6 @@ describe("wrangler", () => {
691
648
  %s
692
649
  If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
693
650
  `);
694
- expect(error).toMatchInlineSnapshot(
695
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
696
- );
697
651
  });
698
652
 
699
653
  it("should error if a given binding has both preview and non-preview and --preview is not specified", async () => {
@@ -703,12 +657,11 @@ describe("wrangler", () => {
703
657
  "my-key",
704
658
  "my-value"
705
659
  );
706
- let error: Error | undefined;
707
- try {
708
- await runWrangler("kv:key put my-key my-value --binding someBinding");
709
- } catch (e) {
710
- error = e;
711
- }
660
+ await expect(
661
+ runWrangler("kv:key put my-key my-value --binding someBinding")
662
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
663
+ `"someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace."`
664
+ );
712
665
  expect(std.out).toMatchInlineSnapshot(`""`);
713
666
  expect(std.err).toMatchInlineSnapshot(`
714
667
  "someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
@@ -716,9 +669,6 @@ describe("wrangler", () => {
716
669
  %s
717
670
  If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
718
671
  `);
719
- expect(error).toMatchInlineSnapshot(
720
- `[Error: someBinding has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.]`
721
- );
722
672
  expect(requests.count).toEqual(0);
723
673
  });
724
674
  });
@@ -863,32 +813,46 @@ describe("wrangler", () => {
863
813
  `);
864
814
  });
865
815
 
866
- it("should make multiple requests for paginated results", async () => {
867
- // Create a lot of mock keys, so that the fetch requests will be paginated
868
- const keys: string[] = [];
869
- for (let i = 0; i < 550; i++) {
870
- keys.push("key-" + i);
871
- }
872
- // Ask for the keys in pages of size 100.
873
- const requests = mockKeyListRequest("some-namespace-id", keys, 100);
874
- await runWrangler(
875
- "kv:key list --namespace-id some-namespace-id --limit 100"
876
- );
877
- expect(std.err).toMatchInlineSnapshot(`""`);
878
- expect(JSON.parse(std.out).map((k) => k.name)).toEqual(keys);
879
- expect(requests.count).toEqual(6);
880
- });
816
+ // We'll run the next test with variations on the cursor
817
+ // that's returned on cloudflare's API after all results
818
+ // have been drained.
819
+ for (const blankCursorValue of [undefined, null, ""] as [
820
+ undefined,
821
+ null,
822
+ ""
823
+ ]) {
824
+ describe(`cursor - ${blankCursorValue}`, () => {
825
+ it("should make multiple requests for paginated results", async () => {
826
+ // Create a lot of mock keys, so that the fetch requests will be paginated
827
+ const keys: string[] = [];
828
+ for (let i = 0; i < 550; i++) {
829
+ keys.push("key-" + i);
830
+ }
831
+ // Ask for the keys in pages of size 100.
832
+ const requests = mockKeyListRequest(
833
+ "some-namespace-id",
834
+ keys,
835
+ 100,
836
+ blankCursorValue
837
+ );
838
+ await runWrangler(
839
+ "kv:key list --namespace-id some-namespace-id --limit 100"
840
+ );
841
+ expect(std.err).toMatchInlineSnapshot(`""`);
842
+ expect(
843
+ JSON.parse(std.out).map((k: NamespaceKeyInfo) => k.name)
844
+ ).toEqual(keys);
845
+ expect(requests.count).toEqual(6);
846
+ });
847
+ });
848
+ }
881
849
 
882
850
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
883
851
  writeWranglerConfig();
884
- let error: Error | undefined;
885
- try {
886
- await runWrangler("kv:key list --binding otherBinding");
887
- } catch (e) {
888
- error = e;
889
- }
890
- expect(error).toMatchInlineSnapshot(
891
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
852
+ await expect(
853
+ runWrangler("kv:key list --binding otherBinding")
854
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
855
+ `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
892
856
  );
893
857
  expect(std.err).toMatchInlineSnapshot(`
894
858
  "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
@@ -901,27 +865,13 @@ describe("wrangler", () => {
901
865
  });
902
866
 
903
867
  describe("get", () => {
904
- function mockKeyGetRequest(
905
- expectedNamespaceId: string,
906
- expectedKey: string,
907
- expectedValue: string
908
- ) {
909
- const requests = { count: 0 };
910
- setMockRawResponse(
911
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
912
- ([_url, accountId, namespaceId, key]) => {
913
- requests.count++;
914
- expect(accountId).toEqual("some-account-id");
915
- expect(namespaceId).toEqual(expectedNamespaceId);
916
- expect(key).toEqual(expectedKey);
917
- return expectedValue;
918
- }
919
- );
920
- return requests;
921
- }
868
+ afterEach(() => {
869
+ unsetMockFetchKVGetValues();
870
+ });
922
871
 
923
872
  it("should get a key in a given namespace specified by namespace-id", async () => {
924
- const requests = mockKeyGetRequest(
873
+ setMockFetchKVGetValue(
874
+ "some-account-id",
925
875
  "some-namespace-id",
926
876
  "my-key",
927
877
  "my-value"
@@ -929,23 +879,27 @@ describe("wrangler", () => {
929
879
  await runWrangler("kv:key get my-key --namespace-id some-namespace-id");
930
880
  expect(std.out).toMatchInlineSnapshot(`"my-value"`);
931
881
  expect(std.err).toMatchInlineSnapshot(`""`);
932
- expect(requests.count).toEqual(1);
933
882
  });
934
883
 
935
884
  it("should get a key in a given namespace specified by binding", async () => {
936
885
  writeWranglerConfig();
937
- const requests = mockKeyGetRequest("bound-id", "my-key", "my-value");
886
+ setMockFetchKVGetValue(
887
+ "some-account-id",
888
+ "bound-id",
889
+ "my-key",
890
+ "my-value"
891
+ );
938
892
  await runWrangler(
939
893
  "kv:key get my-key --binding someBinding --preview false"
940
894
  );
941
895
  expect(std.out).toMatchInlineSnapshot(`"my-value"`);
942
896
  expect(std.err).toMatchInlineSnapshot(`""`);
943
- expect(requests.count).toEqual(1);
944
897
  });
945
898
 
946
899
  it("should get a key in a given preview namespace specified by binding", async () => {
947
900
  writeWranglerConfig();
948
- const requests = mockKeyGetRequest(
901
+ setMockFetchKVGetValue(
902
+ "some-account-id",
949
903
  "preview-bound-id",
950
904
  "my-key",
951
905
  "my-value"
@@ -953,12 +907,12 @@ describe("wrangler", () => {
953
907
  await runWrangler("kv:key get my-key --binding someBinding --preview");
954
908
  expect(std.out).toMatchInlineSnapshot(`"my-value"`);
955
909
  expect(std.err).toMatchInlineSnapshot(`""`);
956
- expect(requests.count).toEqual(1);
957
910
  });
958
911
 
959
912
  it("should get a key for the specified environment in a given namespace", async () => {
960
913
  writeWranglerConfig();
961
- const requests = mockKeyGetRequest(
914
+ setMockFetchKVGetValue(
915
+ "some-account-id",
962
916
  "env-bound-id",
963
917
  "my-key",
964
918
  "my-value"
@@ -968,16 +922,14 @@ describe("wrangler", () => {
968
922
  );
969
923
  expect(std.out).toMatchInlineSnapshot(`"my-value"`);
970
924
  expect(std.err).toMatchInlineSnapshot(`""`);
971
- expect(requests.count).toEqual(1);
972
925
  });
973
926
 
974
927
  it("should error if no key is provided", async () => {
975
- let error: Error | undefined;
976
- try {
977
- await runWrangler("kv:key get");
978
- } catch (e) {
979
- error = e;
980
- }
928
+ await expect(
929
+ runWrangler("kv:key get")
930
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
931
+ `"Not enough non-option arguments: got 0, need at least 1"`
932
+ );
981
933
  expect(std.out).toMatchInlineSnapshot(`""`);
982
934
  expect(std.err).toMatchInlineSnapshot(`
983
935
  "wrangler kv:key get <key>
@@ -1001,18 +953,14 @@ describe("wrangler", () => {
1001
953
 
1002
954
  Not enough non-option arguments: got 0, need at least 1"
1003
955
  `);
1004
- expect(error).toMatchInlineSnapshot(
1005
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
1006
- );
1007
956
  });
1008
957
 
1009
958
  it("should error if no binding nor namespace is provided", async () => {
1010
- let error: Error | undefined;
1011
- try {
1012
- await runWrangler("kv:key get foo");
1013
- } catch (e) {
1014
- error = e;
1015
- }
959
+ await expect(
960
+ runWrangler("kv:key get foo")
961
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
962
+ `"Exactly one of the arguments binding and namespace-id is required"`
963
+ );
1016
964
  expect(std.out).toMatchInlineSnapshot(`""`);
1017
965
  expect(std.err).toMatchInlineSnapshot(`
1018
966
  "wrangler kv:key get <key>
@@ -1036,18 +984,14 @@ describe("wrangler", () => {
1036
984
 
1037
985
  Exactly one of the arguments binding and namespace-id is required"
1038
986
  `);
1039
- expect(error).toMatchInlineSnapshot(
1040
- `[Error: Exactly one of the arguments binding and namespace-id is required]`
1041
- );
1042
987
  });
1043
988
 
1044
989
  it("should error if both binding and namespace is provided", async () => {
1045
- let error: Error | undefined;
1046
- try {
1047
- await runWrangler("kv:key get foo --binding x --namespace-id y");
1048
- } catch (e) {
1049
- error = e;
1050
- }
990
+ await expect(
991
+ runWrangler("kv:key get foo --binding x --namespace-id y")
992
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
993
+ `"Arguments binding and namespace-id are mutually exclusive"`
994
+ );
1051
995
 
1052
996
  expect(std.out).toMatchInlineSnapshot(`""`);
1053
997
  expect(std.err).toMatchInlineSnapshot(`
@@ -1072,19 +1016,15 @@ describe("wrangler", () => {
1072
1016
 
1073
1017
  Arguments binding and namespace-id are mutually exclusive"
1074
1018
  `);
1075
- expect(error).toMatchInlineSnapshot(
1076
- `[Error: Arguments binding and namespace-id are mutually exclusive]`
1077
- );
1078
1019
  });
1079
1020
 
1080
1021
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
1081
1022
  writeWranglerConfig();
1082
- let error: Error | undefined;
1083
- try {
1084
- await runWrangler("kv:key get key --binding otherBinding");
1085
- } catch (e) {
1086
- error = e;
1087
- }
1023
+ await expect(
1024
+ runWrangler("kv:key get key --binding otherBinding")
1025
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1026
+ `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1027
+ );
1088
1028
  expect(std.out).toMatchInlineSnapshot(`""`);
1089
1029
  expect(std.err).toMatchInlineSnapshot(`
1090
1030
  "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
@@ -1092,9 +1032,6 @@ describe("wrangler", () => {
1092
1032
  %s
1093
1033
  If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1094
1034
  `);
1095
- expect(error).toMatchInlineSnapshot(
1096
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
1097
- );
1098
1035
  });
1099
1036
  });
1100
1037
 
@@ -1146,12 +1083,11 @@ describe("wrangler", () => {
1146
1083
 
1147
1084
  it("should error if a given binding name is not in the configured kv namespaces", async () => {
1148
1085
  writeWranglerConfig();
1149
- let error: Error | undefined;
1150
- try {
1151
- await runWrangler(`kv:key delete --binding otherBinding someKey`);
1152
- } catch (e) {
1153
- error = e;
1154
- }
1086
+ await expect(
1087
+ runWrangler(`kv:key delete --binding otherBinding someKey`)
1088
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1089
+ `"A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."`
1090
+ );
1155
1091
 
1156
1092
  expect(std.err).toMatchInlineSnapshot(`
1157
1093
  "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
@@ -1159,9 +1095,6 @@ describe("wrangler", () => {
1159
1095
  %s
1160
1096
  If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1161
1097
  `);
1162
- expect(error).toMatchInlineSnapshot(
1163
- `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
1164
- );
1165
1098
  });
1166
1099
 
1167
1100
  it("should delete a key in a namespace specified by binding name in a given environment", async () => {
@@ -1206,41 +1139,3 @@ function writeWranglerConfig() {
1206
1139
  "utf-8"
1207
1140
  );
1208
1141
  }
1209
-
1210
- export function mockKeyListRequest(
1211
- expectedNamespaceId: string,
1212
- expectedKeys: string[],
1213
- keysPerRequest = 1000
1214
- ) {
1215
- const requests = { count: 0 };
1216
- // See https://api.cloudflare.com/#workers-kv-namespace-list-a-namespace-s-keys
1217
- const expectedKeyObjects = expectedKeys.map((name) => ({
1218
- name,
1219
- expiration: 123456789,
1220
- metadata: {},
1221
- }));
1222
- setMockRawResponse(
1223
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/keys",
1224
- "GET",
1225
- ([_url, accountId, namespaceId], _init, query) => {
1226
- requests.count++;
1227
- expect(accountId).toEqual("some-account-id");
1228
- expect(namespaceId).toEqual(expectedNamespaceId);
1229
- if (expectedKeyObjects.length <= keysPerRequest) {
1230
- return createFetchResult(expectedKeyObjects);
1231
- } else {
1232
- const start = parseInt(query.get("cursor") ?? "0") || 0;
1233
- const end = start + keysPerRequest;
1234
- const cursor = end < expectedKeyObjects.length ? end : undefined;
1235
- return createFetchResult(
1236
- expectedKeyObjects.slice(start, end),
1237
- true,
1238
- [],
1239
- [],
1240
- { cursor }
1241
- );
1242
- }
1243
- }
1244
- );
1245
- return requests;
1246
- }