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

206
Vistas
mockImplementation() still calling the original functionality of the method

Supposed I have a code

const SomeEmailModule = require('SomeEmailModule')
const emailModule = new SomeEmailModule()


async function sendEmail(htmlBody) {
    await emailModule.send(htmlBody)
    return htmlBody
}

and when I used it on test using jest

const SomeEmailModule = require('SomeEmailModule')

it('can test', async () => {

    const emailModule = new SomeEmailModule()
    jest.spyOn(emailModule, 'send').mockImplementation()
    ......some code
) 

the module doesn't actually mock the method send it stills do the original functionality of the method any idea why it happened?


Okay after some testing

const mockSend = jest.fn()
SomeEmailModule.prototype.send = mockSend

this solution works however I want to know how this one works while the other one doesn't


this one also works

const mockSend = jest.spyOne(SomeEmailModule.prototype, 'send').mockImplementation()
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Because the emailModule instance of the SomeEmailModule class in your test case is different from the one in the file under test.

jest.spyOn(emailModule, 'send').mockImplementation();

You just install a spy on the emailModule instance created in the test case, but the emailModule instance in the file under test is not spied.

But installing spy on SomeEmailModule.prototype.send() will work, because all instances share the same prototype.

about 4 years ago · Juan Pablo Isaza Denunciar
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