wrangler 0.0.0-e2a26a0 → 0.0.0-e2b7352

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 (91) hide show
  1. package/README.md +3 -3
  2. package/bin/wrangler.js +10 -1
  3. package/miniflare-dist/index.mjs +79 -0
  4. package/miniflare-dist/index.mjs.map +7 -0
  5. package/package.json +24 -12
  6. package/pages/functions/filepath-routing.test.ts +212 -36
  7. package/pages/functions/filepath-routing.ts +69 -119
  8. package/pages/functions/routes.ts +8 -17
  9. package/pages/functions/template-worker.ts +3 -9
  10. package/src/__tests__/configuration.test.ts +2820 -0
  11. package/src/__tests__/dev.test.tsx +535 -65
  12. package/src/__tests__/guess-worker-format.test.ts +68 -0
  13. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  14. package/src/__tests__/helpers/mock-account-id.ts +26 -8
  15. package/src/__tests__/helpers/mock-bin.ts +4 -1
  16. package/src/__tests__/helpers/mock-cfetch.ts +5 -5
  17. package/src/__tests__/helpers/mock-console.ts +21 -7
  18. package/src/__tests__/helpers/mock-kv.ts +9 -16
  19. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  20. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  21. package/src/__tests__/helpers/run-in-tmp.ts +3 -0
  22. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  23. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  24. package/src/__tests__/https-options.test.ts +127 -0
  25. package/src/__tests__/index.test.ts +467 -27
  26. package/src/__tests__/jest.setup.ts +37 -5
  27. package/src/__tests__/kv.test.ts +442 -201
  28. package/src/__tests__/logout.test.ts +15 -13
  29. package/src/__tests__/package-manager.test.ts +2 -2
  30. package/src/__tests__/pages.test.ts +82 -0
  31. package/src/__tests__/parse.test.ts +322 -0
  32. package/src/__tests__/publish.test.ts +2717 -430
  33. package/src/__tests__/r2.test.ts +198 -0
  34. package/src/__tests__/route.test.ts +45 -0
  35. package/src/__tests__/secret.test.ts +301 -86
  36. package/src/__tests__/sentry.test.ts +146 -0
  37. package/src/__tests__/tail.test.ts +638 -0
  38. package/src/__tests__/whoami.test.tsx +21 -7
  39. package/src/bundle.ts +155 -0
  40. package/src/cfetch/index.ts +8 -6
  41. package/src/cfetch/internal.ts +60 -22
  42. package/src/cli.ts +10 -0
  43. package/src/config/README.md +107 -0
  44. package/src/config/config.ts +218 -0
  45. package/src/config/diagnostics.ts +80 -0
  46. package/src/config/environment.ts +371 -0
  47. package/src/config/index.ts +62 -0
  48. package/src/config/validation-helpers.ts +554 -0
  49. package/src/config/validation.ts +1232 -0
  50. package/src/{api/preview.ts → create-worker-preview.ts} +59 -21
  51. package/src/create-worker-upload-form.ts +217 -0
  52. package/src/dev/dev.tsx +392 -0
  53. package/src/dev/local.tsx +232 -0
  54. package/src/dev/remote.tsx +203 -0
  55. package/src/dev/use-esbuild.ts +104 -0
  56. package/src/entry.ts +240 -0
  57. package/src/environment-variables.ts +33 -0
  58. package/src/https-options.ts +122 -0
  59. package/src/index.tsx +1068 -780
  60. package/src/inspect.ts +25 -4
  61. package/src/intl-polyfill.d.ts +139 -0
  62. package/src/{kv.tsx → kv.ts} +95 -60
  63. package/src/miniflare-cli/README.md +30 -0
  64. package/src/miniflare-cli/enum-keys.ts +17 -0
  65. package/src/miniflare-cli/index.ts +37 -0
  66. package/src/module-collection.ts +226 -31
  67. package/src/open-in-browser.ts +25 -0
  68. package/src/package-manager.ts +21 -15
  69. package/src/pages.tsx +35 -20
  70. package/src/parse.ts +183 -0
  71. package/src/proxy.ts +115 -45
  72. package/src/publish.ts +268 -328
  73. package/src/r2.ts +50 -0
  74. package/src/reporting.ts +131 -0
  75. package/src/selfsigned.d.ts +29 -0
  76. package/src/sites.tsx +83 -43
  77. package/src/tail/filters.ts +279 -0
  78. package/src/tail/index.ts +299 -0
  79. package/src/tail/printing.ts +62 -0
  80. package/src/user.tsx +234 -126
  81. package/src/{api/worker.ts → worker.ts} +44 -36
  82. package/templates/new-worker.js +1 -1
  83. package/templates/new-worker.ts +1 -1
  84. package/templates/tsconfig.json +105 -0
  85. package/wrangler-dist/cli.js +131202 -114243
  86. package/wrangler-dist/cli.js.map +3 -3
  87. package/src/__tests__/helpers/mock-user.ts +0 -27
  88. package/src/api/form_data.ts +0 -134
  89. package/src/config.ts +0 -641
  90. package/src/dev.tsx +0 -834
  91. package/src/tail.tsx +0 -73
