|
| 1 | +import { join } from 'path'; |
| 2 | +import { INestApplication } from '@nestjs/common'; |
| 3 | +import { ExpressAdapter } from '@nestjs/platform-express'; |
| 4 | +import { Test } from '@nestjs/testing'; |
| 5 | +import * as express from 'express'; |
| 6 | +import * as request from 'supertest'; |
| 7 | +import * as nunjucks from 'nunjucks'; |
| 8 | +import { ApplicationModule } from '../src/app.module'; |
| 9 | + |
| 10 | +interface IExpressNestApplication extends INestApplication { |
| 11 | + setBaseViewsDir(string): IExpressNestApplication |
| 12 | + setViewEngine(string): IExpressNestApplication |
| 13 | +} |
| 14 | + |
| 15 | +describe('Hello world MVC', () => { |
| 16 | + let server; |
| 17 | + let app: IExpressNestApplication; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + const module = await Test.createTestingModule({ |
| 21 | + imports: [ApplicationModule], |
| 22 | + }).compile(); |
| 23 | + |
| 24 | + const expressApp = express(); |
| 25 | + nunjucks.configure(join(__dirname, '..', 'src', 'views'), { |
| 26 | + autoescape: true, |
| 27 | + express: expressApp |
| 28 | + }); |
| 29 | + |
| 30 | + app = module.createNestApplication<IExpressNestApplication>(new ExpressAdapter(expressApp)); |
| 31 | + app.setViewEngine('njk') |
| 32 | + server = app.getHttpServer(); |
| 33 | + await app.init(); |
| 34 | + }); |
| 35 | + |
| 36 | + it(`/GET`, () => { |
| 37 | + return request(server) |
| 38 | + .get('/hello/mvc') |
| 39 | + .expect(200) |
| 40 | + .expect(/href="\/hello\/mvc/) |
| 41 | + }); |
| 42 | + |
| 43 | + it(`/GET/:id`, () => { |
| 44 | + const id = 5; |
| 45 | + return request(server) |
| 46 | + .get(`/hello/mvc/${id}`) |
| 47 | + .expect(200) |
| 48 | + .expect(new RegExp(`href="/hello/mvc/${id}`)) |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(async () => { |
| 52 | + await app.close(); |
| 53 | + }); |
| 54 | +}); |
0 commit comments