wrangler 0.0.0-e2a26a0 → 0.0.0-e3cab74

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 (68) hide show
  1. package/package.json +16 -7
  2. package/pages/functions/filepath-routing.test.ts +112 -36
  3. package/pages/functions/filepath-routing.ts +66 -119
  4. package/pages/functions/routes.ts +8 -17
  5. package/pages/functions/template-worker.ts +3 -9
  6. package/src/__tests__/configuration.test.ts +2185 -0
  7. package/src/__tests__/dev.test.tsx +48 -6
  8. package/src/__tests__/guess-worker-format.test.ts +66 -0
  9. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  10. package/src/__tests__/helpers/mock-account-id.ts +6 -6
  11. package/src/__tests__/helpers/mock-bin.ts +4 -1
  12. package/src/__tests__/helpers/mock-cfetch.ts +2 -2
  13. package/src/__tests__/helpers/mock-console.ts +11 -5
  14. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  15. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  16. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  17. package/src/__tests__/index.test.ts +343 -20
  18. package/src/__tests__/jest.setup.ts +21 -1
  19. package/src/__tests__/kv.test.ts +82 -80
  20. package/src/__tests__/package-manager.test.ts +2 -2
  21. package/src/__tests__/parse.test.ts +322 -0
  22. package/src/__tests__/publish.test.ts +1428 -244
  23. package/src/__tests__/r2.test.ts +198 -0
  24. package/src/__tests__/secret.test.ts +176 -60
  25. package/src/__tests__/sentry.test.ts +146 -0
  26. package/src/__tests__/tail.test.ts +564 -0
  27. package/src/api/form_data.ts +106 -23
  28. package/src/api/preview.ts +32 -13
  29. package/src/api/worker.ts +49 -18
  30. package/src/bundle.ts +133 -0
  31. package/src/cfetch/index.ts +8 -6
  32. package/src/cfetch/internal.ts +49 -17
  33. package/src/cli.ts +10 -0
  34. package/src/config/README.md +107 -0
  35. package/src/config/config.ts +265 -0
  36. package/src/config/diagnostics.ts +80 -0
  37. package/src/config/environment.ts +305 -0
  38. package/src/config/index.ts +52 -0
  39. package/src/config/validation-helpers.ts +370 -0
  40. package/src/config/validation.ts +1017 -0
  41. package/src/dev.tsx +338 -211
  42. package/src/entry.ts +57 -0
  43. package/src/environment-variables.ts +33 -0
  44. package/src/guess-worker-format.ts +68 -0
  45. package/src/index.tsx +860 -634
  46. package/src/inspect.ts +16 -3
  47. package/src/kv.tsx +20 -29
  48. package/src/module-collection.ts +141 -34
  49. package/src/open-in-browser.ts +13 -0
  50. package/src/package-manager.ts +21 -15
  51. package/src/pages.tsx +9 -10
  52. package/src/parse.ts +183 -0
  53. package/src/proxy.ts +11 -5
  54. package/src/publish.ts +272 -314
  55. package/src/r2.ts +50 -0
  56. package/src/reporting.ts +115 -0
  57. package/src/sites.tsx +16 -15
  58. package/src/tail/filters.ts +279 -0
  59. package/src/tail/index.ts +299 -0
  60. package/src/tail/printing.ts +62 -0
  61. package/src/user.tsx +74 -44
  62. package/templates/new-worker.js +1 -1
  63. package/templates/new-worker.ts +1 -1
  64. package/templates/tsconfig.json +105 -0
  65. package/wrangler-dist/cli.js +51308 -36440
  66. package/wrangler-dist/cli.js.map +3 -3
  67. package/src/config.ts +0 -641
  68. package/src/tail.tsx +0 -73
