Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
How to mock a particular method of a class in jest?

In the provided code, I have a class with a method that concats a string. I have a function that creates an instance of the class and returns the result of the method. My goal is to create a jest mock implementation of methodOne so that it concats the strings in reverse order (Hello, World) becomes WorldHello. Can anyone help me figure out how to create a mock to accomplish this? The goal is for the second test to pass and for the serviceFunction to return the mocked methods implemented return.

handler.ts:


class Service {
    constructor() {}

    methodOne(a, b) {
        return a + b
    }
}

const serviceFunction = (a, b) => {

    const obj = new Service()
    return obj.methodOne(a, b)

}

module.exports = { Service, serviceFunction }

handler.test.ts:

const myModule = require('./handler.ts')

describe('Test Suite One', () => {
    it('should concat Strings', () => {
        const service = new myModule.Service()

        const result = myModule.serviceFunction("Hello", "World")
        expect(result).toEqual("HelloWorld")
    })

    it('should mock methodOne to reverse concat strings', () => {
        const service = new myModule.Service()
        const result = myModule.serviceFunction("Hello", "World")
        expect(result).toEqual('WorldHello')
    })
})
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda