@msw/playwright 0.4.0 → 0.4.1

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/README.md CHANGED
@@ -65,3 +65,17 @@ test('displays the user dashboard', async ({ network, page }) => {
65
65
  await page.goto('/dashboard')
66
66
  })
67
67
  ```
68
+
69
+ ## Comparison
70
+
71
+ ### `playwright-msw`
72
+
73
+ [`playwright-msw`](https://github.com/valendres/playwright-msw) is a community package that, just like `@msw/playwright`, aims to provide a better experience when mocking APIs in your Playwright tests.
74
+
75
+ > While `playwright-msw` is a fantastic tool and a huge inspiration for this package to exist, I believe it approaches the idea at a rather complex angle. That introduces a layer of abstraction that is subjected to the "left behind" problem as it needs to map to any MSW changes explicitly.
76
+
77
+ | | `playwright-msw` | `@msw/playwright` |
78
+ | -------------- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
79
+ | Initialization | `createWorkerFixture()` is used as a _part_ of your custom fixture. | `createNetworkFixture()` creates _the entire_ fixture for you, pre-configured. |
80
+ | Implementation | Uses a custom router to match handlers and a custom wrapper around `SetupWorker` API. | Uses MSW directly. Uses `page.route()` as the source of the network to route through the handlers. |
81
+ | Feature set | Supports `http` and `graphql` namespaces. | Supports all namespaces (`http`, `graphql`, `ws`, any other APIs exposed by MSW in the future). |
package/build/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LifeCycleEventsMap, RequestHandler, SetupApi, WebSocketHandler } from "msw";
2
- import { Page, TestFixture } from "@playwright/test";
2
+ import { Page, PlaywrightTestArgs, PlaywrightWorkerArgs, TestFixture } from "@playwright/test";
3
3
 
4
4
  //#region src/index.d.ts
5
5
  interface CreateNetworkFixtureArgs {
@@ -24,7 +24,7 @@ interface CreateNetworkFixtureArgs {
24
24
  * })
25
25
  * ```
26
26
  */
27
- declare function createNetworkFixture(args?: CreateNetworkFixtureArgs): [TestFixture<NetworkFixture, any>, {
27
+ declare function createNetworkFixture(args?: CreateNetworkFixtureArgs): [TestFixture<NetworkFixture, PlaywrightTestArgs & PlaywrightWorkerArgs>, {
28
28
  auto: boolean;
29
29
  }];
30
30
  declare class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
package/build/index.js CHANGED
@@ -50,6 +50,10 @@ var NetworkFixture = class extends SetupApi {
50
50
  return handler instanceof RequestHandler;
51
51
  }), fetchRequest);
52
52
  if (response) {
53
+ if (response.status === 0) {
54
+ route.abort();
55
+ return;
56
+ }
53
57
  route.fulfill({
54
58
  status: response.status,
55
59
  headers: Object.fromEntries(response.headers),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@msw/playwright",
4
- "version": "0.4.0",
4
+ "version": "0.4.1",
5
5
  "description": "Mock Service Worker binding for Playwright",
6
6
  "main": "./build/index.js",
7
7
  "types": "./build/index.d.ts",
package/src/index.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import { invariant } from 'outvariant'
2
- import type { Page, TestFixture, WebSocketRoute } from '@playwright/test'
2
+ import type {
3
+ Page,
4
+ PlaywrightTestArgs,
5
+ PlaywrightWorkerArgs,
6
+ TestFixture,
7
+ WebSocketRoute,
8
+ } from '@playwright/test'
3
9
  import {
4
10
  type LifeCycleEventsMap,
5
11
  SetupApi,
@@ -43,7 +49,10 @@ export interface CreateNetworkFixtureArgs {
43
49
  export function createNetworkFixture(
44
50
  args?: CreateNetworkFixtureArgs,
45
51
  /** @todo `onUnhandledRequest`? */
46
- ): [TestFixture<NetworkFixture, any>, { auto: boolean }] {
52
+ ): [
53
+ TestFixture<NetworkFixture, PlaywrightTestArgs & PlaywrightWorkerArgs>,
54
+ { auto: boolean },
55
+ ] {
47
56
  return [
48
57
  async ({ page }, use) => {
49
58
  const worker = new NetworkFixture({
@@ -87,6 +96,11 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
87
96
  )
88
97
 
89
98
  if (response) {
99
+ if (response.status === 0) {
100
+ route.abort()
101
+ return
102
+ }
103
+
90
104
  route.fulfill({
91
105
  status: response.status,
92
106
  headers: Object.fromEntries(response.headers),