Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Respuesta simulada express en nestjs

tengo un controlador simple

 export class AppController { constructor(private readonly appService: AppService) {} @Get() getHello(@Res() res: Response) { return res.json({"messagge":"this.appService.getHello()"); } }

y quiero probarlo con prueba unitaria.

 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); }); }); });

Necesito burlarme de Response, pero me quedo indefinido.

ACTUALIZAR

 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 = { status: 200, message: 'Hello World!' }; const response: Partial<Response> = { status: jest.fn().mockImplementation().mockReturnValue(200), json: jest.fn().mockImplementation().mockReturnValue(responseObject), } beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], providers: [AppService], }).compile(); appController = app.get<AppController>(AppController); }); describe('root', () => { it('should return "Hello World!"', () => { let res = appController.getHello(response as Response); expect(res).toEqual(response.json()) }); }); });

Modifiqué mi código como se indicó anteriormente, pero ahora recibo siempre hola mundo incluso si mi controlador tiene un valor diferente

 @Get() getHello(@Res() res: Response) { return res.json({"message":"KO"}); // return res.status(HttpStatus.OK).json(this.appService.getHello()); }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!