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

180
Vistas
Jest test axios error returned from a Promise. I get an "array"

I have this test:

await waitFor(() => {
            expect(store.getActions()).toMatchObject([{
                type: "TRY_INDEX_ROLES_ITEMS",
            },
            {
                type: "INDEX_ROLES_ITEMS_FAILED",
                error: "Error: Request failed with status code 401"
            }]);
        });

But I get in console:

expect(received).toMatchObject(expected)

    - Expected  - 1
    + Received  + 1

      Array [
        Object {
          "type": "TRY_INDEX_ROLES_ITEMS",
        },
        Object {
    -     "error": "Error: Request failed with status code 401",
    +     "error": [Error: Request failed with status code 401],
          "type": "INDEX_ROLES_ITEMS_FAILED",
        },
      ]

The difference is the "array" notation that really I receive.

In effect this is my action under test:

return this.getApi()
                .index(id)
                .then(data => {
                    dispatch({
                        type: `INDEX_ROLES_ITEMS_SUCCEEDED`,
                        data
                    });
                })
                .catch(error => {
                    dispatch({
                        type: `INDEX_ROLES_ITEMS_FAILED`,
                        error,
                    });
                })

And I get error from:

axiosInstance.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        return Promise.reject(error);
    }
);

I don't want to change error returned with error.toString() (it can works with error.toString(), I just tested it before), I want return from my Axios the error and solve that test.

Thank you

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Since you are using toMatchObject, you can do this.

expect(store.getActions()).toMatchObject([
  {
    type: 'TRY_INDEX_ROLES_ITEMS',
  },
  {
    type: 'INDEX_ROLES_ITEMS_FAILED',
    error: { message: 'Request failed with status code 401' },
  },
]);

Or

expect(store.getActions()).toMatchObject([
  {
    type: 'TRY_INDEX_ROLES_ITEMS',
  },
  {
    type: 'INDEX_ROLES_ITEMS_FAILED',
    error: expect.objectContaining({
      message: 'Request failed with status code 401',
    }),
  },
]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Not sure if that is what you are asking for, but have you tried just using the error.message?

return Promise.reject(error.message);

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