Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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();
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda