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

131
Visualizações
occur error when I want to mock an function that return promise

i have an api directory for server communication that using axios:

import axios from "axios";
const API_BASE_URL = "http://localhost:3002";
axios.defaults.withCredentials = true;
const client = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    "Content-Type": "application/json",
  },
});

function fetchProjects() {
  return client.get("/projects?_embed=tasks");
}
function fetchTasks() {
  return client.get("/tasks");
}

function createTask(params) {
  return client.post("/tasks", params);
}

function changeStatus(id, params) {
  return client.put(`/tasks/${id}`, params);
}
export { fetchProjects, fetchTasks, createTask, changeStatus };

when i wanna mock the "createTask" function that export from this file, i get an error:

task.test.js

import configureMockStore from "redux-mock-store";
import { createTask } from "../../actions/tasks";
import * as api from "../../api";
import thunk from "redux-thunk";
// mock api module
jest.unmock("../../api")
api.createTask = jest.fn(() => Promise.resolve({data : 'foo'}))

// configuration mock store
const middleware = [thunk];
const mockStore = configureMockStore(middleware);
// suite tests
describe("create task action create store<async>", () => {
  test("works", async () => {
    const expectedActions = [
      { type: "REQUEST_STARTED" },
      {
        type: "CREATE_TASK_SUCCEED",
        payLoad: { task: "foo" },
        meta: { analytics: { event: "create_task", data: { id: undefined } } },
      },
    ];
    const store = mockStore({
      tasks: {
        tasks: [],
      },
    });
    await store.dispatch(createTask({}));
    expect(store.getActions()).toEqual(expectedActions);
  });
});

and the error is:

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

      23 |   return (dispatch) => {
      24 |     dispatch(requestStarted());
    > 25 |     api
         |     ^
      26 |       .createTask({ title, description, status, timer, projectId })
      27 |       .then((resp) => {
      28 |         dispatch(createTaskSucceed(resp.data));

it seems the mock function not return a Promsie and I don't know for what. when i mock api module with "jest.mock", it work without any error

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