|
| 1 | +import { Controller, Get, INestApplication, Module } from '@nestjs/common'; |
| 2 | +import { RouterModule, Routes } from '@nestjs/core'; |
| 3 | +import { Test } from '@nestjs/testing'; |
| 4 | +import * as request from 'supertest'; |
| 5 | + |
| 6 | +describe('RouterModule', () => { |
| 7 | + let app: INestApplication; |
| 8 | + |
| 9 | + abstract class BaseController { |
| 10 | + @Get() |
| 11 | + getName() { |
| 12 | + return this.constructor.name; |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + @Controller('/parent-controller') |
| 17 | + class ParentController extends BaseController {} |
| 18 | + @Controller('/child-controller') |
| 19 | + class ChildController extends BaseController {} |
| 20 | + @Controller('no-slash-controller') |
| 21 | + class NoSlashController extends BaseController {} |
| 22 | + |
| 23 | + class UnknownController {} |
| 24 | + @Module({ controllers: [ParentController] }) |
| 25 | + class ParentModule {} |
| 26 | + |
| 27 | + @Module({ controllers: [ChildController] }) |
| 28 | + class ChildModule {} |
| 29 | + |
| 30 | + @Module({}) |
| 31 | + class AuthModule {} |
| 32 | + @Module({}) |
| 33 | + class PaymentsModule {} |
| 34 | + |
| 35 | + @Module({ controllers: [NoSlashController] }) |
| 36 | + class NoSlashModule {} |
| 37 | + |
| 38 | + const routes1: Routes = [ |
| 39 | + { |
| 40 | + path: 'parent', |
| 41 | + module: ParentModule, |
| 42 | + children: [ |
| 43 | + { |
| 44 | + path: 'child', |
| 45 | + module: ChildModule, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + ]; |
| 50 | + const routes2: Routes = [ |
| 51 | + { path: 'v1', children: [AuthModule, PaymentsModule, NoSlashModule] }, |
| 52 | + ]; |
| 53 | + |
| 54 | + @Module({ |
| 55 | + imports: [ParentModule, ChildModule, RouterModule.register(routes1)], |
| 56 | + }) |
| 57 | + class MainModule {} |
| 58 | + |
| 59 | + @Module({ |
| 60 | + imports: [ |
| 61 | + AuthModule, |
| 62 | + PaymentsModule, |
| 63 | + NoSlashModule, |
| 64 | + RouterModule.register(routes2), |
| 65 | + ], |
| 66 | + }) |
| 67 | + class AppModule {} |
| 68 | + |
| 69 | + before(async () => { |
| 70 | + const moduleRef = await Test.createTestingModule({ |
| 71 | + imports: [MainModule, AppModule], |
| 72 | + }).compile(); |
| 73 | + |
| 74 | + app = moduleRef.createNestApplication(); |
| 75 | + await app.init(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should hit the "ParentController"', async () => { |
| 79 | + return request(app.getHttpServer()) |
| 80 | + .get('/parent/parent-controller') |
| 81 | + .expect(200, 'ParentController'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should hit the "ChildController"', async () => { |
| 85 | + return request(app.getHttpServer()) |
| 86 | + .get('/parent/child/child-controller') |
| 87 | + .expect(200, 'ChildController'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should hit the "NoSlashController"', async () => { |
| 91 | + return request(app.getHttpServer()) |
| 92 | + .get('/v1/no-slash-controller') |
| 93 | + .expect(200, 'NoSlashController'); |
| 94 | + }); |
| 95 | + |
| 96 | + afterEach(async () => { |
| 97 | + await app.close(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments