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

188
Views
Mock Response express in nestjs

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.

UPDATE

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

I modified my code as above, but now I get always hello world even if my controller has a different value

  @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!