|
| 1 | +import { INestApplication } from '@nestjs/common'; |
| 2 | +import { Transport } from '@nestjs/microservices'; |
| 3 | +import { Test } from '@nestjs/testing'; |
| 4 | +import { expect } from 'chai'; |
| 5 | +import * as request from 'supertest'; |
| 6 | +import { Guard } from '../src/msvc/guards/request-scoped.guard'; |
| 7 | +import { HelloController } from '../src/msvc/hello.controller'; |
| 8 | +import { HelloModule } from '../src/msvc/hello.module'; |
| 9 | +import { Interceptor } from '../src/msvc/interceptors/logging.interceptor'; |
| 10 | +import { UsersService } from '../src/msvc/users/users.service'; |
| 11 | + |
| 12 | +class Meta { |
| 13 | + static COUNTER = 0; |
| 14 | + constructor() { |
| 15 | + Meta.COUNTER++; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +describe('Request scope (microservices)', () => { |
| 20 | + let server; |
| 21 | + let app: INestApplication; |
| 22 | + |
| 23 | + before(async () => { |
| 24 | + const module = await Test.createTestingModule({ |
| 25 | + imports: [ |
| 26 | + HelloModule.forRoot({ |
| 27 | + provide: 'META', |
| 28 | + useClass: Meta, |
| 29 | + }), |
| 30 | + ], |
| 31 | + }).compile(); |
| 32 | + |
| 33 | + app = module.createNestApplication(); |
| 34 | + app.connectMicroservice({ transport: Transport.TCP }); |
| 35 | + |
| 36 | + server = app.getHttpServer(); |
| 37 | + await app.init(); |
| 38 | + await app.startAllMicroservicesAsync(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('when one service is request scoped', () => { |
| 42 | + before(async () => { |
| 43 | + const performHttpCall = end => |
| 44 | + request(server) |
| 45 | + .get('/hello') |
| 46 | + .end((err, res) => { |
| 47 | + if (err) return end(err); |
| 48 | + end(); |
| 49 | + }); |
| 50 | + await new Promise(resolve => performHttpCall(resolve)); |
| 51 | + await new Promise(resolve => performHttpCall(resolve)); |
| 52 | + await new Promise(resolve => performHttpCall(resolve)); |
| 53 | + }); |
| 54 | + |
| 55 | + it(`should create controller for each request`, async () => { |
| 56 | + expect(HelloController.COUNTER).to.be.eql(3); |
| 57 | + }); |
| 58 | + |
| 59 | + it(`should create service for each request`, async () => { |
| 60 | + expect(UsersService.COUNTER).to.be.eql(3); |
| 61 | + }); |
| 62 | + |
| 63 | + it(`should share static provider across requests`, async () => { |
| 64 | + expect(Meta.COUNTER).to.be.eql(1); |
| 65 | + }); |
| 66 | + |
| 67 | + it(`should create request scoped interceptor for each request`, async () => { |
| 68 | + expect(Interceptor.COUNTER).to.be.eql(3); |
| 69 | + expect(Interceptor.REQUEST_SCOPED_DATA).to.deep.equal([1, 1, 1]); |
| 70 | + }); |
| 71 | + |
| 72 | + it(`should create request scoped guard for each request`, async () => { |
| 73 | + expect(Guard.COUNTER).to.be.eql(3); |
| 74 | + expect(Guard.REQUEST_SCOPED_DATA).to.deep.equal([1, 1, 1]); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + after(async () => { |
| 79 | + await app.close(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments