I have a simple controller
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(@Res() res: Response) {
return res.json({"messagge":"this.appService.getHello()");
}
}
and I want to test it with unit test.
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Response } from 'express';
describe('AppController', () => {
let appController: AppController;
let responseObject = {
message: 'This is a simple example of item returned by your APIs.'
};
const response: Partial<Response> = {
json: jest.fn().mockImplementation((result) => {
responseObject = result;
})
}
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return my string', () => {
let res = appController.getHello(response as Response);
console.log("RES",res);
});
});
});
I need to mock Response, but I get undefined.