1
- import type { SetupWorker } from 'msw/lib/ browser'
1
+ import type { SetupWorker } from 'msw/browser'
2
2
import { HttpServer } from '@open-draft/test-server/http'
3
3
import type { ConsoleMessages } from 'page-with'
4
4
import { test , expect } from '../../../playwright.extend'
@@ -11,7 +11,20 @@ declare namespace window {
11
11
12
12
const ON_EXAMPLE = require . resolve ( './on.mocks.ts' )
13
13
14
- let server : HttpServer
14
+ const server = new HttpServer ( ( app ) => {
15
+ app . post ( '/no-response' , ( _req , res ) => {
16
+ res . send ( 'original-response' )
17
+ } )
18
+ app . get ( '/unknown-route' , ( _req , res ) => {
19
+ res . send ( 'majestic-unknown' )
20
+ } )
21
+ app . get ( '/passthrough' , ( _req , res ) => {
22
+ res . send ( 'passthrough-response' )
23
+ } )
24
+ app . post ( '/bypass' , ( _req , res ) => {
25
+ res . send ( 'bypassed-response' )
26
+ } )
27
+ } )
15
28
16
29
export function getRequestId ( messages : ConsoleMessages ) {
17
30
const requestStartMessage = messages . get ( 'warning' ) ?. find ( ( message ) => {
@@ -20,15 +33,12 @@ export function getRequestId(messages: ConsoleMessages) {
20
33
return requestStartMessage ?. split ( ' ' ) ?. [ 3 ]
21
34
}
22
35
23
- test . beforeEach ( async ( { createServer } ) => {
24
- server = await createServer ( ( app ) => {
25
- app . post ( '/no-response' , ( req , res ) => {
26
- res . send ( 'original-response' )
27
- } )
28
- app . get ( '/unknown-route' , ( req , res ) => {
29
- res . send ( 'majestic-unknown' )
30
- } )
31
- } )
36
+ test . beforeAll ( async ( ) => {
37
+ await server . listen ( )
38
+ } )
39
+
40
+ test . afterAll ( async ( ) => {
41
+ await server . close ( )
32
42
} )
33
43
34
44
test ( 'emits events for a handled request and mocked response' , async ( {
@@ -111,6 +121,74 @@ test('emits events for an unhandled request', async ({
111
121
] )
112
122
} )
113
123
124
+ test ( 'emits events for a passthrough request' , async ( {
125
+ loadExample,
126
+ spyOnConsole,
127
+ fetch,
128
+ waitFor,
129
+ } ) => {
130
+ const consoleSpy = spyOnConsole ( )
131
+ await loadExample ( ON_EXAMPLE )
132
+
133
+ // Explicit "passthrough()" request must go through the
134
+ // same request processing pipeline to contain both
135
+ // "request" and "response" in the life-cycle event listener.
136
+ const url = server . http . url ( '/passthrough' )
137
+ await fetch ( url )
138
+ const requestId = getRequestId ( consoleSpy )
139
+
140
+ await waitFor ( ( ) => {
141
+ expect ( consoleSpy . get ( 'warning' ) ) . toEqual ( [
142
+ `[request:start] GET ${ url } ${ requestId } ` ,
143
+ `[request:end] GET ${ url } ${ requestId } ` ,
144
+ `[response:bypass] ${ url } passthrough-response GET ${ url } ${ requestId } ` ,
145
+ ] )
146
+ } )
147
+ } )
148
+
149
+ test ( 'emits events for a bypassed request' , async ( {
150
+ loadExample,
151
+ spyOnConsole,
152
+ fetch,
153
+ waitFor,
154
+ page,
155
+ } ) => {
156
+ const consoleSpy = spyOnConsole ( )
157
+ await loadExample ( ON_EXAMPLE )
158
+
159
+ const pageErrors : Array < Error > = [ ]
160
+ page . on ( 'pageerror' , ( error ) => pageErrors . push ( error ) )
161
+
162
+ const url = server . http . url ( '/bypass' )
163
+ await fetch ( url )
164
+
165
+ await waitFor ( ( ) => {
166
+ // First, must print the events for the original (mocked) request.
167
+ expect ( consoleSpy . get ( 'warning' ) ) . toEqual (
168
+ expect . arrayContaining ( [
169
+ expect . stringContaining ( `[request:start] GET ${ url } ` ) ,
170
+ expect . stringContaining ( `[request:end] GET ${ url } ` ) ,
171
+ expect . stringContaining (
172
+ `[response:mocked] ${ url } bypassed-response GET ${ url } ` ,
173
+ ) ,
174
+ ] ) ,
175
+ )
176
+
177
+ // Then, must also print events for the bypassed request.
178
+ expect ( consoleSpy . get ( 'warning' ) ) . toEqual (
179
+ expect . arrayContaining ( [
180
+ expect . stringContaining ( `[request:start] POST ${ url } ` ) ,
181
+ expect . stringContaining ( `[request:end] POST ${ url } ` ) ,
182
+ expect . stringContaining (
183
+ `[response:bypass] ${ url } bypassed-response POST ${ url } ` ,
184
+ ) ,
185
+ ] ) ,
186
+ )
187
+
188
+ expect ( pageErrors ) . toEqual ( [ ] )
189
+ } )
190
+ } )
191
+
114
192
test ( 'emits unhandled exceptions in the request handler' , async ( {
115
193
loadExample,
116
194
spyOnConsole,