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

115
Visualizações
Create an AxiosError object for test purpose

I am looking for a way to easily create AxiosError (from the axios lib itself or an external helper)

The context.

I have this function that I want to test.

import { AxiosError } from 'axios';

export function isEmailConflict({ error }: { error: AxiosError<{ type?: string; message?: string }> }) {
// ...
}

So far I performed it in this way

import { AxiosError } from 'axios';
// ...
  describe('isEmailConflict', () => {
    function newConflicError(): AxiosError {
      return {
        isAxiosError: true,
        name: '',
        message: '',
        toJSON: () => ({}),
        config: {},
        response: {
          data: { type: 'aa', message: 'bb' },
          status: 409,
          statusText: 'Conflict',
          headers: {},
          config: {},
        },
      };
    }

    it('should return true if the passed error is of type email already used', () => {
      const error = newConflicError();
      //...
    });

but I find it ugly, is there an easy way to create an AxiosError ?

I already tried ts-auto-mock but I find it too intrusive in my tests config.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I used this package recently, its examples work like a charm

enter image description here https://www.npmjs.com/package/axios-error

about 4 years ago · Juan Pablo Isaza Relatório

0

Meanwhile, I had to switch to TypeScript, where this is the solution that worked for me (without axios-error):

import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import { AxiosError } from "axios";

[...]

  const responseData = "some string";
  const response: AxiosResponse = {
    data: responseData,
    status: 500,
  } as AxiosResponse;
  const axiosError = {
    config: {},
    request: {},
    response: response} as AxiosError<any>;
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