wrangler 2.5.0 → 2.6.0

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.
@@ -1,5 +1,5 @@
1
1
  import { mkdirSync } from "node:fs";
2
- import fetchMock from "jest-fetch-mock";
2
+ import { rest } from "msw";
3
3
  import { version as wranglerVersion } from "../../package.json";
4
4
  import { purgeConfigCaches, saveToConfigCache } from "../config-cache";
5
5
  import { CI } from "../is-ci";
@@ -12,10 +12,10 @@ import {
12
12
  writeMetricsConfig,
13
13
  } from "../metrics/metrics-config";
14
14
  import { writeAuthConfigFile } from "../user";
15
- import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
16
15
  import { mockConsoleMethods } from "./helpers/mock-console";
17
16
  import { mockConfirm } from "./helpers/mock-dialogs";
18
17
  import { useMockIsTTY } from "./helpers/mock-istty";
18
+ import { msw, mswSuccessOauthHandlers } from "./helpers/msw";
19
19
  import { runInTempDir } from "./helpers/run-in-tmp";
20
20
 
21
21
  declare const global: { SPARROW_SOURCE_KEY: string | undefined };
@@ -36,8 +36,6 @@ describe("metrics", () => {
36
36
  });
37
37
  afterEach(() => {
38
38
  global.SPARROW_SOURCE_KEY = ORIGINAL_SPARROW_SOURCE_KEY;
39
- fetchMock.resetMocks();
40
- unsetAllMocks();
41
39
  purgeConfigCaches();
42
40
  });
43
41
 
