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

170
Vistas
Function is not calling mocked constructor in Jest test

I have a setup similar to this question but the partial mocking answer is not working for me.

I have a file 'FiscalYear.jsx' which contains a class FiscalYear and a few related non-class functions eg

export class FiscalYear {
    constructor(fy) { //does stuff }
    // other functionality
}

export function getFY(offset) {
    // does stuff
    return new FiscalYear(fy);
}

I want my Jest test for the getFY function to simply check that the FiscalYear constructor was called with the right parameter. I used partial mocking in my test file to accomplish this:

import { FiscalYear, getFY } from '../FiscalYear';

jest.mock('../FiscalYear', () => {
    const originalModule = require.requireActual('../FiscalYear');
   
    return {
        ...originalModule,
        FiscalYear: jest.fn().mockImplementation((a) => { })
    };
});

test("getFY", () => {
    getFY(2);
    expect(FiscalYear).toHaveBeenCalled();
});

I'll update the toHaveBeenCalled to toBeCalledWith once I make it actually call the function. However currently the test fails with

expect(jest.fn()).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

I put a console.log in the real constructor and confirmed that the getFY function is calling that instead of the mock constructor.

How to get this working? Is it a bad design to have these both in the same file? Totally new to JavaScript so if this is bad practice please let me know.

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