1- import { vi , describe , afterEach , it , expect } from 'vitest'
1+ import { vi , describe , afterEach , beforeEach , it , expect } from 'vitest'
22import yargs from 'yargs/yargs'
33
44import * as apiServerCLIConfig from '@cedarjs/api-server/apiCliConfig'
55import * as bothServerCLIConfig from '@cedarjs/api-server/bothCliConfig'
6+ import * as apiServerCLIConfigHandler from '@cedarjs/api-server/cjs/apiCliConfigHandler'
67
78import { builder } from '../serve.js'
89
910globalThis . __dirname = __dirname
1011
12+ const mocks = vi . hoisted ( ( ) => ( {
13+ isEsm : true ,
14+ } ) )
15+
1116// We mock these to skip the check for web/dist and api/dist
1217vi . mock ( '@cedarjs/project-config' , async ( importOriginal ) => {
1318 const originalProjectConfig = await importOriginal ( )
@@ -31,6 +36,7 @@ vi.mock('@cedarjs/project-config', async (importOriginal) => {
3136 api : { } ,
3237 }
3338 } ,
39+ projectIsEsm : ( ) => mocks . isEsm ,
3440 }
3541} )
3642
@@ -58,6 +64,11 @@ vi.mock('@cedarjs/api-server/apiCliConfig', async (importOriginal) => {
5864 handler : vi . fn ( ) ,
5965 }
6066} )
67+ vi . mock ( '@cedarjs/api-server/cjs/apiCliConfigHandler' , async ( ) => {
68+ return {
69+ handler : vi . fn ( ) ,
70+ }
71+ } )
6172vi . mock ( '@cedarjs/api-server/bothCliConfig' , async ( importOriginal ) => {
6273 const originalBothCLIConfig = await importOriginal ( )
6374 return {
@@ -74,6 +85,10 @@ vi.mock('execa', () => ({
7485} ) )
7586
7687describe ( 'yarn rw serve' , ( ) => {
88+ beforeEach ( ( ) => {
89+ mocks . isEsm = true
90+ } )
91+
7792 afterEach ( ( ) => {
7893 vi . clearAllMocks ( )
7994 } )
@@ -91,6 +106,21 @@ describe('yarn rw serve', () => {
91106 )
92107 } )
93108
109+ it ( 'Should proxy serve api with params to api-server handler for CJS projects' , async ( ) => {
110+ mocks . isEsm = false
111+
112+ const parser = yargs ( ) . command ( 'serve [side]' , false , builder )
113+
114+ await parser . parse ( 'serve api --port 5555 --apiRootPath funkyFunctions' )
115+
116+ expect ( apiServerCLIConfigHandler . handler ) . toHaveBeenCalledWith (
117+ expect . objectContaining ( {
118+ port : 5555 ,
119+ apiRootPath : expect . stringMatching ( / ^ \/ ? f u n k y F u n c t i o n s \/ ? $ / ) ,
120+ } ) ,
121+ )
122+ } )
123+
94124 it ( 'Should proxy serve api with params to api-server handler (alias and slashes in path)' , async ( ) => {
95125 const parser = yargs ( ) . command ( 'serve [side]' , false , builder )
96126
@@ -106,6 +136,23 @@ describe('yarn rw serve', () => {
106136 )
107137 } )
108138
139+ it ( 'Should proxy serve api with params to api-server handler (alias and slashes in path) for CJS projects' , async ( ) => {
140+ mocks . isEsm = false
141+
142+ const parser = yargs ( ) . command ( 'serve [side]' , false , builder )
143+
144+ await parser . parse (
145+ 'serve api --port 5555 --rootPath funkyFunctions/nested/' ,
146+ )
147+
148+ expect ( apiServerCLIConfigHandler . handler ) . toHaveBeenCalledWith (
149+ expect . objectContaining ( {
150+ port : 5555 ,
151+ rootPath : expect . stringMatching ( / ^ \/ ? f u n k y F u n c t i o n s \/ n e s t e d \/ $ / ) ,
152+ } ) ,
153+ )
154+ } )
155+
109156 it ( 'Should proxy rw serve with params to appropriate handler' , async ( ) => {
110157 const parser = yargs ( ) . command ( 'serve [side]' , false , builder )
111158
0 commit comments