@@ -56,14 +54,8 @@ describe("metrics", () => {
56
54
 
57
55
  describe("identify()", () => {
58
56
  it("should send a request to the default URL", async () => {
59
- const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
60
- fetchMock.mockResponse("");
61
- await dispatcher.identify({ a: 1, b: 2 });
62
- expect(fetchMock).toHaveBeenCalledTimes(1);
63
- const [url, { body, headers }] = fetchMock.mock.calls[0];
64
- expect(url).toEqual("https://sparrow.cloudflare.com/api/v1/identify");
65
- expect(JSON.parse(body)).toEqual(
66
- expect.objectContaining({
57
+ const request = mockMetricRequest(
58
+ {
67
59
  event: "identify",
68
60
  properties: {
69
61
  category: "Workers",
@@ -72,13 +64,14 @@ describe("metrics", () => {
72
64
  a: 1,
73
65
  b: 2,
74
66
  },
75
- })
76
- );
77
- expect(headers).toEqual(
78
- expect.objectContaining({
79
- "Sparrow-Source-Key": "MOCK_KEY",
80
- })
67
+ },
68
+ { "Sparrow-Source-Key": "MOCK_KEY" },
69
+ "identify"
81
70
  );
71
+ const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
72
+ await dispatcher.identify({ a: 1, b: 2 });
73
+
74
+ expect(request.count).toBe(1);
82
75
  expect(std.debug).toMatchInlineSnapshot(
83
76
  `"Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"`
84
77
  );
@@ -88,12 +81,15 @@ describe("metrics", () => {
88
81
  });
89
82
 
90
83
  it("should write a debug log if the dispatcher is disabled", async () => {
84
+ const requests = mockMetricRequest({}, {}, "identify");
91
85
  const dispatcher = await getMetricsDispatcher({
92
86
  ...MOCK_DISPATCHER_OPTIONS,
93
87
  sendMetrics: false,
94
88
  });
95
89
  await dispatcher.identify({ a: 1, b: 2 });
96
- expect(fetchMock).toHaveBeenCalledTimes(0);
90
+ await flushPromises();
91
+
92
+ expect(requests.count).toBe(0);
97
93
  expect(std.debug).toMatchInlineSnapshot(
98
94
  `"Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."`
99
95
  );
@@ -103,12 +99,18 @@ describe("metrics", () => {
103
99
  });
104
100
 
105
101
  it("should write a debug log if the request fails", async () => {
102
+ msw.use(
103
+ rest.post("*/identify", async (req, res) => {
104
+ return res.networkError("BAD REQUEST");
105
+ })
106
+ );
107
+
106
108
  const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
107
- fetchMock.mockReject(new Error("BAD REQUEST"));
108
109
  await dispatcher.identify({ a: 1, b: 2 });
110
+ await flushPromises();
109
111
  expect(std.debug).toMatchInlineSnapshot(`
110
112
  "Metrics dispatcher: Posting data {\\"type\\":\\"identify\\",\\"name\\":\\"identify\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
111
- Metrics dispatcher: Failed to send request: BAD REQUEST"
113
+ Metrics dispatcher: Failed to send request: request to https://sparrow.cloudflare.com/api/v1/identify failed, reason: BAD REQUEST"
112
114
  `);
113
115
  expect(std.out).toMatchInlineSnapshot(`""`);
114
116
  expect(std.warn).toMatchInlineSnapshot(`""`);
@@ -119,7 +121,6 @@ describe("metrics", () => {
119
121
  global.SPARROW_SOURCE_KEY = undefined;
120
122
  const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
121
123
  await dispatcher.identify({ a: 1, b: 2 });
122
- expect(fetchMock).toHaveBeenCalledTimes(0);
123
124
  expect(std.debug).toMatchInlineSnapshot(
124
125
  `"Metrics dispatcher: Source Key not provided. Be sure to initialize before sending events. { type: 'identify', name: 'identify', properties: { a: 1, b: 2 } }"`
125
126
  );
@@ -131,14 +132,8 @@ describe("metrics", () => {
131
132
 
132
133
  describe("sendEvent()", () => {
133
134
  it("should send a request to the default URL", async () => {
134
- const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
135
- fetchMock.mockResponse("");
136
- await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
137
- expect(fetchMock).toHaveBeenCalledTimes(1);
138
- const [url, { body, headers }] = fetchMock.mock.calls[0];
139
- expect(url).toEqual("https://sparrow.cloudflare.com/api/v1/event");
140
- expect(JSON.parse(body)).toEqual(
141
- expect.objectContaining({
135
+ const requests = mockMetricRequest(
136
+ {
142
137
  event: "some-event",
143
138
  properties: {
144
139
  category: "Workers",
@@ -147,13 +142,16 @@ describe("metrics", () => {
147
142
  a: 1,
148
143
  b: 2,
149
144
  },
150
- })
151
- );
152
- expect(headers).toEqual(
153
- expect.objectContaining({
145
+ },
146
+ {
154
147
  "Sparrow-Source-Key": "MOCK_KEY",
155
- })
148
+ },
149
+ "event"
156
150
  );
151
+ const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
152
+ await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
153
+
154
+ expect(requests.count).toBe(1);
157
155
  expect(std.debug).toMatchInlineSnapshot(
158
156
  `"Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}"`
159
157
  );
@@ -163,12 +161,16 @@ describe("metrics", () => {
163
161
  });
164
162
 
165
163
  it("should write a debug log if the dispatcher is disabled", async () => {
164
+ const requests = mockMetricRequest({}, {}, "event");
165
+
166
166
  const dispatcher = await getMetricsDispatcher({
167
167
  ...MOCK_DISPATCHER_OPTIONS,
168
168
  sendMetrics: false,
169
169
  });
170
170
  await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
171
- expect(fetchMock).toHaveBeenCalledTimes(0);
171
+ await flushPromises();
172
+
173
+ expect(requests.count).toBe(0);
172
174
  expect(std.debug).toMatchInlineSnapshot(
173
175
  `"Metrics dispatcher: Dispatching disabled - would have sent {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}."`
174
176
  );
@@ -178,12 +180,17 @@ describe("metrics", () => {
178
180
  });
179
181
 
180
182
  it("should write a debug log if the request fails", async () => {
183
+ msw.use(
184
+ rest.post("*/event", async (_, res) => {
185
+ return res.networkError("BAD REQUEST");
186
+ })
187
+ );
181
188
  const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
182
- fetchMock.mockReject(new Error("BAD REQUEST"));
183
189
  await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
190
+ await flushPromises();
184
191
  expect(std.debug).toMatchInlineSnapshot(`
185
192
  "Metrics dispatcher: Posting data {\\"type\\":\\"event\\",\\"name\\":\\"some-event\\",\\"properties\\":{\\"a\\":1,\\"b\\":2}}
186
- Metrics dispatcher: Failed to send request: BAD REQUEST"
193
+ Metrics dispatcher: Failed to send request: request to https://sparrow.cloudflare.com/api/v1/event failed, reason: BAD REQUEST"
187
194
  `);
188
195
  expect(std.out).toMatchInlineSnapshot(`""`);
189
196
  expect(std.warn).toMatchInlineSnapshot(`""`);
@@ -192,9 +199,11 @@ describe("metrics", () => {
192
199
 
193
200
  it("should write a warning log if no source key has been provided", async () => {
194
201
  global.SPARROW_SOURCE_KEY = undefined;
202
+ const requests = mockMetricRequest({}, {}, "event");
195
203
  const dispatcher = await getMetricsDispatcher(MOCK_DISPATCHER_OPTIONS);
196
204
  await dispatcher.sendEvent("some-event", { a: 1, b: 2 });
197
- expect(fetchMock).toHaveBeenCalledTimes(0);
205
+
206
+ expect(requests.count).toBe(0);
198
207
  expect(std.debug).toMatchInlineSnapshot(
199
208
  `"Metrics dispatcher: Source Key not provided. Be sure to initialize before sending events. { type: 'event', name: 'some-event', properties: { a: 1, b: 2 } }"`
200
209
  );
@@ -418,6 +427,8 @@ describe("metrics", () => {
418
427
  sendMetrics: true,
419
428
  offline: false,
420
429
  });
430
+ await flushPromises();
431
+
421
432
  expect(userId).toEqual("MOCK_USER_ID");
422
433
  expect(userRequests.count).toBe(1);
423
434
  });
@@ -438,13 +449,47 @@ describe("metrics", () => {
438
449
  function mockUserRequest() {
439
450
  const requests = { count: 0 };
440
451
  beforeEach(() => {
441
- setMockResponse("/user", () => {
442
- requests.count++;
443
- return { id: "MOCK_USER_ID" };
444
- });
452
+ msw.use(
453
+ ...mswSuccessOauthHandlers,
454
+ rest.get("*/user", (_, res, cxt) => {
455
+ requests.count++;
456
+ return res(
457
+ cxt.status(200),
458
+ cxt.json({
459
+ success: true,
460
+ errors: [],
461
+ messages: [],
462
+ result: { id: "MOCK_USER_ID" },
463
+ })
464
+ );
465
+ })
466
+ );
445
467
  });
446
468
  afterEach(() => {
447
469
  requests.count = 0;
448
470
  });
449
471
  return requests;
450
472
  }
473
+
474
+ function mockMetricRequest(
475
+ body: unknown,
476
+ header: unknown,
477
+ endpoint: "identify" | "event"
478
+ ) {
479
+ const requests = { count: 0 };
480
+ msw.use(
481
+ rest.post(`*/${endpoint}`, async (req, res, cxt) => {
482
+ requests.count++;
483
+ expect(await req.json()).toEqual(body);
484
+ expect(req.headers).toContain(header);
485
+ return res.once(cxt.status(200), cxt.json({}));
486
+ })
487
+ );
488
+
489
+ return requests;
490
+ }
491
+
492
+ // Forces a tick to allow the non-awaited fetch promise to resolve.
493
+ function flushPromises(): Promise<void> {
494
+ return new Promise((resolve) => setTimeout(resolve, 0));
495
+ }