|
| 1 | +import { INestApplication, Logger } from '@nestjs/common'; |
| 2 | +import { Test } from '@nestjs/testing'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import * as request from 'supertest'; |
| 5 | +import * as sinon from 'sinon'; |
| 6 | +import { HelloModule } from '../src/inject-inquirer/hello.module'; |
| 7 | + |
| 8 | +describe('Inject Inquirer', () => { |
| 9 | + let logger; |
| 10 | + |
| 11 | + let server; |
| 12 | + let app: INestApplication; |
| 13 | + |
| 14 | + before(async () => { |
| 15 | + logger = { log: sinon.spy() }; |
| 16 | + |
| 17 | + const module = await Test.createTestingModule({ |
| 18 | + imports: [HelloModule], |
| 19 | + }) |
| 20 | + .overrideProvider(Logger) |
| 21 | + .useValue(logger) |
| 22 | + .compile(); |
| 23 | + |
| 24 | + app = module.createNestApplication(); |
| 25 | + server = app.getHttpServer(); |
| 26 | + await app.init(); |
| 27 | + }); |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + logger.log.reset(); |
| 31 | + }); |
| 32 | + |
| 33 | + it(`should allow the injection of the inquirer in a Transient Scope`, async () => { |
| 34 | + await request(server).get('/hello/transient'); |
| 35 | + |
| 36 | + expect( |
| 37 | + logger.log.calledWith({ |
| 38 | + message: 'Hello transient!', |
| 39 | + feature: 'transient', |
| 40 | + }), |
| 41 | + ).to.be.true; |
| 42 | + }); |
| 43 | + |
| 44 | + it(`should allow the injection of the inquirer in a Request Scope`, async () => { |
| 45 | + await request(server).get('/hello/request'); |
| 46 | + |
| 47 | + expect( |
| 48 | + logger.log.calledWith({ |
| 49 | + message: 'Hello request!', |
| 50 | + requestId: sinon.match.string, |
| 51 | + feature: 'request', |
| 52 | + }), |
| 53 | + ).to.be.true; |
| 54 | + |
| 55 | + const requestId = logger.log.getCall(0).args[0].requestId; |
| 56 | + |
| 57 | + expect( |
| 58 | + logger.log.calledWith({ |
| 59 | + message: 'Goodbye request!', |
| 60 | + requestId, |
| 61 | + feature: 'request', |
| 62 | + }), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + after(async () => { |
| 67 | + await app.close(); |
| 68 | + }); |
| 69 | +}); |
0 commit comments