@@ -2,21 +2,18 @@ import { invariant } from 'outvariant'
22import type {
33 BrowserContext ,
44 Page ,
5- PlaywrightTestArgs ,
6- PlaywrightWorkerArgs ,
75 Request as PlaywrightRequest ,
86 Route ,
9- TestFixture ,
107 WebSocketRoute ,
118} from '@playwright/test'
9+ import { WebSocketHandler } from 'msw'
1210import {
13- type LifeCycleEventsMap ,
14- type UnhandledRequestStrategy ,
1511 SetupApi ,
16- RequestHandler ,
17- WebSocketHandler ,
1812 handleRequest ,
1913 isCommonAssetRequest ,
14+ type AnyHandler ,
15+ type LifeCycleEventsMap ,
16+ type UnhandledRequestStrategy ,
2017} from 'msw'
2118import {
2219 type WebSocketClientEventMap ,
@@ -27,9 +24,11 @@ import {
2724 WebSocketClientConnectionProtocol ,
2825 WebSocketServerConnectionProtocol ,
2926} from '@mswjs/interceptors/WebSocket'
27+ import { RequestHandler } from 'msw'
3028
31- export interface CreateNetworkFixtureArgs {
32- initialHandlers ?: Array < RequestHandler | WebSocketHandler >
29+ export interface NetworkFixtureOptions {
30+ context : BrowserContext
31+ handlers ?: Array < AnyHandler >
3332 onUnhandledRequest ?: UnhandledRequestStrategy
3433 /**
3534 * Skip common asset requests (e.g. `*.html`, `*.css`, `*.js`, etc).
@@ -41,46 +40,27 @@ export interface CreateNetworkFixtureArgs {
4140 skipAssetRequests ?: boolean
4241}
4342
44- /**
45- * Creates a fixture that controls the network in your tests.
46- *
47- * @note The returned fixture already has the `auto` option set to `true`.
48- *
49- * **Usage**
50- * ```ts
51- * import { test as testBase } from '@playwright/test'
52- * import { createNetworkFixture, type WorkerFixture } from '@msw/playwright'
53- *
54- * interface Fixtures {
55- * network: WorkerFixture
56- * }
57- *
58- * export const test = testBase.extend<Fixtures>({
59- * network: createNetworkFixture()
60- * })
61- * ```
62- */
63- export function createNetworkFixture (
64- args ?: CreateNetworkFixtureArgs ,
65- ) : [
66- TestFixture < NetworkFixture , PlaywrightTestArgs & PlaywrightWorkerArgs > ,
67- { auto : boolean } ,
68- ] {
69- return [
70- async ( { context } , use ) => {
71- const worker = new NetworkFixture ( {
72- context,
73- skipAssetRequests : args ?. skipAssetRequests ?? true ,
74- initialHandlers : args ?. initialHandlers || [ ] ,
75- onUnhandledRequest : args ?. onUnhandledRequest ,
76- } )
43+ export type NetworkFixture = Omit < SetupApi < LifeCycleEventsMap > , 'dispose' > & {
44+ enable : ( ) => Promise < void >
45+ disable : ( ) => Promise < void >
46+ }
47+
48+ export function defineNetworkFixture (
49+ options : NetworkFixtureOptions ,
50+ ) : NetworkFixture {
51+ return new SetupPlaywrightApi ( {
52+ context : options . context ,
53+ initialHandlers : options . handlers || [ ] ,
54+ onUnhandledRequest : options . onUnhandledRequest ,
55+ skipAssetRequests : options . skipAssetRequests ?? true ,
56+ } )
57+ }
7758
78- await worker . start ( )
79- await use ( worker )
80- await worker . stop ( )
81- } ,
82- { auto : true } ,
83- ]
59+ interface SetupPlaywrightOptions {
60+ context : BrowserContext
61+ initialHandlers : Array < AnyHandler >
62+ onUnhandledRequest ?: UnhandledRequestStrategy
63+ skipAssetRequests ?: boolean
8464}
8565
8666/**
@@ -91,21 +71,14 @@ export function createNetworkFixture(
9171 */
9272export const INTERNAL_MATCH_ALL_REG_EXP = / .+ ( _ _ M S W _ P L A Y W R I G H T _ P R E D I C A T E _ _ ) ? /
9373
94- export class NetworkFixture extends SetupApi < LifeCycleEventsMap > {
95- constructor (
96- protected args : {
97- context : BrowserContext
98- skipAssetRequests : boolean
99- initialHandlers : Array < RequestHandler | WebSocketHandler >
100- onUnhandledRequest ?: UnhandledRequestStrategy
101- } ,
102- ) {
103- super ( ...args . initialHandlers )
74+ class SetupPlaywrightApi extends SetupApi < LifeCycleEventsMap > {
75+ constructor ( private readonly options : SetupPlaywrightOptions ) {
76+ super ( ...options . initialHandlers )
10477 }
10578
106- public async start ( ) : Promise < void > {
79+ public async enable ( ) : Promise < void > {
10780 // Handle HTTP requests.
108- await this . args . context . route (
81+ await this . options . context . route (
10982 INTERNAL_MATCH_ALL_REG_EXP ,
11083 async ( route : Route , request : PlaywrightRequest ) => {
11184 const fetchRequest = new Request ( request . url ( ) , {
@@ -120,7 +93,10 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
12093 * requests through the matching logic below.
12194 * @see https://github.com/mswjs/playwright/issues/13
12295 */
123- if ( this . args . skipAssetRequests && isCommonAssetRequest ( fetchRequest ) ) {
96+ if (
97+ this . options . skipAssetRequests &&
98+ isCommonAssetRequest ( fetchRequest )
99+ ) {
124100 return route . continue ( )
125101 }
126102
@@ -143,7 +119,7 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
143119 crypto . randomUUID ( ) ,
144120 handlers ,
145121 {
146- onUnhandledRequest : this . args . onUnhandledRequest || 'bypass' ,
122+ onUnhandledRequest : this . options . onUnhandledRequest || 'bypass' ,
147123 } ,
148124 this . emitter ,
149125 {
@@ -173,7 +149,7 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
173149 )
174150
175151 // Handle WebSocket connections.
176- await this . args . context . routeWebSocket (
152+ await this . options . context . routeWebSocket (
177153 INTERNAL_MATCH_ALL_REG_EXP ,
178154 async ( route ) => {
179155 const allWebSocketHandlers = this . handlersController
@@ -190,7 +166,7 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
190166 const client = new PlaywrightWebSocketClientConnection ( route )
191167 const server = new PlaywrightWebSocketServerConnection ( route )
192168
193- const pages = this . args . context . pages ( )
169+ const pages = this . options . context . pages ( )
194170 const lastPage = pages [ pages . length - 1 ]
195171 const baseUrl = lastPage ? this . getPageUrl ( lastPage ) : undefined
196172
@@ -210,10 +186,10 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
210186 )
211187 }
212188
213- public async stop ( ) : Promise < void > {
189+ public async disable ( ) : Promise < void > {
214190 super . dispose ( )
215- await this . args . context . unroute ( INTERNAL_MATCH_ALL_REG_EXP )
216- await unrouteWebSocket ( this . args . context , INTERNAL_MATCH_ALL_REG_EXP )
191+ await this . options . context . unroute ( INTERNAL_MATCH_ALL_REG_EXP )
192+ await unrouteWebSocket ( this . options . context , INTERNAL_MATCH_ALL_REG_EXP )
217193 }
218194
219195 private getPageUrl ( page : Page ) : string | undefined {
0 commit comments