@msw/playwright 0.6.3 → 0.6.4

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.
package/build/index.js CHANGED
@@ -37,7 +37,7 @@ var SetupPlaywrightApi = class extends SetupApi {
37
37
  * requests through the matching logic below.
38
38
  * @see https://github.com/mswjs/playwright/issues/13
39
39
  */
40
- if (this.options.skipAssetRequests && isCommonAssetRequest(fetchRequest)) return route.fallback();
40
+ if (this.options.skipAssetRequests && isCommonAssetRequest(fetchRequest)) return this.safelyHandleRoute(() => route.fallback());
41
41
  const handlers = this.handlersController.currentHandlers().filter((handler) => {
42
42
  return handler instanceof RequestHandler;
43
43
  });
@@ -51,14 +51,16 @@ var SetupPlaywrightApi = class extends SetupApi {
51
51
  baseUrl
52
52
  } });
53
53
  if (response) {
54
- if (response.status === 0) return route.abort();
55
- return route.fulfill({
56
- status: response.status,
57
- headers: Object.fromEntries(response.headers),
58
- body: response.body ? Buffer.from(await response.arrayBuffer()) : void 0
54
+ if (response.status === 0) return this.safelyHandleRoute(() => route.abort());
55
+ return this.safelyHandleRoute(async () => {
56
+ return route.fulfill({
57
+ status: response.status,
58
+ headers: Object.fromEntries(response.headers),
59
+ body: response.body ? Buffer.from(await response.arrayBuffer()) : void 0
60
+ });
59
61
  });
60
62
  }
61
- return route.fallback();
63
+ return this.safelyHandleRoute(() => route.fallback());
62
64
  });
63
65
  await context.routeWebSocket(INTERNAL_MATCH_ALL_REG_EXP, async (route) => {
64
66
  const allWebSocketHandlers = this.handlersController.currentHandlers().filter((handler) => {
@@ -90,6 +92,21 @@ var SetupPlaywrightApi = class extends SetupApi {
90
92
  if (url === "about:blank") return;
91
93
  return decodeURI(new URL(encodeURI(url)).origin);
92
94
  }
95
+ async safelyHandleRoute(callback) {
96
+ try {
97
+ await callback();
98
+ } catch (error) {
99
+ /**
100
+ * @note Ignore "Route is already handled!" errors.
101
+ * Playwright has a bug where requests terminated due to navigation
102
+ * cause your in-flight route handlers to throw. There's no means to
103
+ * detect that scenario as both "route.handled" and "route._handlingPromise" are internal.
104
+ * @see https://github.com/mswjs/playwright/issues/35
105
+ */
106
+ if (error instanceof Error && /route is already handled/i.test(error.message)) return;
107
+ throw error;
108
+ }
109
+ }
93
110
  };
94
111
  var PlaywrightWebSocketClientConnection = class {
95
112
  id;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@msw/playwright",
4
- "version": "0.6.3",
4
+ "version": "0.6.4",
5
5
  "description": "Mock Service Worker binding for Playwright",
6
6
  "main": "./build/index.js",
7
7
  "types": "./build/index.d.ts",
package/src/fixture.ts CHANGED
@@ -99,7 +99,7 @@ class SetupPlaywrightApi extends SetupApi<LifeCycleEventsMap> {
99
99
  this.options.skipAssetRequests &&
100
100
  isCommonAssetRequest(fetchRequest)
101
101
  ) {
102
- return route.fallback()
102
+ return this.safelyHandleRoute(() => route.fallback())
103
103
  }
104
104
 
105
105
  const handlers = this.handlersController
@@ -134,19 +134,21 @@ class SetupPlaywrightApi extends SetupApi<LifeCycleEventsMap> {
134
134
 
135
135
  if (response) {
136
136
  if (response.status === 0) {
137
- return route.abort()
137
+ return this.safelyHandleRoute(() => route.abort())
138
138
  }
139
139
 
140
- return route.fulfill({
141
- status: response.status,
142
- headers: Object.fromEntries(response.headers),
143
- body: response.body
144
- ? Buffer.from(await response.arrayBuffer())
145
- : undefined,
140
+ return this.safelyHandleRoute(async () => {
141
+ return route.fulfill({
142
+ status: response.status,
143
+ headers: Object.fromEntries(response.headers),
144
+ body: response.body
145
+ ? Buffer.from(await response.arrayBuffer())
146
+ : undefined,
147
+ })
146
148
  })
147
149
  }
148
150
 
149
- return route.fallback()
151
+ return this.safelyHandleRoute(() => route.fallback())
150
152
  },
151
153
  )
152
154
 
@@ -201,6 +203,30 @@ class SetupPlaywrightApi extends SetupApi<LifeCycleEventsMap> {
201
203
  // Encode/decode to preserve escape characters.
202
204
  return decodeURI(new URL(encodeURI(url)).origin)
203
205
  }
206
+
207
+ private async safelyHandleRoute(
208
+ callback: () => Promise<void>,
209
+ ): Promise<void> {
210
+ try {
211
+ await callback()
212
+ } catch (error) {
213
+ /**
214
+ * @note Ignore "Route is already handled!" errors.
215
+ * Playwright has a bug where requests terminated due to navigation
216
+ * cause your in-flight route handlers to throw. There's no means to
217
+ * detect that scenario as both "route.handled" and "route._handlingPromise" are internal.
218
+ * @see https://github.com/mswjs/playwright/issues/35
219
+ */
220
+ if (
221
+ error instanceof Error &&
222
+ /route is already handled/i.test(error.message)
223
+ ) {
224
+ return
225
+ }
226
+
227
+ throw error
228
+ }
229
+ }
204
230
  }
205
231
 
206
232
  class PlaywrightWebSocketClientConnection implements WebSocketClientConnectionProtocol {