@@ -546,20 +546,20 @@ describe('ServerKafka', () => {
546546
547547 let perTopicServer : ServerKafka ;
548548 let perTopicUntyped : any ;
549- let perTopicConnect : ReturnType < typeof vi . fn > ;
550- let perTopicSubscribe : ReturnType < typeof vi . fn > ;
551- let perTopicRun : ReturnType < typeof vi . fn > ;
552- let perTopicOn : ReturnType < typeof vi . fn > ;
553- let perTopicConsumerFactory : ReturnType < typeof vi . fn > ;
549+ let perTopicConnect : sinon . SinonStub ;
550+ let perTopicSubscribe : sinon . SinonStub ;
551+ let perTopicRun : sinon . SinonStub ;
552+ let perTopicOn : sinon . SinonStub ;
553+ let perTopicConsumerFactory : sinon . SinonStub ;
554554
555555 beforeEach ( ( ) => {
556556 perTopicServer = new ServerKafka ( { topicConsumers : true } ) ;
557557 perTopicUntyped = perTopicServer as any ;
558558
559- perTopicConnect = vi . fn ( ) ;
560- perTopicSubscribe = vi . fn ( ) ;
561- perTopicRun = vi . fn ( ) ;
562- perTopicOn = vi . fn ( ) ;
559+ perTopicConnect = sinon . stub ( ) ;
560+ perTopicSubscribe = sinon . stub ( ) ;
561+ perTopicRun = sinon . stub ( ) ;
562+ perTopicOn = sinon . stub ( ) ;
563563
564564 const mockConsumer = ( ) => ( {
565565 connect : perTopicConnect ,
@@ -569,160 +569,157 @@ describe('ServerKafka', () => {
569569 events : mockConsumerEvents ,
570570 } ) ;
571571
572- perTopicConsumerFactory = vi . fn ( ) . mockImplementation ( mockConsumer ) ;
573-
574- vi . spyOn ( perTopicServer , 'createClient' ) . mockImplementation (
575- async ( ) =>
576- ( {
577- consumer : perTopicConsumerFactory ,
578- producer : vi . fn ( ) . mockReturnValue ( {
579- connect : perTopicConnect ,
580- send : vi . fn ( ) ,
581- on : perTopicOn ,
582- events : {
583- CONNECT : 'producer.connect' ,
584- DISCONNECT : 'producer.disconnect' ,
585- } ,
586- } ) ,
587- } ) as any ,
588- ) ;
572+ perTopicConsumerFactory = sinon . stub ( ) . callsFake ( mockConsumer ) ;
573+
574+ sinon . stub ( perTopicServer , 'createClient' ) . resolves ( {
575+ consumer : perTopicConsumerFactory ,
576+ producer : sinon . stub ( ) . returns ( {
577+ connect : perTopicConnect ,
578+ send : sinon . stub ( ) ,
579+ on : perTopicOn ,
580+ events : {
581+ CONNECT : 'producer.connect' ,
582+ DISCONNECT : 'producer.disconnect' ,
583+ } ,
584+ } ) ,
585+ } as any ) ;
589586 } ) ;
590587
591- afterEach ( ( ) => vi . restoreAllMocks ( ) ) ;
588+ afterEach ( ( ) => sinon . restore ( ) ) ;
592589
593590 describe ( 'bindEventsPerTopic' , ( ) => {
594591 it ( 'should create a separate consumer for each registered topic' , async ( ) => {
595592 perTopicUntyped . messageHandlers = objectToMap ( {
596- 'topic-a' : vi . fn ( ) ,
597- 'topic-b' : vi . fn ( ) ,
593+ 'topic-a' : sinon . stub ( ) ,
594+ 'topic-b' : sinon . stub ( ) ,
598595 } ) ;
599596
600- await perTopicServer . listen ( vi . fn ( ) ) ;
597+ await perTopicServer . listen ( sinon . stub ( ) ) ;
601598
602- expect ( perTopicConsumerFactory ) . toHaveBeenCalledTimes ( 2 ) ;
599+ expect ( perTopicConsumerFactory . callCount ) . to . equal ( 2 ) ;
603600 } ) ;
604601
605602 it ( 'should suffix groupId with topic name for each consumer' , async ( ) => {
606603 perTopicUntyped . messageHandlers = objectToMap ( {
607- 'topic-a' : vi . fn ( ) ,
608- 'topic-b' : vi . fn ( ) ,
604+ 'topic-a' : sinon . stub ( ) ,
605+ 'topic-b' : sinon . stub ( ) ,
609606 } ) ;
610607
611- await perTopicServer . listen ( vi . fn ( ) ) ;
608+ await perTopicServer . listen ( sinon . stub ( ) ) ;
612609
613- const groupIds = perTopicConsumerFactory . mock . calls . map (
610+ const groupIds = perTopicConsumerFactory . args . map (
614611 args => args [ 0 ] . groupId ,
615612 ) ;
616- expect ( groupIds . some ( id => id . endsWith ( '-topic-a' ) ) ) . toBe ( true ) ;
617- expect ( groupIds . some ( id => id . endsWith ( '-topic-b' ) ) ) . toBe ( true ) ;
613+ expect ( groupIds . some ( id => id . endsWith ( '-topic-a' ) ) ) . to . be . true ;
614+ expect ( groupIds . some ( id => id . endsWith ( '-topic-b' ) ) ) . to . be . true ;
618615 } ) ;
619616
620617 it ( 'should subscribe each consumer to exactly one topic' , async ( ) => {
621618 perTopicUntyped . messageHandlers = objectToMap ( {
622- 'topic-a' : vi . fn ( ) ,
623- 'topic-b' : vi . fn ( ) ,
619+ 'topic-a' : sinon . stub ( ) ,
620+ 'topic-b' : sinon . stub ( ) ,
624621 } ) ;
625622
626- await perTopicServer . listen ( vi . fn ( ) ) ;
623+ await perTopicServer . listen ( sinon . stub ( ) ) ;
627624
628- expect ( perTopicSubscribe ) . toHaveBeenCalledTimes ( 2 ) ;
629- perTopicSubscribe . mock . calls . forEach ( args => {
630- expect ( args [ 0 ] . topics . length ) . toEqual ( 1 ) ;
625+ expect ( perTopicSubscribe . callCount ) . to . equal ( 2 ) ;
626+ perTopicSubscribe . args . forEach ( args => {
627+ expect ( args [ 0 ] . topics . length ) . to . equal ( 1 ) ;
631628 } ) ;
632- const subscribedTopics = perTopicSubscribe . mock . calls
629+ const subscribedTopics = perTopicSubscribe . args
633630 . map ( args => args [ 0 ] . topics [ 0 ] )
634631 . sort ( ) ;
635- expect ( subscribedTopics ) . toEqual ( [ 'topic-a' , 'topic-b' ] ) ;
632+ expect ( subscribedTopics ) . to . deep . equal ( [ 'topic-a' , 'topic-b' ] ) ;
636633 } ) ;
637634
638635 it ( 'should call run on each per-topic consumer' , async ( ) => {
639636 perTopicUntyped . messageHandlers = objectToMap ( {
640- 'topic-a' : vi . fn ( ) ,
641- 'topic-b' : vi . fn ( ) ,
637+ 'topic-a' : sinon . stub ( ) ,
638+ 'topic-b' : sinon . stub ( ) ,
642639 } ) ;
643640
644- await perTopicServer . listen ( vi . fn ( ) ) ;
641+ await perTopicServer . listen ( sinon . stub ( ) ) ;
645642
646- expect ( perTopicRun ) . toHaveBeenCalledTimes ( 2 ) ;
647- perTopicRun . mock . calls . forEach ( args => {
648- expect ( args [ 0 ] ) . toHaveProperty ( 'eachMessage' ) ;
643+ expect ( perTopicRun . callCount ) . to . equal ( 2 ) ;
644+ perTopicRun . args . forEach ( args => {
645+ expect ( args [ 0 ] ) . to . have . property ( 'eachMessage' ) ;
649646 } ) ;
650647 } ) ;
651648
652649 it ( 'should populate consumers map with one entry per topic' , async ( ) => {
653650 perTopicUntyped . messageHandlers = objectToMap ( {
654- 'topic-a' : vi . fn ( ) ,
655- 'topic-b' : vi . fn ( ) ,
651+ 'topic-a' : sinon . stub ( ) ,
652+ 'topic-b' : sinon . stub ( ) ,
656653 } ) ;
657654
658- await perTopicServer . listen ( vi . fn ( ) ) ;
655+ await perTopicServer . listen ( sinon . stub ( ) ) ;
659656
660- expect ( perTopicUntyped . consumers . size ) . toEqual ( 2 ) ;
661- expect ( perTopicUntyped . consumers . has ( 'topic-a' ) ) . toBe ( true ) ;
662- expect ( perTopicUntyped . consumers . has ( 'topic-b' ) ) . toBe ( true ) ;
657+ expect ( perTopicUntyped . consumers . size ) . to . equal ( 2 ) ;
658+ expect ( perTopicUntyped . consumers . has ( 'topic-a' ) ) . to . be . true ;
659+ expect ( perTopicUntyped . consumers . has ( 'topic-b' ) ) . to . be . true ;
663660 } ) ;
664661
665662 it ( 'should not create any consumer when there are no messageHandlers' , async ( ) => {
666- await perTopicServer . listen ( vi . fn ( ) ) ;
663+ await perTopicServer . listen ( sinon . stub ( ) ) ;
667664
668- expect ( perTopicConsumerFactory ) . not . toHaveBeenCalled ( ) ;
669- expect ( perTopicUntyped . consumers . size ) . toEqual ( 0 ) ;
665+ expect ( perTopicConsumerFactory . called ) . to . be . false ;
666+ expect ( perTopicUntyped . consumers . size ) . to . equal ( 0 ) ;
670667 } ) ;
671668
672669 it ( 'should clean up connected consumers and rethrow when a topic connect fails' , async ( ) => {
673- const disconnectOk = vi . fn ( ) ;
670+ const disconnectOk = sinon . stub ( ) ;
674671 const connectError = new Error ( 'connect failed' ) ;
675672 let callCount = 0 ;
676673
677- perTopicConsumerFactory . mockImplementation ( ( ) => ( {
678- connect : vi . fn ( ) . mockImplementation ( ( ) => {
674+ perTopicConsumerFactory . callsFake ( ( ) => ( {
675+ connect : sinon . stub ( ) . callsFake ( ( ) => {
679676 callCount ++ ;
680677 if ( callCount === 2 ) throw connectError ;
681678 } ) ,
682- subscribe : vi . fn ( ) ,
683- run : vi . fn ( ) ,
679+ subscribe : sinon . stub ( ) ,
680+ run : sinon . stub ( ) ,
684681 on : perTopicOn ,
685682 events : mockConsumerEvents ,
686683 disconnect : disconnectOk ,
687684 } ) ) ;
688685
689686 perTopicUntyped . messageHandlers = objectToMap ( {
690- 'topic-a' : vi . fn ( ) ,
691- 'topic-b' : vi . fn ( ) ,
687+ 'topic-a' : sinon . stub ( ) ,
688+ 'topic-b' : sinon . stub ( ) ,
692689 } ) ;
693690
694- const cb = vi . fn ( ) ;
691+ const cb = sinon . stub ( ) ;
695692 await perTopicServer . listen ( cb ) ;
696693
697- expect ( cb ) . toHaveBeenCalledWith ( connectError ) ;
698- expect ( disconnectOk ) . toHaveBeenCalledOnce ( ) ;
699- expect ( perTopicUntyped . consumers . size ) . toEqual ( 0 ) ;
694+ expect ( cb . calledWith ( connectError ) ) . to . be . true ;
695+ expect ( disconnectOk . calledOnce ) . to . be . true ;
696+ expect ( perTopicUntyped . consumers . size ) . to . equal ( 0 ) ;
700697 } ) ;
701698 } ) ;
702699
703700 describe ( 'close with topicConsumers' , ( ) => {
704701 it ( 'should disconnect all per-topic consumers and null refs' , async ( ) => {
705- const disconnectA = vi . fn ( ) ;
706- const disconnectB = vi . fn ( ) ;
702+ const disconnectA = sinon . stub ( ) ;
703+ const disconnectB = sinon . stub ( ) ;
707704 perTopicUntyped . consumers = new Map ( [
708705 [ 'topic-a' , { disconnect : disconnectA } ] ,
709706 [ 'topic-b' , { disconnect : disconnectB } ] ,
710707 ] ) ;
711- perTopicUntyped . producer = { disconnect : vi . fn ( ) } ;
708+ perTopicUntyped . producer = { disconnect : sinon . stub ( ) } ;
712709
713710 await perTopicServer . close ( ) ;
714711
715- expect ( disconnectA ) . toHaveBeenCalledOnce ( ) ;
716- expect ( disconnectB ) . toHaveBeenCalledOnce ( ) ;
717- expect ( perTopicUntyped . consumers . size ) . toEqual ( 0 ) ;
718- expect ( perTopicUntyped . producer ) . toBeNull ( ) ;
719- expect ( perTopicUntyped . client ) . toBeNull ( ) ;
712+ expect ( disconnectA . calledOnce ) . to . be . true ;
713+ expect ( disconnectB . calledOnce ) . to . be . true ;
714+ expect ( perTopicUntyped . consumers . size ) . to . equal ( 0 ) ;
715+ expect ( perTopicUntyped . producer ) . to . be . null ;
716+ expect ( perTopicUntyped . client ) . to . be . null ;
720717 } ) ;
721718 } ) ;
722719 } ) ;
723720
724721 describe ( 'createClient' , ( ) => {
725- it ( 'should accept a custom logCreator in client options' , ( ) => {
722+ it ( 'should accept a custom logCreator in client options' , async ( ) => {
726723 const logCreatorSpy = sinon . spy ( ( ) => 'test' ) ;
727724 const logCreator = ( ) => logCreatorSpy ;
728725
@@ -736,21 +733,16 @@ describe('ServerKafka', () => {
736733 }
737734 }
738735
739- vi . spyOn (
740- ServerKafka . prototype as any ,
741- 'loadPackage' ,
742- ) . mockResolvedValueOnce ( {
743- Kafka : MockKafka ,
744- } ) ;
745-
746736 server = new ServerKafka ( {
747737 client : {
748738 brokers : [ ] ,
749739 logCreator,
750740 } ,
751741 } ) ;
742+ sinon . stub ( server as any , 'loadPackage' ) . resolves ( { Kafka : MockKafka } ) ;
752743
753- const logger = server . createClient ( ) . logger ( ) ;
744+ const kafkaClient = await server . createClient ( ) ;
745+ const logger = kafkaClient . logger ( ) ;
754746
755747 logger . info ( { namespace : '' , level : 1 , log : 'test' } ) ;
756748
0 commit comments