1- import { BadRequestException , Post } from '@nestjs/common' ;
1+ import { BadRequestException , Post , Module } from '@nestjs/common' ;
22import { expect } from 'chai' ;
33import * as sinon from 'sinon' ;
44import { Controller } from '../../../common/decorators/core/controller.decorator' ;
55import { Get } from '../../../common/decorators/http/request-mapping.decorator' ;
66import { ExpressAdapter } from '../../adapters/express-adapter' ;
77import { ApplicationConfig } from '../../application-config' ;
88import { RoutesResolver } from '../../router/routes-resolver' ;
9+ import { MODULE_PATH } from '@nestjs/common/constants' ;
910
1011describe ( 'RoutesResolver' , ( ) => {
12+
1113 @Controller ( 'global' )
1214 class TestRoute {
1315 @Get ( 'test' )
@@ -17,6 +19,16 @@ describe('RoutesResolver', () => {
1719 public anotherTest ( ) { }
1820 }
1921
22+ @Module ( {
23+ controllers : [ TestRoute ]
24+ } )
25+ class TestModule { }
26+
27+ @Module ( {
28+ controllers : [ TestRoute ]
29+ } )
30+ class TestModule2 { }
31+
2032 let router ;
2133 let routesResolver : RoutesResolver ;
2234 let container ;
@@ -97,6 +109,54 @@ describe('RoutesResolver', () => {
97109 ) ;
98110 expect ( spy . calledTwice ) . to . be . true ;
99111 } ) ;
112+
113+ describe ( 'registerRouters' , ( ) => {
114+ it ( 'should register each module with the base path and append the __module_path__ if present ' , ( ) => {
115+ const routes = new Map ( ) ;
116+ routes . set ( 'TestRoute' , {
117+ instance : new TestRoute ( ) ,
118+ metatype : TestRoute ,
119+ } ) ;
120+
121+ Reflect . defineMetadata ( MODULE_PATH , '/test' , TestModule ) ;
122+ modules . set ( 'TestModule' , { routes, metatype : TestModule } ) ;
123+ modules . set ( 'TestModule2' , { routes, metatype : TestModule2 } ) ;
124+
125+ const spy = sinon
126+ . stub ( routesResolver , 'registerRouters' )
127+ . callsFake ( ( ) => undefined ) ;
128+
129+ routesResolver . resolve ( applicationRef , 'api/v1' ) ;
130+
131+ // with module path
132+ expect ( spy . getCall ( 0 ) . calledWith ( sinon . match . any , sinon . match . any , 'api/v1/test' , sinon . match . any ) ) . to . be . true ;
133+ // without module path
134+ expect ( spy . getCall ( 1 ) . calledWith ( sinon . match . any , sinon . match . any , 'api/v1' , sinon . match . any ) ) . to . be . true ;
135+ } ) ;
136+
137+ it ( 'should register each module with __module_path__ if present and no basePath ' , ( ) => {
138+ const routes = new Map ( ) ;
139+ routes . set ( 'TestRoute' , {
140+ instance : new TestRoute ( ) ,
141+ metatype : TestRoute ,
142+ } ) ;
143+
144+ Reflect . defineMetadata ( MODULE_PATH , '/test' , TestModule ) ;
145+ modules . set ( 'TestModule' , { routes, metatype : TestModule } ) ;
146+ modules . set ( 'TestModule2' , { routes, metatype : TestModule2 } ) ;
147+
148+ const spy = sinon
149+ . stub ( routesResolver , 'registerRouters' )
150+ . callsFake ( ( ) => undefined ) ;
151+
152+ routesResolver . resolve ( applicationRef , '' ) ;
153+
154+ // with module path
155+ expect ( spy . getCall ( 0 ) . calledWith ( sinon . match . any , sinon . match . any , '/test' , sinon . match . any ) ) . to . be . true ;
156+ // without module path
157+ expect ( spy . getCall ( 1 ) . calledWith ( sinon . match . any , sinon . match . any , '' , sinon . match . any ) ) . to . be . true ;
158+ } ) ;
159+ } )
100160 } ) ;
101161
102162 describe ( 'mapExternalExceptions' , ( ) => {
0 commit comments