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 simulacro de exportación exponiendo axios crear instancia

Esto es fácil, pero llevo 5 horas luchando y aún no tengo una solución.

 app.ts import { apiClient, getCustomFieldMapping } from './apiClient' const otherFunction = () => { // just to show that getCustomFieldMapping it's used getCustomFieldMapping() } // route /ext/:userId const callApi = async (path: string,) => { const url = apiClient.defaults.baseURL + path return apiClient.get(url); }
 apiClient.ts import axios from "axios"; export const apiClient = axios.create({ baseURL: "http://localhost:3000", auth: { username: `${config.zendeskUsername}/token`, password: config.zendeskToken, }, }); export const getCustomFieldMapping = ( from: "fieldId" | "name", value: string ): TicketFieldMapping => { const fieldMap = fieldMappings.find((mapping) => mapping[from] === value); if (!fieldMap) { throw { message: `Zendesk custom field's mapping from "${from}" "${value}" does not exists.`, }; } return fieldMap; };

Lo que realmente está sucediendo es que no he podido simular solo la llamada GET api desde el módulo apiClient.

He intentado muchas cosas (enumeradas después de esto) como:

 test.ts // first try import axios from 'axios' jest.mock("axios", () => ({ create: jest.fn(), })); test("Get user", async () => { (axios.create as jest.Mock).mockImplementation(() => { return { defaults: { baseURL: "https://localhost:3000", }, get: jest .fn() .mockResolvedValueOnce({}) }; }); return request(app) .get("/ext/121") .expect(StatusCodes.NOT_FOUND) .then((response) => { console.log(response.body); }); }); // I get that apiClient is undefined // another one is const mockGet = jest.fn(); jest.mock("./apiClient", () => { const actual = jest.requireActual("./apiClient"); actual.apiClient.get = jest.fn().mockImplementation(() => mockGet); return actual; }); test("Get user", async () => { mockGet.returnResolvedValueOnce({}) return request(app) .get("/ext/121") .expect(StatusCodes.NOT_FOUND) .then((response) => { console.log(response.body); }); }); // here I have two things.. one is that defualts is undefiend and it breaks the run // otherone it's that getCustomFieldMapping is undefined and can't be called

Quiero burlarme de la función GET de apiClient. eso es todo

Gracias por adelantado

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