@@ -679,6 +679,54 @@ describe('MongoMemoryServer', () => {
679679 expect ( readdirSpy ) . toHaveBeenCalledTimes ( 1 ) ;
680680 expect ( options . createAuth ) . toEqual ( false ) ;
681681 } ) ;
682+
683+ it ( 'should generate a port when no suggestion is defined' , async ( ) => {
684+ const mongoServer = new MongoMemoryServer ( ) ;
685+ const newPortSpy = jest
686+ // @ts -expect-error "getNewPort" is protected
687+ . spyOn ( mongoServer , 'getNewPort' )
688+ // @ts -expect-error it somehow gets a wrong function type
689+ . mockImplementation ( ( port ) => Promise . resolve ( port ? port : 2000 ) ) ;
690+
691+ // @ts -expect-error "getStartOptions" is protected
692+ const options = await mongoServer . getStartOptions ( ) ;
693+
694+ expect ( newPortSpy ) . toHaveBeenCalledTimes ( 1 ) ;
695+ expect ( typeof options . data . port === 'number' ) . toBeTruthy ( ) ;
696+ } ) ;
697+
698+ it ( 'should use a predefined port as a suggestion for a port' , async ( ) => {
699+ const predefinedPort = 10000 ;
700+
701+ const mongoServer = new MongoMemoryServer ( { instance : { port : predefinedPort } } ) ;
702+ const newPortSpy = jest
703+ // @ts -expect-error "getNewPort" is protected
704+ . spyOn ( mongoServer , 'getNewPort' )
705+ // @ts -expect-error it somehow gets a wrong function type
706+ . mockImplementation ( ( port ) => Promise . resolve ( port ? port : 2000 ) ) ;
707+
708+ // @ts -expect-error "getStartOptions" is protected
709+ const options = await mongoServer . getStartOptions ( ) ;
710+
711+ expect ( newPortSpy ) . toHaveBeenCalledWith ( predefinedPort ) ;
712+ expect ( options . data . port ) . toStrictEqual ( predefinedPort ) ;
713+ } ) ;
714+
715+ it ( 'should use a predefined port for a port with "forceSamePort" on' , async ( ) => {
716+ const predefinedPort = 30000 ;
717+
718+ const mongoServer = new MongoMemoryServer ( { instance : { port : predefinedPort } } ) ;
719+ const newPortSpy = jest
720+ // @ts -expect-error "getNewPort" is protected
721+ . spyOn ( mongoServer , 'getNewPort' )
722+ . mockImplementation ( ( ) => fail ( 'Expected this function to not be called' ) ) ;
723+
724+ // @ts -expect-error "getStartOptions" is protected
725+ const options = await mongoServer . getStartOptions ( true ) ;
726+
727+ expect ( newPortSpy ) . not . toHaveBeenCalled ( ) ;
728+ expect ( options . data . port ) . toStrictEqual ( predefinedPort ) ;
729+ } ) ;
682730 } ) ;
683731
684732 it ( '"getDbPath" should return the dbPath' , async ( ) => {
0 commit comments