@@ -0,0 +1,198 @@
1
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
3
+ import { mockConsoleMethods } from "./helpers/mock-console";
4
+ import { runInTempDir } from "./helpers/run-in-tmp";
5
+ import { runWrangler } from "./helpers/run-wrangler";
6
+ import type { R2BucketInfo } from "../r2";
7
+
8
+ describe("wrangler", () => {
9
+ mockAccountId();
10
+ mockApiToken();
11
+ runInTempDir();
12
+ const std = mockConsoleMethods();
13
+
14
+ afterEach(() => {
15
+ unsetAllMocks();
16
+ });
17
+
18
+ describe("r2", () => {
19
+ describe("bucket", () => {
20
+ describe("list", () => {
21
+ function mockListRequest(buckets: R2BucketInfo[]) {
22
+ const requests = { count: 0 };
23
+ setMockResponse(
24
+ "/accounts/:accountId/r2/buckets",
25
+ ([_url, accountId], init) => {
26
+ requests.count++;
27
+ expect(accountId).toEqual("some-account-id");
28
+ expect(init).toEqual({});
29
+ return { buckets };
30
+ }
31
+ );
32
+ return requests;
33
+ }
34
+
35
+ it("should list buckets", async () => {
36
+ const expectedBuckets: R2BucketInfo[] = [
37
+ { name: "bucket-1", creation_date: "01-01-2001" },
38
+ { name: "bucket-2", creation_date: "01-01-2001" },
39
+ ];
40
+ mockListRequest(expectedBuckets);
41
+ await runWrangler("r2 bucket list");
42
+
43
+ expect(std.err).toMatchInlineSnapshot(`""`);
44
+ const buckets = JSON.parse(std.out);
45
+ expect(buckets).toEqual(expectedBuckets);
46
+ });
47
+ });
48
+
49
+ describe("create", () => {
50
+ function mockCreateRequest(expectedBucketName: string) {
51
+ const requests = { count: 0 };
52
+ setMockResponse(
53
+ "/accounts/:accountId/r2/buckets/:bucketName",
54
+ "PUT",
55
+ ([_url, accountId, bucketName]) => {
56
+ expect(accountId).toEqual("some-account-id");
57
+ expect(bucketName).toEqual(expectedBucketName);
58
+ requests.count += 1;
59
+ }
60
+ );
61
+ return requests;
62
+ }
63
+
64
+ it("should error if no bucket name is given", async () => {
65
+ await expect(
66
+ runWrangler("r2 bucket create")
67
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
68
+ `"Not enough non-option arguments: got 0, need at least 1"`
69
+ );
70
+ expect(std.out).toMatchInlineSnapshot(`""`);
71
+ expect(std.err).toMatchInlineSnapshot(`
72
+ "wrangler r2 bucket create <name>
73
+
74
+ Create a new R2 bucket
75
+
76
+ Positionals:
77
+ name The name of the new bucket [string] [required]
78
+
79
+ Flags:
80
+ -c, --config Path to .toml configuration file [string]
81
+ -h, --help Show help [boolean]
82
+ -v, --version Show version number [boolean]
83
+ --legacy-env Use legacy environments [boolean]
84
+
85
+ Not enough non-option arguments: got 0, need at least 1"
86
+ `);
87
+ });
88
+
89
+ it("should error if the bucket to create contains spaces", async () => {
90
+ await expect(
91
+ runWrangler("r2 bucket create abc def ghi")
92
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
93
+ `"Unexpected additional positional arguments \\"def ghi\\"."`
94
+ );
95
+ expect(std.out).toMatchInlineSnapshot(`""`);
96
+ expect(std.err).toMatchInlineSnapshot(`
97
+ "wrangler r2 bucket create <name>
98
+
99
+ Create a new R2 bucket
100
+
101
+ Positionals:
102
+ name The name of the new bucket [string] [required]
103
+
104
+ Flags:
105
+ -c, --config Path to .toml configuration file [string]
106
+ -h, --help Show help [boolean]
107
+ -v, --version Show version number [boolean]
108
+ --legacy-env Use legacy environments [boolean]
109
+
110
+ Unexpected additional positional arguments \\"def ghi\\"."
111
+ `);
112
+ });
113
+
114
+ it("should create a bucket", async () => {
115
+ const requests = mockCreateRequest("testBucket");
116
+ await runWrangler("r2 bucket create testBucket");
117
+ expect(std.out).toMatchInlineSnapshot(`
118
+ "Creating bucket testBucket.
119
+ Created bucket testBucket."
120
+ `);
121
+ expect(requests.count).toEqual(1);
122
+ });
123
+ });
124
+
125
+ describe("delete", () => {
126
+ function mockDeleteRequest(expectedBucketName: string) {
127
+ const requests = { count: 0 };
128
+ setMockResponse(
129
+ "/accounts/:accountId/r2/buckets/:bucketName",
130
+ "DELETE",
131
+ ([_url, accountId, bucketName]) => {
132
+ expect(accountId).toEqual("some-account-id");
133
+ expect(bucketName).toEqual(expectedBucketName);
134
+ requests.count += 1;
135
+ }
136
+ );
137
+ return requests;
138
+ }
139
+
140
+ it("should error if no bucket name is given", async () => {
141
+ await expect(
142
+ runWrangler("r2 bucket delete")
143
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
144
+ `"Not enough non-option arguments: got 0, need at least 1"`
145
+ );
146
+ expect(std.out).toMatchInlineSnapshot(`""`);
147
+ expect(std.err).toMatchInlineSnapshot(`
148
+ "wrangler r2 bucket delete <name>
149
+
150
+ Delete an R2 bucket
151
+
152
+ Positionals:
153
+ name The name of the bucket to delete [string] [required]
154
+
155
+ Flags:
156
+ -c, --config Path to .toml configuration file [string]
157
+ -h, --help Show help [boolean]
158
+ -v, --version Show version number [boolean]
159
+ --legacy-env Use legacy environments [boolean]
160
+
161
+ Not enough non-option arguments: got 0, need at least 1"
162
+ `);
163
+ });
164
+
165
+ it("should error if the bucket name to delete contains spaces", async () => {
166
+ await expect(
167
+ runWrangler("r2 bucket delete abc def ghi")
168
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
169
+ `"Unexpected additional positional arguments \\"def ghi\\"."`
170
+ );
171
+ expect(std.out).toMatchInlineSnapshot(`""`);
172
+ expect(std.err).toMatchInlineSnapshot(`
173
+ "wrangler r2 bucket delete <name>
174
+
175
+ Delete an R2 bucket
176
+
177
+ Positionals:
178
+ name The name of the bucket to delete [string] [required]
179
+
180
+ Flags:
181
+ -c, --config Path to .toml configuration file [string]
182
+ -h, --help Show help [boolean]
183
+ -v, --version Show version number [boolean]
184
+ --legacy-env Use legacy environments [boolean]
185
+
186
+ Unexpected additional positional arguments \\"def ghi\\"."
187
+ `);
188
+ });
189
+
190
+ it("should delete a bucket specified by name", async () => {
191
+ const requests = mockDeleteRequest("some-bucket");
192
+ await runWrangler(`r2 bucket delete some-bucket`);
193
+ expect(requests.count).toEqual(1);
194
+ });
195
+ });
196
+ });
197
+ });
198
+ });
@@ -16,12 +16,24 @@ describe("wrangler secret", () => {
16
16
  });
