• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

198
Vistas
Jest, How to mock an imported callback in Object Destructuring?

I did the tests in the following way, but it bothers me how I need to import an object with the functions, instead of just importing the functions.

this works

//service.test.js
const get = require('../../modules/bankUser/model/getRegisteredUser');

    get.getRegisteredUser = jest.fn()
      .mockImplementationOnce(async () => mock)
      .mockImplementationOnce(async () => mockUpdated);

//service.js
const get = require('../model/getRegisteredUser');

const testedFunction () => {
  const depositReciver = await get.getRegisteredUser(depositName, depositCpf);
}

this dosent

//service.test.js
const get = require('../../modules/bankUser/model/getRegisteredUser');

    get.getRegisteredUser = jest.fn()
      .mockImplementationOnce(async () => mock)
      .mockImplementationOnce(async () => mockUpdated);

//service.js
const { getRegisteredUser } = require('../model/getRegisteredUser');

const testedFunction () => {
  const depositReciver = await getRegisteredUser(depositName, depositCpf);
}

I'm triyng this (way):

//service.test.js
const get = require('../../modules/bankUser/model/getRegisteredUser');

    jest.mock('../../modules/bankUser/model/getRegisteredUser');
    get.mockImplementationOnce(() => ({ getRegisteredUser: () => mockObject }));

jest returns this:

TypeError: get.mockImplementationOnce is not a function

also I've tried to import like this

//service.test.js
const { getRegisteredUser } = require('../../modules/bankUser/model/getRegisteredUser');

    jest.mock('../../modules/bankUser/model/getRegisteredUser');
    getRegisteredUser.mockImplementationOnce(() => ({ getRegisteredUser: () => mockObject }));

EDIT

//getRegisteredUser.js
const { getConnection } = require('../../../global/connection');

const getRegisteredUser = async (userName, cpf) => {
  const db = await getConnection('Data-Base');
  const res = await db.collection('Collection')
    .findOne({ userName, cpf });
  return res;
};

module.exports = { getRegisteredUser };
almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The example you are pointing [https://jestjs.io/docs/es6-class-mocks#replacing-the-mock-using-mockimplementation-or-mockimplementationonce][1] is related to the default export but I think you are using named export in your code.

There are multiple ways to do so

One Way

const get = require('../../modules/bankUser/model/getRegisteredUser');

jest.mock('../../modules/bankUser/model/getRegisteredUser');
get.getRegisteredUser.mockImplementationOnce(() => mockObject);

Second Way

jest.mock('../../modules/bankUser/model/getRegisteredUser', () => ({
  ...jest.requireActual('../../modules/bankUser/model/getRegisteredUser'),
  getRegisteredUser: jest.fn().mockImplementation(() => mockObject),
}))

An Another Way

We can also mock a module using the __mocks__ directory which is inbuilt feature of jest

And at the end never forgot to call clearAllMocks() after each test else it may impact the output of other tests.

afterEach(() => {
  jest.clearAllMocks();
});
almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda