Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
redux test with jest

I mocked the "createTask" function in the "api":

import configureMockStore from "redux-mock-store";
import { createTask } from "../../actions/tasks";
import * as api from "../../api";
import thunk from "redux-thunk";

jest.mock('../../api')
// mock api module
api.createTask = jest.fn(() => {
  return 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", () => {
    const expectedActions = [
      { type: "REQUEST_STARTED" },
      {
        type: "CREATE_TASK_SUCCEED",
        payLaod: { task: "foo" },
        meta: { analytics: { event: "create_task", data: { id: undefined } } },
      },
    ];
    const store = mockStore({
      tasks: {
        tasks: [],
      },
    });
    return store.dispatch(createTask({})).then(() => {
      expect(store.getActions()).isEqual(expectedActions);
      expect(api.createTask).toHaveBeenCalled();
    });
  });
});

But after running the test, I get this error. seem my mock function does not return a promise.

● create task action create store<async> › works

    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));

I do not know the reason for this. Can anyone help me?

about 4 years ago · Juan Pablo Isaza
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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda