@@ -4,23 +4,30 @@ export default async function mockRequests(
44 page : Page ,
55 overrides ?: Record < string , any >
66) {
7- const mocks = {
7+ const mocks : Record < string , any > = {
88 ...MOCKS ,
99 ...overrides ,
1010 } ;
1111
12- Object . entries ( mocks ) . forEach ( ( [ path , response ] ) => {
13- const handler = typeof response === "function" ? response : ( ) => response ;
12+ await page . route ( "**/api/**" , ( route ) => {
13+ const url = route . request ( ) . url ( ) ;
14+ const rawPath = url . split ( "/api/" ) [ 1 ] || "" ;
15+ const normalized = rawPath . split ( "?" ) [ 0 ] . replace ( / \/ $ / , "" ) . toLowerCase ( ) ;
1416
15- page . route ( `**/api/${ path } **` , ( route ) => {
16- const mockResponse = handler ( ) ;
17-
18- route . fulfill ( {
17+ const key = Object . keys ( mocks ) . find ( ( k ) => k . toLowerCase ( ) === normalized ) ;
18+ if ( key ) {
19+ const response = mocks [ key ] ;
20+ const handler =
21+ typeof response === "function" ? response : ( ) => response ;
22+ return route . fulfill ( {
1923 status : 200 ,
2024 contentType : "application/json" ,
21- body : JSON . stringify ( mockResponse ) ,
25+ body : JSON . stringify ( handler ( ) ) ,
2226 } ) ;
23- } ) ;
27+ }
28+
29+ console . warn ( "[mocks] no mock for /api/" + normalized ) ;
30+ return route . continue ( ) ;
2431 } ) ;
2532}
2633
@@ -43,6 +50,7 @@ const MOCKS = {
4350 emailConfirmed : true ,
4451 } ,
4552 } ,
53+ "accounts/login" : makeLoginResponse ( ) ,
4654 "accounts/getProfile" : {
4755 isSuccess : true ,
4856 user : {
@@ -62,7 +70,6 @@ const MOCKS = {
6270 features : [ ] ,
6371 } ,
6472 } ,
65- "accounts/login" : makeLoginResponse ( ) ,
6673 "accounts/resendConfirmationEmail" : {
6774 isSuccess : true ,
6875 code : "REG_SUCCESS" ,
0 commit comments