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

188
Visualizações
jest mock function dont work in my test case

when i import modules, i mock them with jest.mock() api :

import analytics, { fakeAnalyticsApi } from "../../middleware/analytics";
jest.mock("../../middleware/analytics", () => {
  const orginalModules = jest.requireActual("../../middleware/analytics");
  return {
    __esModule: true,
    ...orginalModules,
    fakeAnalyticsApi: jest.fn(() => Promise.resolve("success")),
  };
});

then when i call "fakeAnalyticsApi" function Its original version is executed.

full test file:

import analytics, { fakeAnalyticsApi } from "../../middleware/analytics";
jest.mock("../../middleware/analytics", () => {
  const orginalModules = jest.requireActual("../../middleware/analytics");
  return {
    __esModule: true,
    ...orginalModules,
    fakeAnalyticsApi: jest.fn(() => Promise.resolve("success")),
  };
});
const create = () => {
  const store = {
    getState: jest.fn(() => {}),
    dispatch: jest.fn(),
  };
  const next = jest.fn();
  const invoke = (action) => analytics(store)(next)(action);
  return { store, next, invoke };
};
// suite test
describe("analytics middleware", () => {
  test("should pass on irrelevant keys", () => {
    const { next, invoke } = create();
    const action = { type: "IRREVELANT" };
    invoke(action);
    expect(next).toHaveBeenCalledWith(action);
    expect(fakeAnalyticsApi).not.toHaveBeenCalled();
  });
  test("should make an analytics API call", () => {
    const { next, invoke } = create();
    const action = {
      type: "REVELANT",
      meta: {
        analytics: {
          event: "REVELANT",
          data: { extra: "stuff" },
        },
      },
    };
    invoke(action);
    expect(fakeAnalyticsApi).toHaveBeenCalled();
    expect(next).toHaveBeenCalledWith(action);
  });
});

and I get the following error:

● analytics middleware › should make an analytics API call

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

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

      39 |     };
      40 |     invoke(action);
    > 41 |     expect(fakeAnalyticsApi).toHaveBeenCalled();
         |                              ^
      42 |     expect(next).toHaveBeenCalledWith(action);
      43 |   });
      44 | });

I log a statement in orginal version function of "fakeAnalyticsApi", it seems orginal version was call because the console log the statement

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