17
17
 
18
18
  describe("put", () => {
19
- function mockPutRequest(input: { name: string; text: string }) {
19
+ function mockPutRequest(
20
+ input: { name: string; text: string },
21
+ env?: string,
22
+ legacyEnv = false
23
+ ) {
24
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
25
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
20
26
  setMockResponse(
21
- "/accounts/:accountId/workers/scripts/:scriptName/secrets",
27
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
22
28
  "PUT",
23
- ([_url, accountId], { body }) => {
29
+ ([_url, accountId, scriptName, envName], { body }) => {
24
30
  expect(accountId).toEqual("some-account-id");
31
+ expect(scriptName).toEqual(
32
+ legacyEnv && env ? `script-name-${env}` : "script-name"
33
+ );
34
+ if (!legacyEnv) {
35
+ expect(envName).toEqual(env);
36
+ }
25
37
  const { name, text, type } = JSON.parse(body as string);
26
38
  expect(type).toEqual("secret_text");
27
39
  expect(name).toEqual(input.name);
@@ -49,12 +61,58 @@ describe("wrangler secret", () => {
49
61
  expect(std.err).toMatchInlineSnapshot(`""`);
50
62
  });
51
63
 
64
+ it("should create a secret: legacy envs", async () => {
65
+ mockPrompt({
66
+ text: "Enter a secret value:",
67
+ type: "password",
68
+ result: "the-secret",
69
+ });
70
+
71
+ mockPutRequest(
72
+ { name: "the-secret-name", text: "the-secret" },
73
+ "some-env",
74
+ true
75
+ );
76
+ await runWrangler(
77
+ "secret put the-key --name script-name --env some-env --legacy-env"
78
+ );
79
+
80
+ expect(std.out).toMatchInlineSnapshot(`
81
+ "🌀 Creating the secret for script script-name-some-env
82
+ ✨ Success! Uploaded secret the-key"
83
+ `);
84
+ expect(std.err).toMatchInlineSnapshot(`""`);
85
+ });
86
+
87
+ it("should create a secret: service envs", async () => {
88
+ mockPrompt({
89
+ text: "Enter a secret value:",
90
+ type: "password",
91
+ result: "the-secret",
92
+ });
93
+
94
+ mockPutRequest(
95
+ { name: "the-secret-name", text: "the-secret" },
96
+ "some-env",
97
+ false
98
+ );
99
+ await runWrangler(
100
+ "secret put the-key --name script-name --env some-env --legacy-env false"
101
+ );
102
+
103
+ expect(std.out).toMatchInlineSnapshot(`
104
+ "🌀 Creating the secret for script script-name (some-env)
105
+ ✨ Success! Uploaded secret the-key"
106
+ `);
107
+ expect(std.err).toMatchInlineSnapshot(`""`);
108
+ });
109
+
52
110
  it("should error without a script name", async () => {
53
111
  let error: Error | undefined;
54
112
  try {
55
113
  await runWrangler("secret put the-key");
56
114
  } catch (e) {
57
- error = e;
115
+ error = e as Error;
58
116
  }
59
117
  expect(std.out).toMatchInlineSnapshot(`""`);
60
118
  expect(std.err).toMatchInlineSnapshot(`
@@ -65,37 +123,37 @@ describe("wrangler secret", () => {
65
123
  `);
66
124
  expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
67
125
  });
68
-
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",
74
- });
75
-
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
- );
83
- });
84
126
  });
85
127
 
86
128
  describe("delete", () => {
87
- function mockDeleteRequest(input: {
88
- scriptName: string;
89
- secretName: string;
90
- }) {
129
+ function mockDeleteRequest(
130
+ input: {
131
+ scriptName: string;
132
+ secretName: string;
133
+ },
134
+ env?: string,
135
+ legacyEnv = false
136
+ ) {
137
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
138
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
91
139
  setMockResponse(
92
- "/accounts/:accountId/workers/scripts/:scriptName/secrets/:secretName",
140
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets/:secretName`,
93
141
  "DELETE",
94
- ([_url, accountId, scriptName, secretName]) => {
142
+ ([_url, accountId, scriptName, envOrSecretName, secretName]) => {
95
143
  expect(accountId).toEqual("some-account-id");
96
- expect(scriptName).toEqual(input.scriptName);
97
- expect(secretName).toEqual(input.secretName);
98
-
144
+ expect(scriptName).toEqual(
145
+ legacyEnv && env ? `script-name-${env}` : "script-name"
146
+ );
147
+ if (!legacyEnv) {
148
+ if (env) {
149
+ expect(envOrSecretName).toEqual(env);
150
+ expect(secretName).toEqual(input.secretName);
151
+ } else {
152
+ expect(envOrSecretName).toEqual(input.secretName);
153
+ }
154
+ } else {
155
+ expect(envOrSecretName).toEqual(input.secretName);
156
+ }
99
157
  return null;
100
158
  }
101
159
  );
@@ -108,7 +166,46 @@ describe("wrangler secret", () => {
108
166
  });
109
167
  await runWrangler("secret delete the-key --name script-name");
110
168
  expect(std.out).toMatchInlineSnapshot(`
111
- "🌀 Deleting the secret the-key on script script-name.
169
+ "🌀 Deleting the secret the-key on script script-name
170
+ ✨ Success! Deleted secret the-key"
171
+ `);
172
+ expect(std.err).toMatchInlineSnapshot(`""`);
173
+ });
174
+
175
+ it("should delete a secret: legacy envs", async () => {
176
+ mockDeleteRequest(
177
+ { scriptName: "script-name", secretName: "the-key" },
178
+ "some-env",
179
+ true
180
+ );
181
+ mockConfirm({
182
+ text: "Are you sure you want to permanently delete the variable the-key on the script script-name-some-env?",
183
+ result: true,
184
+ });
185
+ await runWrangler(
186
+ "secret delete the-key --name script-name --env some-env --legacy-env"
187
+ );
188
+ expect(std.out).toMatchInlineSnapshot(`
189
+ "🌀 Deleting the secret the-key on script script-name-some-env
190
+ ✨ Success! Deleted secret the-key"
191
+ `);
192
+ expect(std.err).toMatchInlineSnapshot(`""`);
193
+ });
194
+
195
+ it("should delete a secret: service envs", async () => {
196
+ mockDeleteRequest(
197
+ { scriptName: "script-name", secretName: "the-key" },
198
+ "some-env"
199
+ );
200
+ mockConfirm({
201
+ text: "Are you sure you want to permanently delete the variable the-key on the script script-name (some-env)?",
202
+ result: true,
203
+ });
204
+ await runWrangler(
205
+ "secret delete the-key --name script-name --env some-env --legacy-env false"
206
+ );
207
+ expect(std.out).toMatchInlineSnapshot(`
208
+ "🌀 Deleting the secret the-key on script script-name (some-env)
112
209
  ✨ Success! Deleted secret the-key"
113
210
  `);
114
211
  expect(std.err).toMatchInlineSnapshot(`""`);
@@ -120,7 +217,7 @@ describe("wrangler secret", () => {
120
217
  try {
121
218
  await runWrangler("secret delete the-key");
122
219
  } catch (e) {
123
- error = e;
220
+ error = e as Error;
124
221
  }
125
222
  expect(std.out).toMatchInlineSnapshot(`""`);
126
223
  expect(std.err).toMatchInlineSnapshot(`
@@ -131,31 +228,27 @@ describe("wrangler secret", () => {
131
228
  `);
132
229
  expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
133
230
  });
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
231
  });
150
232
 
151
233
  describe("list", () => {
152
- function mockListRequest(input: { scriptName: string }) {
234
+ function mockListRequest(
235
+ input: { scriptName: string },
236
+ env?: string,
237
+ legacyEnv = false
238
+ ) {
239
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
240
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
153
241
  setMockResponse(
154
- "/accounts/:accountId/workers/scripts/:scriptName/secrets",
242
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
155
243
  "GET",
156
- ([_url, accountId, scriptName]) => {
244
+ ([_url, accountId, scriptName, envName]) => {
157
245
  expect(accountId).toEqual("some-account-id");
158
- expect(scriptName).toEqual(input.scriptName);
246
+ expect(scriptName).toEqual(
247
+ legacyEnv && env ? `script-name-${env}` : "script-name"
248
+ );
249
+ if (!legacyEnv) {
250
+ expect(envName).toEqual(env);
251
+ }
159
252
 
160
253
  return [
161
254
  {
@@ -181,12 +274,44 @@ describe("wrangler secret", () => {
181
274
  expect(std.err).toMatchInlineSnapshot(`""`);
182
275
  });
183
276
 
277
+ it("should list secrets: legacy envs", async () => {
278
+ mockListRequest({ scriptName: "script-name" }, "some-env", true);
279
+ await runWrangler(
280
+ "secret list --name script-name --env some-env --legacy-env"
281
+ );
282
+ expect(std.out).toMatchInlineSnapshot(`
283
+ "[
284
+ {
285
+ \\"name\\": \\"the-secret-name\\",
286
+ \\"type\\": \\"secret_text\\"
287
+ }
288
+ ]"
289
+ `);
290
+ expect(std.err).toMatchInlineSnapshot(`""`);
291
+ });
292
+
293
+ it("should list secrets: service envs", async () => {
294
+ mockListRequest({ scriptName: "script-name" }, "some-env");
295
+ await runWrangler(
296
+ "secret list --name script-name --env some-env --legacy-env false"
297
+ );
298
+ expect(std.out).toMatchInlineSnapshot(`
299
+ "[
300
+ {
301
+ \\"name\\": \\"the-secret-name\\",
302
+ \\"type\\": \\"secret_text\\"
303
+ }
304
+ ]"
305
+ `);
306
+ expect(std.err).toMatchInlineSnapshot(`""`);
307
+ });
308
+
184
309
  it("should error without a script name", async () => {
185
310
  let error: Error | undefined;
186
311
  try {
187
312
  await runWrangler("secret list");
188
313
  } catch (e) {
189
- error = e;
314
+ error = e as Error;
190
315
  }
191
316
  expect(std.out).toMatchInlineSnapshot(`""`);
192
317
  expect(std.err).toMatchInlineSnapshot(`
@@ -197,14 +322,5 @@ describe("wrangler secret", () => {
197
322
  `);
198
323
  expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
199
324
  });
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
325
  });
210
326
  });
@@ -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
+ });