@@ -1,7 +1,11 @@
1
+ import * as fs from "node:fs";
2
+ import * as TOML from "@iarna/toml";
3
+ import fetchMock from "jest-fetch-mock";
1
4
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2
5
  import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
3
6
  import { mockConsoleMethods } from "./helpers/mock-console";
4
7
  import { mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
8
+ import { useMockStdin } from "./helpers/mock-stdin";
5
9
  import { runInTempDir } from "./helpers/run-in-tmp";
6
10
  import { runWrangler } from "./helpers/run-wrangler";
7
11
 
@@ -16,12 +20,24 @@ describe("wrangler secret", () => {
16
20
  });
17
21
 
18
22
  describe("put", () => {
19
- function mockPutRequest(input: { name: string; text: string }) {
23
+ function mockPutRequest(
24
+ input: { name: string; text: string },
25
+ env?: string,
26
+ legacyEnv = false
27
+ ) {
28
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
29
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
20
30
  setMockResponse(
21
- "/accounts/:accountId/workers/scripts/:scriptName/secrets",
31
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
22
32
  "PUT",
23
- ([_url, accountId], { body }) => {
33
+ ([_url, accountId, scriptName, envName], { body }) => {
24
34
  expect(accountId).toEqual("some-account-id");
35
+ expect(scriptName).toEqual(
36
+ legacyEnv && env ? `script-name-${env}` : "script-name"
37
+ );
38
+ if (!legacyEnv) {
39
+ expect(envName).toEqual(env);
40
+ }
25
41
  const { name, text, type } = JSON.parse(body as string);
26
42
  expect(type).toEqual("secret_text");
27
43
  expect(name).toEqual(input.name);
@@ -32,70 +48,213 @@ describe("wrangler secret", () => {
32
48
  );
33
49
  }
34
50
 
35
- it("should create a secret", async () => {
36
- mockPrompt({
37
- text: "Enter a secret value:",
38
- type: "password",
39
- result: "the-secret",
51
+ describe("interactive", () => {
52
+ useMockStdin({ isTTY: true });
53
+
54
+ it("should create a secret", async () => {
55
+ mockPrompt({
56
+ text: "Enter a secret value:",
57
+ type: "password",
58
+ result: "the-secret",
59
+ });
60
+
61
+ mockPutRequest({ name: "the-key", text: "the-secret" });
62
+ await runWrangler("secret put the-key --name script-name");
63
+
64
+ expect(std.out).toMatchInlineSnapshot(`
65
+ "🌀 Creating the secret for script script-name
66
+ ✨ Success! Uploaded secret the-key"
67
+ `);
68
+ expect(std.err).toMatchInlineSnapshot(`""`);
40
69
  });
41
70
 
42
- mockPutRequest({ name: "the-secret-name", text: "the-secret" });
43
- await runWrangler("secret put the-key --name script-name");
71
+ it("should create a secret: legacy envs", async () => {
72
+ mockPrompt({
73
+ text: "Enter a secret value:",
74
+ type: "password",
75
+ result: "the-secret",
76
+ });
44
77
 
45
- expect(std.out).toMatchInlineSnapshot(`
46
- "🌀 Creating the secret for script script-name
47
- ✨ Success! Uploaded secret the-key"
48
- `);
49
- expect(std.err).toMatchInlineSnapshot(`""`);
50
- });
78
+ mockPutRequest(
79
+ { name: "the-key", text: "the-secret" },
80
+ "some-env",
81
+ true
82
+ );
83
+ await runWrangler(
84
+ "secret put the-key --name script-name --env some-env --legacy-env"
85
+ );
51
86
 
52
- it("should error without a script name", async () => {
53
- let error: Error | undefined;
54
- try {
55
- await runWrangler("secret put the-key");
56
- } catch (e) {
57
- error = e;
58
- }
59
- expect(std.out).toMatchInlineSnapshot(`""`);
60
- expect(std.err).toMatchInlineSnapshot(`
61
- "Missing script name
87
+ expect(std.out).toMatchInlineSnapshot(`
88
+ "🌀 Creating the secret for script script-name-some-env
89
+ ✨ Success! Uploaded secret the-key"
90
+ `);
91
+ expect(std.err).toMatchInlineSnapshot(`""`);
92
+ });
62
93
 
63
- %s
64
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
65
- `);
66
- expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
94
+ it("should create a secret: service envs", async () => {
95
+ mockPrompt({
96
+ text: "Enter a secret value:",
97
+ type: "password",
98
+ result: "the-secret",
99
+ });
100
+
101
+ mockPutRequest(
102
+ { name: "the-key", text: "the-secret" },
103
+ "some-env",
104
+ false
105
+ );
106
+ await runWrangler(
107
+ "secret put the-key --name script-name --env some-env --legacy-env false"
108
+ );
109
+
110
+ expect(std.out).toMatchInlineSnapshot(`
111
+ "🌀 Creating the secret for script script-name (some-env)
112
+ ✨ Success! Uploaded secret the-key"
113
+ `);
114
+ expect(std.err).toMatchInlineSnapshot(`""`);
115
+ });
116
+
117
+ it("should error without a script name", async () => {
118
+ let error: Error | undefined;
119
+ try {
120
+ await runWrangler("secret put the-key");
121
+ } catch (e) {
122
+ error = e as Error;
123
+ }
124
+ expect(std.out).toMatchInlineSnapshot(`""`);
125
+ expect(std.err).toMatchInlineSnapshot(`
126
+ "Missing script name
127
+
128
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
129
+ `);
130
+ expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
131
+ });
67
132
  });
68
133
 
69
- it("warns about being a no-op in local mode", async () => {
70
- mockPrompt({
71
- text: "Enter a secret value:",
72
- type: "password",
73
- result: "the-secret",
134
+ describe("non-interactive", () => {
135
+ const mockStdIn = useMockStdin({ isTTY: false });
136
+
137
+ it("should create a secret, from piped input", async () => {
138
+ mockPutRequest({ name: "the-key", text: "the-secret" });
139
+ // Pipe the secret in as three chunks to test that we reconstitute it correctly.
140
+ mockStdIn.send("the", "-", "secret");
141
+ await runWrangler("secret put the-key --name script-name");
142
+
143
+ expect(std.out).toMatchInlineSnapshot(`
144
+ "🌀 Creating the secret for script script-name
145
+ ✨ Success! Uploaded secret the-key"
146
+ `);
147
+ expect(std.warn).toMatchInlineSnapshot(`""`);
148
+ expect(std.err).toMatchInlineSnapshot(`""`);
74
149
  });
75
150
 
76
- mockPutRequest({ name: "the-secret-name", text: "the-secret" });
77
- await runWrangler("secret put the-key --name script-name --local");
78
- expect(std.out).toMatchInlineSnapshot(`""`);
79
- expect(std.err).toMatchInlineSnapshot(`""`);
80
- expect(std.warn).toMatchInlineSnapshot(
81
- `"\`wrangler secret put\` is a no-op in --local mode"`
82
- );
151
+ it("should error if the piped input fails", async () => {
152
+ mockPutRequest({ name: "the-key", text: "the-secret" });
153
+ mockStdIn.throwError(new Error("Error in stdin stream"));
154
+ await expect(
155
+ runWrangler("secret put the-key --name script-name")
156
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`"Error in stdin stream"`);
157
+
158
+ expect(std.out).toMatchInlineSnapshot(`""`);
159
+ expect(std.warn).toMatchInlineSnapshot(`""`);
160
+ });
161
+
162
+ describe("with accountId", () => {
163
+ mockAccountId({ accountId: null });
164
+
165
+ it("should error if a user has no account", async () => {
166
+ await expect(
167
+ runWrangler("secret put the-key --name script-name")
168
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
169
+ `"No account id found, quitting..."`
170
+ );
171
+ });
172
+
173
+ it("should use the account from wrangler.toml", async () => {
174
+ fs.writeFileSync(
175
+ "wrangler.toml",
176
+ TOML.stringify({
177
+ account_id: "some-account-id",
178
+ }),
179
+ "utf-8"
180
+ );
181
+ mockStdIn.send("the-secret");
182
+ mockPutRequest({ name: "the-key", text: "the-secret" });
183
+ await runWrangler("secret put the-key --name script-name");
184
+ expect(std.out).toMatchInlineSnapshot(`
185
+ "🌀 Creating the secret for script script-name
186
+ ✨ Success! Uploaded secret the-key"
187
+ `);
188
+ expect(std.warn).toMatchInlineSnapshot(`""`);
189
+ expect(std.err).toMatchInlineSnapshot(`""`);
190
+ });
191
+
192
+ it("should error if a user has multiple accounts, and has not specified an account in wrangler.toml", async () => {
193
+ // This is a mock response for the request to the CF API memberships of the current user.
194
+ fetchMock.doMockOnce(async () => {
195
+ return {
196
+ body: JSON.stringify({
197
+ success: true,
198
+ result: [
199
+ {
200
+ id: "1",
201
+ account: { id: "account-id-1", name: "account-name-1" },
202
+ },
203
+ {
204
+ id: "2",
205
+ account: { id: "account-id-2", name: "account-name-2" },
206
+ },
207
+ {
208
+ id: "3",
209
+ account: { id: "account-id-3", name: "account-name-3" },
210
+ },
211
+ ],
212
+ }),
213
+ };
214
+ });
215
+ await expect(runWrangler("secret put the-key --name script-name"))
216
+ .rejects.toThrowErrorMatchingInlineSnapshot(`
217
+ "More than one account available but unable to select one in non-interactive mode.
218
+ Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
219
+ Available accounts are (\\"<name>\\" - \\"<id>\\"):
220
+ \\"account-name-1\\" - \\"account-id-1\\")
221
+ \\"account-name-2\\" - \\"account-id-2\\")
222
+ \\"account-name-3\\" - \\"account-id-3\\")"
223
+ `);
224
+ });
225
+ });
83
226
  });
84
227
  });
85
228
 
86
229
  describe("delete", () => {
87
- function mockDeleteRequest(input: {
88
- scriptName: string;
89
- secretName: string;
90
- }) {
230
+ function mockDeleteRequest(
231
+ input: {
232
+ scriptName: string;
233
+ secretName: string;
234
+ },
235
+ env?: string,
236
+ legacyEnv = false
237
+ ) {
238
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
239
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
91
240
  setMockResponse(
92
- "/accounts/:accountId/workers/scripts/:scriptName/secrets/:secretName",
241
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets/:secretName`,
93
242
  "DELETE",
94
- ([_url, accountId, scriptName, secretName]) => {
243
+ ([_url, accountId, scriptName, envOrSecretName, secretName]) => {
95
244
  expect(accountId).toEqual("some-account-id");
96
- expect(scriptName).toEqual(input.scriptName);
97
- expect(secretName).toEqual(input.secretName);
98
-
245
+ expect(scriptName).toEqual(
246
+ legacyEnv && env ? `script-name-${env}` : "script-name"
247
+ );
248
+ if (!legacyEnv) {
249
+ if (env) {
250
+ expect(envOrSecretName).toEqual(env);
251
+ expect(secretName).toEqual(input.secretName);
252
+ } else {
253
+ expect(envOrSecretName).toEqual(input.secretName);
254
+ }
255
+ } else {
256
+ expect(envOrSecretName).toEqual(input.secretName);
257
+ }
99
258
  return null;
100
259
  }
101
260
  );
@@ -108,7 +267,46 @@ describe("wrangler secret", () => {
108
267
  });
109
268
  await runWrangler("secret delete the-key --name script-name");
110
269
  expect(std.out).toMatchInlineSnapshot(`
111
- "🌀 Deleting the secret the-key on script script-name.
270
+ "🌀 Deleting the secret the-key on script script-name
271
+ ✨ Success! Deleted secret the-key"
272
+ `);
273
+ expect(std.err).toMatchInlineSnapshot(`""`);
274
+ });
275
+
276
+ it("should delete a secret: legacy envs", async () => {
277
+ mockDeleteRequest(
278
+ { scriptName: "script-name", secretName: "the-key" },
279
+ "some-env",
280
+ true
281
+ );
282
+ mockConfirm({
283
+ text: "Are you sure you want to permanently delete the variable the-key on the script script-name-some-env?",
284
+ result: true,
285
+ });
286
+ await runWrangler(
287
+ "secret delete the-key --name script-name --env some-env --legacy-env"
288
+ );
289
+ expect(std.out).toMatchInlineSnapshot(`
290
+ "🌀 Deleting the secret the-key on script script-name-some-env
291
+ ✨ Success! Deleted secret the-key"
292
+ `);
293
+ expect(std.err).toMatchInlineSnapshot(`""`);
294
+ });
295
+
296
+ it("should delete a secret: service envs", async () => {
297
+ mockDeleteRequest(
298
+ { scriptName: "script-name", secretName: "the-key" },
299
+ "some-env"
300
+ );
301
+ mockConfirm({
302
+ text: "Are you sure you want to permanently delete the variable the-key on the script script-name (some-env)?",
303
+ result: true,
304
+ });
305
+ await runWrangler(
306
+ "secret delete the-key --name script-name --env some-env --legacy-env false"
307
+ );
308
+ expect(std.out).toMatchInlineSnapshot(`
309
+ "🌀 Deleting the secret the-key on script script-name (some-env)
112
310
  ✨ Success! Deleted secret the-key"
113
311
  `);
114
312
  expect(std.err).toMatchInlineSnapshot(`""`);
@@ -120,42 +318,37 @@ describe("wrangler secret", () => {
120
318
  try {
121
319
  await runWrangler("secret delete the-key");
122
320
  } catch (e) {
123
- error = e;
321
+ error = e as Error;
124
322
  }
125
323
  expect(std.out).toMatchInlineSnapshot(`""`);
126
324
  expect(std.err).toMatchInlineSnapshot(`
127
325
  "Missing script name
128
326
 
129
- %s
130
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
327
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
131
328
  `);
132
329
  expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
133
330
  });
134
-
135
- it("warns about being a no-op in local mode", async () => {
136
- mockConfirm({
137
- text: "Are you sure you want to permanently delete the variable the-key on the script script-name?",
138
- result: true,
139
- });
140
- await runWrangler("secret delete the-key --name script-name --local");
141
- expect(std.out).toMatchInlineSnapshot(
142
- `"🌀 Deleting the secret the-key on script script-name."`
143
- );
144
- expect(std.err).toMatchInlineSnapshot(`""`);
145
- expect(std.warn).toMatchInlineSnapshot(
146
- `"\`wrangler secret delete\` is a no-op in --local mode"`
147
- );
148
- });
149
331
  });
150
332
 
151
333
  describe("list", () => {
152
- function mockListRequest(input: { scriptName: string }) {
334
+ function mockListRequest(
335
+ input: { scriptName: string },
336
+ env?: string,
337
+ legacyEnv = false
338
+ ) {
339
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
340
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
153
341
  setMockResponse(
154
- "/accounts/:accountId/workers/scripts/:scriptName/secrets",
342
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
155
343
  "GET",
156
- ([_url, accountId, scriptName]) => {
344
+ ([_url, accountId, scriptName, envName]) => {
157
345
  expect(accountId).toEqual("some-account-id");
158
- expect(scriptName).toEqual(input.scriptName);
346
+ expect(scriptName).toEqual(
347
+ legacyEnv && env ? `script-name-${env}` : "script-name"
348
+ );
349
+ if (!legacyEnv) {
350
+ expect(envName).toEqual(env);
351
+ }
159
352
 
160
353
  return [
161
354
  {
@@ -181,30 +374,52 @@ describe("wrangler secret", () => {
181
374
  expect(std.err).toMatchInlineSnapshot(`""`);
182
375
  });
183
376
 
377
+ it("should list secrets: legacy envs", async () => {
378
+ mockListRequest({ scriptName: "script-name" }, "some-env", true);
379
+ await runWrangler(
380
+ "secret list --name script-name --env some-env --legacy-env"
381
+ );
382
+ expect(std.out).toMatchInlineSnapshot(`
383
+ "[
384
+ {
385
+ \\"name\\": \\"the-secret-name\\",
386
+ \\"type\\": \\"secret_text\\"
387
+ }
388
+ ]"
389
+ `);
390
+ expect(std.err).toMatchInlineSnapshot(`""`);
391
+ });
392
+
393
+ it("should list secrets: service envs", async () => {
394
+ mockListRequest({ scriptName: "script-name" }, "some-env");
395
+ await runWrangler(
396
+ "secret list --name script-name --env some-env --legacy-env false"
397
+ );
398
+ expect(std.out).toMatchInlineSnapshot(`
399
+ "[
400
+ {
401
+ \\"name\\": \\"the-secret-name\\",
402
+ \\"type\\": \\"secret_text\\"
403
+ }
404
+ ]"
405
+ `);
406
+ expect(std.err).toMatchInlineSnapshot(`""`);
407
+ });
408
+
184
409
  it("should error without a script name", async () => {
185
410
  let error: Error | undefined;
186
411
  try {
187
412
  await runWrangler("secret list");
188
413
  } catch (e) {
189
- error = e;
414
+ error = e as Error;
190
415
  }
191
416
  expect(std.out).toMatchInlineSnapshot(`""`);
192
417
  expect(std.err).toMatchInlineSnapshot(`
193
418
  "Missing script name
194
419
 
195
- %s
196
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
420
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
197
421
  `);
198
422
  expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
199
423
  });
200
-
201
- it("warns about being a no-op in local mode", async () => {
202
- await runWrangler("secret list --name script-name --local");
203
- expect(std.out).toMatchInlineSnapshot(`""`);
204
- expect(std.err).toMatchInlineSnapshot(`""`);
205
- expect(std.warn).toMatchInlineSnapshot(
206
- `"\`wrangler secret list\` is a no-op in --local mode"`
207
- );
208
- });
209
424
  });
210
425
  });
@@ -0,0 +1,146 @@
1
+ import * as fs from "node:fs";
2
+ import * as fsp from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import * as TOML from "@iarna/toml";
6
+ import * as Sentry from "@sentry/node";
7
+ import prompts from "prompts";
8
+
9
+ import { mockConsoleMethods } from "./helpers/mock-console";
10
+ import { runInTempDir } from "./helpers/run-in-tmp";
11
+ import { runWrangler } from "./helpers/run-wrangler";
12
+ const { reportError } = jest.requireActual("../reporting");
13
+
14
+ describe("Error Reporting", () => {
15
+ runInTempDir({ homedir: "./home" });
16
+ mockConsoleMethods();
17
+ const reportingTOMLPath = ".wrangler/config/reporting.toml";
18
+
19
+ const originalTTY = process.stdout.isTTY;
20
+ beforeEach(() => {
21
+ jest.mock("@sentry/node");
22
+ jest.spyOn(Sentry, "captureException");
23
+ process.stdout.isTTY = true;
24
+ });
25
+
26
+ afterEach(() => {
27
+ jest.unmock("@sentry/node");
28
+ jest.clearAllMocks();
29
+ process.stdout.isTTY = originalTTY;
30
+ });
31
+
32
+ it("should confirm user will allow error reporting usage", async () => {
33
+ jest.spyOn(prompts, "prompt").mockResolvedValue({ sentryDecision: true });
34
+
35
+ await reportError(new Error("test error"), "testFalse");
36
+
37
+ const { error_tracking_opt, error_tracking_opt_date } = TOML.parse(
38
+ await fsp.readFile(path.join(os.homedir(), reportingTOMLPath), "utf-8")
39
+ );
40
+
41
+ expect(error_tracking_opt).toBe(true);
42
+ expect(error_tracking_opt_date).toBeTruthy();
43
+
44
+ expect(Sentry.captureException).toHaveBeenCalledWith(
45
+ new Error("test error")
46
+ );
47
+ });
48
+
49
+ it("should confirm user will disallow error reporting usage", async () => {
50
+ jest.spyOn(prompts, "prompt").mockResolvedValue({ sentryDecision: false });
51
+ await reportError(new Error("test error"), "testFalse");
52
+
53
+ const { error_tracking_opt, error_tracking_opt_date } = TOML.parse(
54
+ await fsp.readFile(path.join(os.homedir(), reportingTOMLPath), "utf-8")
55
+ );
56
+
57
+ expect(error_tracking_opt).toBe(false);
58
+ expect(error_tracking_opt_date).toBeTruthy();
59
+
60
+ expect(Sentry.captureException).not.toHaveBeenCalledWith(
61
+ new Error("test error"),
62
+ "testFalse"
63
+ );
64
+ });
65
+
66
+ it("should confirm user will allow 'once' error reporting usage", async () => {
67
+ jest.spyOn(prompts, "prompt").mockResolvedValue({ sentryDecision: "once" });
68
+ await runWrangler();
69
+ await reportError(new Error("test error"), "testFalse");
70
+
71
+ expect(fs.existsSync(path.join(os.homedir(), reportingTOMLPath))).toBe(
72
+ false
73
+ );
74
+
75
+ expect(Sentry.captureException).not.toHaveBeenCalledWith(
76
+ new Error("test error"),
77
+ "testFalse"
78
+ );
79
+ });
80
+
81
+ it("should not prompt w/ subsequent Errors after user disallows error reporting", async () => {
82
+ jest.spyOn(prompts, "prompt").mockResolvedValue({ sentryDecision: false });
83
+ await reportError(new Error("test error"), "testFalse");
84
+
85
+ expect(Sentry.captureException).not.toHaveBeenCalledWith(
86
+ new Error("test error"),
87
+ "testFalse"
88
+ );
89
+
90
+ await reportError(new Error("second test error"), "testFalse");
91
+ expect(Sentry.captureException).not.toHaveBeenCalledWith(
92
+ new Error("second test error"),
93
+ "testFalse"
94
+ );
95
+ });
96
+
97
+ it("should not prompt w/ subsequent Errors after user Always allows error reporting", async () => {
98
+ jest.spyOn(prompts, "prompt").mockResolvedValue({ sentryDecision: true });
99
+ await reportError(new Error("test error"), "testFalse");
100
+
101
+ expect(Sentry.captureException).toHaveBeenCalledWith(
102
+ new Error("test error")
103
+ );
104
+
105
+ await reportError(new Error("second test error"), "testFalse");
106
+ expect(Sentry.captureException).toHaveBeenCalledWith(
107
+ new Error("second test error")
108
+ );
109
+ });
110
+
111
+ it("should prompt w/ subsequent Errors after user allows error reporting once", async () => {
112
+ const promptSpy = jest
113
+ .spyOn(prompts, "prompt")
114
+ .mockResolvedValue({ sentryDecision: "once" });
115
+ await reportError(new Error("test error"), "testFalse");
116
+
117
+ expect(Sentry.captureException).toHaveBeenCalledWith(
118
+ new Error("test error")
119
+ );
120
+
121
+ await reportError(new Error("second test error"), "testFalse");
122
+ expect(Sentry.captureException).toHaveBeenCalledWith(
123
+ new Error("second test error")
124
+ );
125
+
126
+ expect(promptSpy).toBeCalledTimes(2);
127
+ });
128
+
129
+ it("should not prompt in non-TTY environment", async () => {
130
+ process.stdout.isTTY = false;
131
+
132
+ await reportError(new Error("test error"), "testFalse");
133
+
134
+ const { error_tracking_opt, error_tracking_opt_date } = TOML.parse(
135
+ await fsp.readFile(path.join(os.homedir(), reportingTOMLPath), "utf-8")
136
+ );
137
+
138
+ expect(error_tracking_opt).toBe(false);
139
+ expect(error_tracking_opt_date).toBeTruthy();
140
+
141
+ expect(Sentry.captureException).not.toHaveBeenCalledWith(
142
+ new Error("test error")
143
+ );
144
+ process.stdout.isTTY = originalTTY;
145
+ });
146
+ });