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

184
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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