.then(({body}: request.Response) => {
expect(body.data).toBe(appService.getHello())
}) // ({body}: value)
import { Controller, Get, HttpCode, Post, HttpStatus, Res } from '@nestjs/common';
import { AppService } from './app.service';
import { Response } from 'express';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(@Res() res: Response):object {
return res.send({data: this.appService.getHello('WTF it working!')});
}
}
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { AppModule } from './app.module';
import { AppService } from './app.service';
import { INestApplication } from '@nestjs/common';
describe('Cats', () => {
let app: INestApplication;
let appService = { getHello: () => 'test' };
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(AppService)
.useValue(appService)
.compile();
app = moduleRef.createNestApplication();
await app.init();
});
it(`/GET WTF`, () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect({
data: appService.getHello(),
});
});
afterAll(async () => {
await app.close();
});
});
PASS src/app.controller.spec.ts (6.898 s)
Cats
√ /GET WTF (55 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 7.087 s
Ran all test suites.
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { AppModule } from './app.module';
import { AppService } from './app.service';
import { INestApplication } from '@nestjs/common';
describe('Cats', () => {
let app: INestApplication;
let appService = { getHello: () => 'test' };
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(AppService)
.useValue(appService)
.compile();
app = moduleRef.createNestApplication();
await app.init();
});
it(`/GET WTF`, () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect({
data: appService.getHello(),
});
});
afterAll(async () => {
await app.close();
});
});
src/app.controller.spec.ts (17.215 s)
● Cats › /GET WTF
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
21 | });
22 |
> 23 | it(`/GET WTF`, () => {
| ^
24 | return request(app.getHttpServer())
25 | .get('/')
26 | .expect(200)
at app.controller.spec.ts:23:2
at Object. (app.controller.spec.ts:7:1)