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

151
Visualizações
Mocking axios using interceptors (issue)

When running the following test to check if the login endpoint is being called with given arguments...

jest.mock("axios", () => {
  return {
    create: () => {
      return {
        interceptors: {
          request: { eject: jest.fn(), use: jest.fn() },
          response: { eject: jest.fn(), use: jest.fn() },
        },
      };
    },
  };
});

const mockedAxios = axios as jest.Mocked<typeof axios>;

it("should call login endpoint with given arguments", async () => {
  render(<MainContextProvider />);

  const mockedResponse = {
    succeeded: true,
    token: "eyJhbGc",
    firstName: "Test",
    lastName: "Test",
    email: "test@test.io",
    roles: ["Client"],
  };

  const { login } = authApi();

  mockedAxios.post.mockResolvedValue(mockedResponse);
  await login("test@test.io", "1234");
  expect(axios.post).toHaveBeenCalledWith(
    `${process.env.REACT_APP_URL_IDENTITY}/login`
  );
  expect(axios.post).toHaveBeenCalled();
});

I have the following error regarding mockResolvedValue property:

TypeError: Cannot read properties of undefined (reading 'mockResolvedValue')

  231 |     const { login } = authApi();
  232 |
> 233 |     mockedAxios.post.mockResolvedValue(mockedResponse);
      |                      ^
  234 |     await login("test@test.io", "1234");

authApi.js

const authApi = function () {
  const context = this;

  const identityService = axios.create({
    baseURL: process.env.REACT_APP_URL_IDENTITY,
  });

  identityService.interceptors.request.use(
    (config) => {
      config.headers.authorization = context.state.securityToken || null;

      return config;
    },
    (error) => Promise.reject(error)
  );

  return {
    login: async (email, pinCode) => {
      const request = { username: email, password: pinCode };

      const { data } = await identityService.post("/login", request);

      return data;
    },
  };
};

export default authApi;

Any ideas on how to fix this issue and make the test pass?

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